diff --git a/a.out b/a.out index edb2a47..b3544e4 100755 Binary files a/a.out and b/a.out differ diff --git a/fff/get_next_line_utils_madumerg.c b/fff/get_next_line_utils_madumerg.c new file mode 100644 index 0000000..9f6d8fc --- /dev/null +++ b/fff/get_next_line_utils_madumerg.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/24 11:51:43 by madumerg #+# #+# */ +/* Updated: 2023/12/08 17:21:26 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +size_t ft_strlen(char *str) +{ + size_t i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +char *ft_strjoin(char *s1, char *s2) +{ + size_t len; + size_t i; + size_t j; + char *str; + + if (!s2) + return (NULL); + len = ft_strlen(s1) + ft_strlen(s2); + str = malloc(sizeof(char) * (len + 1)); + if (!str) + return (NULL); + i = 0; + while (s1[i] != '\0') + { + str[i] = s1[i]; + i++; + } + j = 0; + while (s2[j] != '\0') + str[i++] = s2[j++]; + free(s1); + str[i] = '\0'; + return (str); +} + +void *ft_calloc(size_t ct, size_t size) +{ + char *str; + size_t i; + + i = 0; + if (size == 0 || ct == 0) + return (malloc(1)); + if ((int)size < 0 && (int)ct < 0) + return (NULL); + if ((unsigned long long)(size * ct) > 4294967295) + return (NULL); + str = malloc(size * ct); + if (!str) + return (NULL); + while (i < (ct * size)) + { + *(unsigned char *)(str + i) = '\0'; + i++; + } + return (str); +} diff --git a/get_next_line.c b/get_next_line.c index f6c5497..4b5fa94 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,21 +6,34 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ -/* Updated: 2023/12/08 11:31:04 by adjoly ### ########.fr */ +/* Updated: 2023/12/08 18:17:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "get_next_line.h" -int check_line(char *buf) +char check_line(char *res, char *buf) { int i; + int j; i = 0; - while (buf[i] && buf[i] != '\n') + j = 0; + while (res[i] && res[i] != '\n') i++; - if (buf[i] == '\n' || buf[i] == '\0') - return (i + (buf[i] == '\n')); + if (res[i] == '\n') + { + i++; + while (res[i]) + { + buf[j] = res[i]; + i++; + j++; + } + buf[j] = 0; + res[i - j] = 0; + return (1); + } return (0); } @@ -28,39 +41,32 @@ char *get_next_line(int fd) { static char *buf; char *res; - size_t size; - int bytes_read; + ssize_t bytes_read; - if (BUFFER_SIZE <= 0 || fd < 0) - return (NULL); res = ft_calloc(1, 1); - size = BUFFER_SIZE; if (!buf) - buf = malloc(sizeof(char) * (BUFFER_SIZE + 1)); + buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1); while (buf && res) { + res = ft_strjoin(res, buf); + if (check_line(res, buf)) + return (res); bytes_read = read(fd, buf, BUFFER_SIZE); - 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; + if (res[0] != 0) + return (res); + free(res); return (NULL); } - else if (bytes_read >= 1) - return (res); + buf[bytes_read] = 0; } return (NULL); } -#include +/*#include void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}} #include #include @@ -69,11 +75,11 @@ int main(void) char *ln; int fd; fd = open("test.txt", O_RDONLY); - // while (ln != NULL) - // { + while (ln) + { ln = get_next_line(fd); ft_putstr(ln); free(ln); - // } + } close(fd); -} +}*/ diff --git a/get_next_line.h b/get_next_line.h index 38bc24e..d72585d 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/08 11:12:12 by adjoly ### ########.fr */ +/* Updated: 2023/12/08 18:01:49 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,9 @@ # endif char *get_next_line(int fd); -char *ft_strljoin(char *s1, char *s2, size_t len); +char *ft_strjoin(char *s1, char *s2); 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_strlen_nl(char *s); #endif \ No newline at end of file diff --git a/get_next_line_bonus.c b/get_next_line_bonus.c new file mode 100644 index 0000000..dc6235d --- /dev/null +++ b/get_next_line_bonus.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ +/* Updated: 2023/12/08 18:02:13 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line_bonus.h" + +char check_line(char *res, char *buf) +{ + int i; + int j; + + i = 0; + j = 0; + while (res[i] && res[i] != '\n') + i++; + if (res[i] == '\n') + { + i++; + while (res[i]) + { + buf[j] = res[i]; + i++; + j++; + } + buf[j] = 0; + res[i - j] = 0; + return (1); + } + return (0); +} + +char *get_next_line(int fd) +{ + static char *buf[1024]; + char *res; + size_t bytes_read; + + res = ft_calloc(1, 1); + if (!buf[fd]) + buf[fd] = ft_calloc(sizeof(char), BUFFER_SIZE + 1); + while (buf[fd] && res) + { + res = ft_strjoin(res, buf); + if (check_line(res, buf[fd])) + return (res); + bytes_read = read(fd, buf[fd], BUFFER_SIZE); + if (bytes_read < 1) + { + free(&buf[fd]); + buf[fd] = NULL; + if (res[0] != 0) + return (res); + free(res); + return (NULL); + } + buf[fd][bytes_read] = 0; + } + return (NULL); +} + +/*#include +void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}} +#include +#include +int main(void) +{ + char *ln; + int fd; + fd = open("test.txt", O_RDONLY); + while (ln) + { + ln = get_next_line(fd); + ft_putstr(ln); + free(ln); + } + close(fd); +}*/ diff --git a/get_next_line_bonus.h b/get_next_line_bonus.h new file mode 100644 index 0000000..9fa539a --- /dev/null +++ b/get_next_line_bonus.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */ +/* Updated: 2023/12/08 18:01:40 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_BONUS_H +# define GET_NEXT_LINE_BONUS_H + +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 5 +# endif + +char *get_next_line(int fd); +char *ft_strjoin(char *s1, char *s2); +size_t ft_strlen(char *s); +void *ft_calloc(size_t nmemb, size_t size); +size_t ft_strlen_nl(char *s); + +#endif \ No newline at end of file diff --git a/get_next_line_utils.c b/get_next_line_utils.c index bb97260..37fcc75 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/08 11:17:51 by adjoly ### ########.fr */ +/* Updated: 2023/12/08 17:52:41 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,17 +22,7 @@ size_t ft_strlen(char *s) return (i); } -/*size_t ft_strlen_til_nl(char *s) -{ - size_t i; - - i = 0; - while (s[i] && s[i] != '\n') - i++; - return (i); -}*/ - -char *ft_strljoin(char *s1, char *s2, size_t len) +char *ft_strjoin(char *s1, char *s2) { char *result; size_t i; @@ -40,9 +30,9 @@ char *ft_strljoin(char *s1, char *s2, size_t len) i = 0; j = 0; - if (!s1 || !s2) + if (!s2) return (NULL); - result = ft_calloc((ft_strlen(s1) + len + 1), sizeof(char)); + result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char)); if (result == NULL) return (NULL); while (s1[i]) @@ -50,7 +40,7 @@ char *ft_strljoin(char *s1, char *s2, size_t len) result[i] = s1[i]; i++; } - while (s2[j] || j < len) + while (s2[j]) { result[i] = s2[j]; i++; @@ -68,32 +58,34 @@ size_t ft_strlcpy(char *dst, char *src, size_t size) i = 0; if (size == 0) return (ft_strlen(src)); - while (i < size - 1 && src[i]) + while (i < size && src[i]) { dst[i] = src[i]; i++; } - if (i < size) - dst[i] = '\0'; + dst[size] = '\0'; return (ft_strlen(src)); } void *ft_calloc(size_t nmemb, size_t size) { + char *str; size_t i; - void *result; - if (size != 0 && nmemb != 0 && (nmemb * size) / nmemb != size) - return (NULL); + i = 0; if (nmemb == 0 || size == 0) return (malloc(1)); - result = malloc(size * nmemb); - if (result == NULL) + if ((int)size < 0 && (int)nmemb < 0) return (NULL); - while (i < size * nmemb) + if ((unsigned long long)(size * nmemb) > 4294967295) + return (NULL); + str = malloc(nmemb * size); + if (!str) + return (NULL); + while (i < (nmemb * size)) { - *(unsigned char *)(result + i) = '\0'; + *(unsigned char *)(str + i) = '\0'; i++; } - return (result); + return (str); } diff --git a/get_next_line_utils_bonus.c b/get_next_line_utils_bonus.c new file mode 100644 index 0000000..2d6e67e --- /dev/null +++ b/get_next_line_utils_bonus.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */ +/* Updated: 2023/12/08 17:54:32 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line_bonus.h" + +size_t ft_strlen(char *s) +{ + size_t i; + + i = 0; + while (s[i]) + i++; + return (i); +} + +char *ft_strjoin(char *s1, char *s2) +{ + char *result; + size_t i; + size_t j; + + i = 0; + j = 0; + if (!s2) + return (NULL); + result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char)); + if (result == NULL) + return (NULL); + while (s1[i]) + { + result[i] = s1[i]; + i++; + } + while (s2[j]) + { + result[i] = s2[j]; + i++; + j++; + } + free(s1); + result[i] = '\0'; + return (result); +} + +size_t ft_strlcpy(char *dst, char *src, size_t size) +{ + size_t i; + + i = 0; + if (size == 0) + return (ft_strlen(src)); + while (i < size && src[i]) + { + dst[i] = src[i]; + i++; + } + dst[size] = '\0'; + return (ft_strlen(src)); +} + +void *ft_calloc(size_t nmemb, size_t size) +{ + char *str; + size_t i; + + i = 0; + if (nmemb == 0 || size == 0) + return (malloc(1)); + if ((int)size < 0 && (int)nmemb < 0) + return (NULL); + if ((unsigned long long)(size * nmemb) > 4294967295) + return (NULL); + str = malloc(nmemb * size); + if (!str) + return (NULL); + while (i < (nmemb * size)) + { + *(unsigned char *)(str + i) = '\0'; + i++; + } + return (str); +} diff --git a/test.txt b/test.txt index 8f485f7..5be26ed 100644 --- a/test.txt +++ b/test.txt @@ -1,3 +1,8 @@ -Si tu lit cette ligne GGWP -Main non tu peux voir celle ci aussi -What tu lis celle la aussi +Si tu lis cette ligne GGWP +asdfasdf +sad +f + +sadf + +asdfasdff