-
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
dce3248
commit fda232c
Showing
9 changed files
with
167 additions
and
6 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
a |
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,4 @@ | ||
Hola como | ||
estas, este es | ||
un test para | ||
el gnl |
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:46:09 by acaceres ### ########.fr */ | ||
/* Updated: 2023/09/11 06:10:13 by acaceres ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -19,10 +19,14 @@ char *get_next_line(int fd) | |
{ | ||
static t_list *lst; | ||
char *line; | ||
char *tmp; | ||
|
||
line = NULL; | ||
tmp = NULL; | ||
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, 0, 0) == -1) | ||
return (ft_lstclear(&lst, free), NULL); | ||
if (BUFFER_SIZE == 1) | ||
return (buff_size_1(fd, line, tmp)); | ||
create_list(fd, &lst); | ||
line = set_line(&lst, line); | ||
if (!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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: acaceres <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/09/10 05:27:38 by acaceres #+# #+# */ | ||
/* Updated: 2023/09/10 05:27:40 by acaceres ### ########.fr */ | ||
/* Updated: 2023/09/11 06:10:25 by acaceres ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -30,6 +30,7 @@ typedef struct s_list { | |
char *get_next_line(int fd); | ||
void ft_lstclear(t_list **lst, void (*del)(void*)); | ||
void ft_lstadd_back(t_list **lst, t_list *new); | ||
char *buff_size_1(int fd, char *line, char *tmp); | ||
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: acaceres <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/09/10 05:22:15 by acaceres #+# #+# */ | ||
/* Updated: 2023/09/10 05:23:42 by acaceres ### ########.fr */ | ||
/* Updated: 2023/09/11 06:11:04 by acaceres ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -47,6 +47,7 @@ static ssize_t line_len(t_list **lst) | |
ssize_t len; | ||
|
||
len = 0; | ||
tmp = NULL; | ||
if (!*lst) | ||
return (0); | ||
tmp = *lst; | ||
|
@@ -113,10 +114,14 @@ char *get_next_line(int fd) | |
{ | ||
static t_list *lst[FD_MAX]; | ||
char *line; | ||
char *tmp; | ||
|
||
line = NULL; | ||
tmp = NULL; | ||
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, 0, 0) == -1) | ||
return (ft_lstclear(&lst[fd], free), NULL); | ||
if (BUFFER_SIZE == 1) | ||
return (buff_size_1(fd, line, tmp)); | ||
create_list(fd, &lst[fd]); | ||
line = set_line(&lst[fd], line); | ||
if (!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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: acaceres <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/09/10 05:26:30 by acaceres #+# #+# */ | ||
/* Updated: 2023/09/11 04:04:32 by acaceres ### ########.fr */ | ||
/* Updated: 2023/09/11 06:11:17 by acaceres ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -33,6 +33,7 @@ typedef struct s_list { | |
char *get_next_line(int fd); | ||
void ft_lstclear(t_list **lst, void (*del)(void*)); | ||
void ft_lstadd_back(t_list **lst, t_list *new); | ||
char *buff_size_1(int fd, char *line, char *tmp); | ||
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: acaceres <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/05/02 07:58:06 by acaceres #+# #+# */ | ||
/* Updated: 2023/09/11 04:03:46 by acaceres ### ########.fr */ | ||
/* Updated: 2023/09/11 06:10:37 by acaceres ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -77,3 +77,59 @@ t_list *create_node(char *content) | |
node->content[i] = '\0'; | ||
return (node->len = i, node->next = NULL, node); | ||
} | ||
|
||
char *ft_strjoin_gnl(char const *s1, char const *s2) | ||
{ | ||
char *new_str; | ||
int i; | ||
int s1_len; | ||
int s2_len; | ||
|
||
i = 0; | ||
s1_len = 0; | ||
s2_len = 0; | ||
if (!s2) | ||
return (0); | ||
if (!s1) | ||
s1 = ""; | ||
while (s1[s1_len]) | ||
s1_len++; | ||
while (s2[s2_len]) | ||
s2_len++; | ||
new_str = malloc(((s1_len + s2_len) + 1) * sizeof(char)); | ||
if (!new_str) | ||
return (0); | ||
while (*s1) | ||
new_str[i++] = *s1++; | ||
while (*s2) | ||
new_str[i++] = *s2++; | ||
return (new_str[i] = '\0', new_str); | ||
} | ||
|
||
char *buff_size_1(int fd, char *line, char *tmp) | ||
{ | ||
char *buff; | ||
int _r; | ||
|
||
_r = 1; | ||
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1)); | ||
if (!buff) | ||
return (NULL); | ||
while (_r > 0) | ||
{ | ||
_r = read(fd, buff, BUFFER_SIZE); | ||
if (_r == -1) | ||
return (free(buff), NULL); | ||
buff[_r] = '\0'; | ||
tmp = line; | ||
line = ft_strjoin_gnl(line, buff); | ||
if (!line) | ||
return (free(buff), NULL); | ||
free(tmp); | ||
if (buff[0] == '\n') | ||
break ; | ||
} | ||
if (line[0] == '\0') | ||
return (free(buff), free(line), NULL); | ||
return (free(buff), 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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: acaceres <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/09/10 05:24:19 by acaceres #+# #+# */ | ||
/* Updated: 2023/09/11 04:03:54 by acaceres ### ########.fr */ | ||
/* Updated: 2023/09/11 06:16:10 by acaceres ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -77,3 +77,60 @@ t_list *create_node(char *content) | |
node->content[i] = '\0'; | ||
return (node->len = i, node->next = NULL, node); | ||
} | ||
|
||
char *ft_strjoin_gnl(char const *s1, char const *s2) | ||
{ | ||
char *new_str; | ||
int i; | ||
int s1_len; | ||
int s2_len; | ||
|
||
i = 0; | ||
s1_len = 0; | ||
s2_len = 0; | ||
if (!s2) | ||
return (0); | ||
if (!s1) | ||
s1 = ""; | ||
while (s1[s1_len]) | ||
s1_len++; | ||
while (s2[s2_len]) | ||
s2_len++; | ||
new_str = malloc(((s1_len + s2_len) + 1) * sizeof(char)); | ||
if (!new_str) | ||
return (0); | ||
while (*s1) | ||
new_str[i++] = *s1++; | ||
while (*s2) | ||
new_str[i++] = *s2++; | ||
return (new_str[i] = '\0', new_str); | ||
} | ||
|
||
char *buff_size_1(int fd, char *line, char *tmp) | ||
{ | ||
char *buff; | ||
int _r; | ||
|
||
_r = 1; | ||
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1)); | ||
if (!buff) | ||
return (NULL); | ||
while (_r > 0) | ||
{ | ||
_r = read(fd, buff, BUFFER_SIZE); | ||
if (_r == -1) | ||
return (free(buff), NULL); | ||
buff[_r] = '\0'; | ||
tmp = line; | ||
line = ft_strjoin_gnl(tmp, buff); | ||
if (!line) | ||
return (free(buff), NULL); | ||
free(tmp); | ||
if (buff[0] == '\n') | ||
break ; | ||
} | ||
free(buff); | ||
if (line[0] == '\0') | ||
return (free(line), 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,32 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* main.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: acaceres <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/05/11 12:59:41 by acaceres #+# #+# */ | ||
/* Updated: 2023/09/11 06:13:48 by acaceres ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "get_next_line_bonus.h" | ||
#include <stdio.h> | ||
#include <fcntl.h> | ||
|
||
int main(void) | ||
{ | ||
int fd; | ||
char *line; | ||
|
||
fd = open("b.txt", O_RDONLY); | ||
while (1) | ||
{ | ||
line = get_next_line(fd); | ||
printf("line: %s", line); | ||
free(line); | ||
if (!line) | ||
return (0); | ||
} | ||
return (0); | ||
} |