diff --git a/a.out b/a.out index e311974..edb2a47 100755 Binary files a/a.out and b/a.out differ diff --git a/get_next_line.c b/get_next_line.c index dacc830..f6c5497 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ -/* Updated: 2023/12/06 12:34:24 by adjoly ### ########.fr */ +/* Updated: 2023/12/08 11:31:04 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int check_line(char *buf) i = 0; while (buf[i] && buf[i] != '\n') i++; - if (buf[i] == '\n') + if (buf[i] == '\n' || buf[i] == '\0') return (i + (buf[i] == '\n')); return (0); } @@ -28,24 +28,28 @@ char *get_next_line(int fd) { static char *buf; char *res; - int size; + size_t size; int bytes_read; if (BUFFER_SIZE <= 0 || fd < 0) return (NULL); res = ft_calloc(1, 1); + size = BUFFER_SIZE; if (!buf) - buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1); + buf = malloc(sizeof(char) * (BUFFER_SIZE + 1)); while (buf && res) { - // size = check_line(res); bytes_read = read(fd, buf, BUFFER_SIZE); - res = ft_strjoin(res, buf); - buf[bytes_read] = '\0'; + size = check_line(buf); + res = ft_strljoin(res, buf, size); + // ft_strlcpy(buf, &res[size], ft_strlen(&res[size])); if (bytes_read < 1) { if (ft_strlen(res) > 0) + { + free(buf); return (res); + } free(buf); buf = NULL; return (NULL); @@ -65,12 +69,11 @@ int main(void) char *ln; int fd; fd = open("test.txt", O_RDONLY); - while (1) - { + // while (ln != NULL) + // { ln = get_next_line(fd); - if (ln == NULL) - return (0); ft_putstr(ln); free(ln); - } + // } + close(fd); } diff --git a/get_next_line.h b/get_next_line.h index 26f5e14..38bc24e 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */ -/* Updated: 2023/12/06 11:16:29 by adjoly ### ########.fr */ +/* Updated: 2023/12/08 11:12:12 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,9 @@ # endif char *get_next_line(int fd); -char *ft_strjoin(char *s1, char *s2); -size_t ft_strlen_til_nl(char *s); +char *ft_strljoin(char *s1, char *s2, size_t len); size_t ft_strlen(char *s); void *ft_calloc(size_t nmemb, size_t size); -size_t ft_strlcpy(char *dst, char *src, size_t size); +size_t ft_strlcpy(char *dst, char *src, size_t size); #endif \ No newline at end of file diff --git a/get_next_line.h.gch b/get_next_line.h.gch new file mode 100644 index 0000000..e894ef3 Binary files /dev/null and b/get_next_line.h.gch differ diff --git a/get_next_line_utils.c b/get_next_line_utils.c index d84998c..bb97260 100644 --- a/get_next_line_utils.c +++ b/get_next_line_utils.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */ -/* Updated: 2023/12/06 12:05:08 by adjoly ### ########.fr */ +/* Updated: 2023/12/08 11:17:51 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,14 +17,12 @@ size_t ft_strlen(char *s) size_t i; i = 0; - if (s[i] == 0) - return (0); while (s[i]) i++; return (i); } -size_t ft_strlen_til_nl(char *s) +/*size_t ft_strlen_til_nl(char *s) { size_t i; @@ -32,9 +30,9 @@ size_t ft_strlen_til_nl(char *s) while (s[i] && s[i] != '\n') i++; return (i); -} +}*/ -char *ft_strjoin(char *s1, char *s2) +char *ft_strljoin(char *s1, char *s2, size_t len) { char *result; size_t i; @@ -42,9 +40,9 @@ char *ft_strjoin(char *s1, char *s2) i = 0; j = 0; - if (s1 == NULL && s2 == NULL) + if (!s1 || !s2) return (NULL); - result = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char)); + result = ft_calloc((ft_strlen(s1) + len + 1), sizeof(char)); if (result == NULL) return (NULL); while (s1[i]) @@ -52,7 +50,7 @@ char *ft_strjoin(char *s1, char *s2) result[i] = s1[i]; i++; } - while (s2[j]) + while (s2[j] || j < len) { result[i] = s2[j]; i++; diff --git a/test.txt b/test.txt index 9f03ea6..8f485f7 100644 --- a/test.txt +++ b/test.txt @@ -1,4 +1,3 @@ Si tu lit cette ligne GGWP Main non tu peux voir celle ci aussi What tu lis celle la aussi -LLL \ No newline at end of file