-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06e8971
commit a949f01
Showing
9 changed files
with
257 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -85,6 +85,7 @@ static char | |
int i; | ||
int j; | ||
|
||
i = -1; | ||
j = 0; | ||
while (tmp) | ||
{ | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
||
|