Skip to content

Commit

Permalink
finished
Browse files Browse the repository at this point in the history
  • Loading branch information
axelcacerest committed Sep 10, 2023
1 parent 06e8971 commit a949f01
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 18 deletions.
1 change: 0 additions & 1 deletion 1char.txt

This file was deleted.

5 changes: 3 additions & 2 deletions get_next_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 07:52:22 by acaceres #+# #+# */
/* Updated: 2023/09/10 05:20:21 by acaceres ### ########.fr */
/* Updated: 2023/09/10 05:46:09 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -85,6 +85,7 @@ static char
int i;
int j;

i = -1;
j = 0;
while (tmp)
{
Expand Down Expand Up @@ -123,6 +124,6 @@ static char *set_line(t_list **lst, char *line)
return (ft_lstclear(lst, free), NULL);
line = fill_line(lst, tmp, new_lst, line);
if (!line || line[0] == '\0')
return (free(line), ft_lstclear(lst, free), NULL);
return (free(line), line = NULL, ft_lstclear(lst, free), NULL);
return (line);
}
9 changes: 3 additions & 6 deletions get_next_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 07:48:59 by acaceres #+# #+# */
/* Updated: 2023/09/10 03:45:53 by acaceres ### ########.fr */
/* Created: 2023/09/10 05:27:38 by acaceres #+# #+# */
/* Updated: 2023/09/10 05:27:40 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,11 +28,8 @@ typedef struct s_list {
} t_list;

char *get_next_line(int fd);
void ft_find_line_break(t_list *node, ssize_t *size);
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void*));
t_list *ft_lstlast(t_list *lst);
t_list *create_node(char *content);

#endif
125 changes: 125 additions & 0 deletions get_next_line_bonus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 05:22:15 by acaceres #+# #+# */
/* Updated: 2023/09/10 05:23:42 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

#include "get_next_line_bonus.h"

static void create_list(int fd, t_list **lst)
{
t_list *node;
char *buff;
int _r;

_r = 1;
node = NULL;
if (*lst && (*lst)->nl != -1)
return ;
buff = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return (ft_lstclear(lst, free));
while (_r > 0)
{
_r = read(fd, buff, BUFFER_SIZE);
if (_r == -1)
return (free(buff), ft_lstclear(lst, free));
buff[_r] = '\0';
node = create_node(buff);
if (!node)
return (free(buff), ft_lstclear(lst, free));
ft_lstadd_back(lst, node);
if (node->nl != -1)
return (free(buff));
}
free(buff);
}

static ssize_t line_len(t_list **lst)
{
t_list *tmp;
ssize_t len;

len = 0;
if (!*lst)
return (0);
tmp = *lst;
while (tmp)
{
if (tmp->nl == -1)
len += tmp->len;
else
return (len += tmp->nl, ++len);
tmp = tmp->next;
}
return (len);
}

static char
*fill_line(t_list **lst, t_list *tmp, t_list *new_lst, char *line)
{
int i;
int j;

j = 0;
while (tmp)
{
i = -1;
while (tmp->content[++i])
{
if (tmp->content[i] == '\n')
{
line[j++] = tmp->content[i];
line[j] = '\0';
new_lst = create_node(&tmp->content[++i]);
if (!new_lst)
return (NULL);
return (ft_lstclear(lst, free),
ft_lstadd_back(lst, new_lst), line);
}
line[j++] = tmp->content[i];
}
tmp = tmp->next;
}
return (ft_lstclear(lst, free), line[j] = '\0', line);
}

static char *set_line(t_list **lst, char *line)
{
t_list *new_lst;
t_list *tmp;

tmp = NULL;
new_lst = NULL;
if (!*lst)
return (NULL);
tmp = *lst;
line = malloc(sizeof(char) * (line_len(lst) + 1));
if (!line)
return (ft_lstclear(lst, free), NULL);
line = fill_line(lst, tmp, new_lst, line);
if (!line || line[0] == '\0')
return (free(line), ft_lstclear(lst, free), NULL);
return (line);
}

char *get_next_line(int fd)
{
static t_list *lst[FD_MAX];
char *line;

line = NULL;
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, 0, 0) == -1)
return (ft_lstclear(&lst[fd], free), NULL);
create_list(fd, &lst[fd]);
line = set_line(&lst[fd], line);
if (!line)
return (NULL);
return (line);
}
41 changes: 41 additions & 0 deletions get_next_line_bonus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 05:26:30 by acaceres #+# #+# */
/* Updated: 2023/09/10 05:29:05 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 1
# endif
# ifndef FD_MAX
# define FD_MAX 1024
# endif
# include <unistd.h>
# include <stdlib.h>
# include <limits.h>
# include <stdio.h>

typedef struct s_list {
char *content;
ssize_t nl;
ssize_t len;
struct s_list *next;
} t_list;

char *get_next_line(int fd);
void ft_find_line_break(t_list *node, ssize_t *size);
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void*));
t_list *ft_lstlast(t_list *lst);
t_list *create_node(char *content);

#endif
83 changes: 83 additions & 0 deletions get_next_line_utils_bonus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 05:24:19 by acaceres #+# #+# */
/* Updated: 2023/09/10 05:24:24 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

#include "get_next_line_bonus.h"

void ft_lstclear(t_list **lst, void (*del)(void*))
{
t_list *current;
t_list *link;

if (!lst || !del)
return ;
current = *lst;
link = 0;
while (current)
{
link = current->next;
del(current->content);
free(current);
current = link;
}
*lst = NULL;
}

t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (0);
if (!lst->next)
return (lst);
return (ft_lstlast(lst->next));
}

void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *tail;

if (!*lst)
{
*lst = new;
return ;
}
tail = ft_lstlast(*lst);
tail->next = new;
}

t_list *create_node(char *content)
{
t_list *node;
int i;

i = 0;
if (!content)
return (NULL);
node = malloc(sizeof(t_list));
if (!node)
return (NULL);
while (content[i])
i++;
node->content = malloc(sizeof(char) * (i + 1));
if (!node->content)
return (free(node), NULL);
i = -1;
node->nl = -1;
node->len = -1;
while (content[++i])
{
if (node->nl == -1 && content[i] == '\n')
node->nl = i;
node->content[i] = content[i];
}
node->content[i] = '\0';
return (node->len = i, node->next = NULL, node);
}
7 changes: 0 additions & 7 deletions o.txt

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions main.c → test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/11 12:59:41 by acaceres #+# #+# */
/* Updated: 2023/09/10 04:27:57 by acaceres ### ########.fr */
/* Updated: 2023/09/10 05:37:30 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

#include "get_next_line.h"
#include "get_next_line_bonus.h"
#include <stdio.h>
#include <fcntl.h>

Expand Down

0 comments on commit a949f01

Please sign in to comment.