diff --git a/get_next_line.c b/get_next_line.c index b980ebc..9ef66b8 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -12,10 +12,16 @@ #include "get_next_line.h" -void *read_line(char *str, char *buff, int fd, int y) +static char *read_line(int fd) { + char *buff; + int y; + + buff = ft_calloc(BUFFER_SIZE + 1, 1); + if (buff == NULL) + return (NULL); y = read(fd, buff, BUFFER_SIZE); - if (y == -1 || (ft_strlen(buff) == 0 && str == NULL)) + if (y == -1) { free(buff); return (NULL); @@ -24,102 +30,85 @@ void *read_line(char *str, char *buff, int fd, int y) return (buff); } -int get_next(char *str, char *buff, int y, int last_malloc) +static char *join_str(char *s1, char *s2) { - if (y == -1 || (ft_strlen(buff) == 0 && str == NULL)) + char *n_str; + int i; + int len; + + len = ft_strlen(s1); + n_str = ft_calloc(len + ft_strlen(s2) + 1, 1); + if (n_str == NULL) + return (NULL); + i = -1; + if (s1 != NULL) + while (s1[++i]) + n_str[i] = s1[i]; + i = -1; + while (s2[++i]) + n_str[i + len] = s2[i]; + n_str[i + len] = '\0'; + free(s1); + free(s2); + return (n_str); +} + +static char *get_text(char *save) +{ + int i; + char *new_s; + + i = 0; + while (save[i] && save[i] != '\n') + i++; + if (save[i] == '\n') + i++; + new_s = ft_calloc(i + 1, 1); + if (new_s == NULL) + return (NULL); + ft_strlcpy(new_s, save, i + 1); + ft_strlcpy(save, save + i, ft_strlen(save)); + return (new_s); +} + +static void *make_free(char *buff, char **save, int choice) +{ + if (choice == 1) { free(buff); - return (-1); - } - buff[y] = '\0'; - if (str == NULL) - { - str = ft_calloc(BUFFER_SIZE + 1, 1); - ft_strlcpy(str, buff, BUFFER_SIZE + 1); + free(*save); } else { - if ((size_t)last_malloc - 1 < ft_strlen(str) + ft_strlen(buff)) - { - str = ft_realloc(str, last_malloc); - last_malloc += BUFFER_SIZE; - } - ft_strlcpy(str + ft_strlen(str), buff, BUFFER_SIZE + 1); + free(*save); + *save = NULL; } - return (last_malloc); + return (NULL); } char *get_next_line(int fd) { char *buff; - static char *str; - size_t i; - int y; - static int last_malloc; + char *str; + static char *save; - last_malloc = BUFFER_SIZE + 1; - buff = ft_calloc(BUFFER_SIZE + 1, 1); - y = 1; - while ((str == NULL || ft_strchr(str, '\n') == -1) && y > 0) + buff = NULL; + while (buff == NULL || (ft_strchr(save, '\n') == -1)) { - y = read(fd, buff, BUFFER_SIZE); - last_malloc = get_next(str, buff, y, last_malloc); - if (last_malloc == -1) + buff = read_line(fd); + if (buff == NULL || (ft_strlen(buff) == 0 && ft_strlen(save) == 0)) + return (make_free(buff, &save, 1)); + if (ft_strlen(buff) == 0) + { + free(buff); + break ; + } + save = join_str(save, buff); + if (save == NULL) return (NULL); } - i = 0; - while (str[i] != '\n' && str[i]) - i++; - if (str[i] == '\n') - i++; - free(buff); - buff = malloc(i + 1); - ft_strlcpy(buff, str, i + 1); - buff[i] = '\0'; - ft_strlcpy(str, str + i, BUFFER_SIZE + 1); - if (ft_strlen(str) == 0) - { - free(str); - str = NULL; - } - return (buff); -} - -#include -#include - -int main(int argc, char *argv[]) -{ - char *r; - int fd; - (void) argc; - - fd = open(argv[1], 0); - r = get_next_line(fd); - printf("%s", r); - printf("\n"); - free(r); - r = get_next_line(fd); - printf("%s", r); - printf("\n"); - free(r); - r = get_next_line(fd); - printf("%s", r); - printf("\n"); - r = get_next_line(fd); - printf("%s", r); - printf("\n"); - r = get_next_line(fd); - printf("%s", r); - printf("\n"); - r = get_next_line(fd); - printf("%s", r); - printf("\n"); - r = get_next_line(fd); - printf("%s", r); - printf("\n"); - - free(r); - close(fd); - return (0); + str = get_text(save); + if (ft_strlen(save) == 0) + make_free(NULL, &save, 2); + return (str); } diff --git a/get_next_line.h b/get_next_line.h index 72a229d..529ac95 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/15 23:40:12 by erey-bet #+# #+# */ -/* Updated: 2022/10/15 23:51:30 by erey-bet ### ########.fr */ +/* Updated: 2022/11/15 19:42:13 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,9 @@ # include # include +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 41 +# endif char *get_next_line(int fd); void *ft_calloc(size_t nitems, size_t size); diff --git a/get_next_line_bonus.c b/get_next_line_bonus.c new file mode 100644 index 0000000..0e4b6aa --- /dev/null +++ b/get_next_line_bonus.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/15 23:42:35 by erey-bet #+# #+# */ +/* Updated: 2022/11/16 13:20:26 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +static char *read_line(int fd) +{ + char *buff; + int y; + + buff = ft_calloc(BUFFER_SIZE + 1, 1); + if (buff == NULL) + return (NULL); + y = read(fd, buff, BUFFER_SIZE); + if (y == -1) + { + free(buff); + return (NULL); + } + buff[y] = '\0'; + return (buff); +} + +static char *join_str(char *s1, char *s2) +{ + char *n_str; + int i; + int len; + + len = ft_strlen(s1); + n_str = ft_calloc(len + ft_strlen(s2) + 1, 1); + if (n_str == NULL) + return (NULL); + i = -1; + if (s1 != NULL) + while (s1[++i]) + n_str[i] = s1[i]; + i = -1; + while (s2[++i]) + n_str[i + len] = s2[i]; + n_str[i + len] = '\0'; + free(s1); + free(s2); + return (n_str); +} + +static char *get_text(char *save) +{ + int i; + char *new_s; + + i = 0; + while (save[i] && save[i] != '\n') + i++; + if (save[i] == '\n') + i++; + new_s = ft_calloc(i + 1, 1); + if (new_s == NULL) + return (NULL); + ft_strlcpy(new_s, save, i + 1); + ft_strlcpy(save, save + i, ft_strlen(save)); + return (new_s); +} + +static void *make_free(char *buff, char **save, int choice) +{ + if (choice == 1) + { + free(buff); + free(*save); + } + else + { + free(*save); + *save = NULL; + } + return (NULL); +} + +static char *get_next_line(int fd) +{ + char *buff; + char *str; + static char *save[1024]; + + if (fd < 0 || fd >= 1024) + return (NULL); + buff = NULL; + while (buff == NULL || ft_strchr(save[fd], '\n') == -1) + { + buff = read_line(fd); + if (buff == NULL || (ft_strlen(buff) == 0 && ft_strlen(save[fd]) == 0)) + return (make_free(buff, &save[fd], 1)); + if (ft_strlen(buff) == 0) + { + free(buff); + break; + } + save[fd] = join_str(save[fd], buff); + if (save[fd] == NULL) + return (NULL); + } + str = get_text(save[fd]); + if (ft_strlen(save[fd]) == 0) + make_free(NULL, &save[fd], 2); + return (str); +} diff --git a/get_next_line_bonus.h b/get_next_line_bonus.h new file mode 100644 index 0000000..f24775b --- /dev/null +++ b/get_next_line_bonus.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/15 23:40:12 by erey-bet #+# #+# */ +/* Updated: 2022/11/15 20:00:55 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_BONUS_H +# define GET_NEXT_LINE_BONUS_H + +# include +# include +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 41 +# endif + +char *get_next_line(int fd); +void *ft_calloc(size_t nitems, size_t size); +size_t ft_strlen(const char *str); +int ft_strchr(const char *str, int search); +void ft_strlcpy(char *dest, const char *src, size_t size); +void *ft_realloc(void *ptr, size_t size); + +#endif diff --git a/get_next_line_utils.c b/get_next_line_utils.c index f6a7043..ea28369 100644 --- a/get_next_line_utils.c +++ b/get_next_line_utils.c @@ -37,6 +37,8 @@ size_t ft_strlen(const char *str) { int i; + if (str == NULL) + return (0); i = 0; while (str[i] != '\0') i++; @@ -72,7 +74,7 @@ void ft_strlcpy(char *dest, const char *src, size_t size) dest[i] = '\0'; } -void *ft_realloc(void *ptr, size_t size) +void *ft_realloc(void *ptr, size_t new_size) { char *str; char *p; @@ -85,7 +87,7 @@ void *ft_realloc(void *ptr, size_t size) str[i] = p[i]; str[i] = '\0'; free(ptr); - p = ft_calloc(size + BUFFER_SIZE + 1, 1); + p = ft_calloc(new_size, 1); if (p == NULL) return (NULL); i = 0; diff --git a/get_next_line_utils_bonus.c b/get_next_line_utils_bonus.c new file mode 100644 index 0000000..b5e9a69 --- /dev/null +++ b/get_next_line_utils_bonus.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/11/07 17:19:41 by erey-bet #+# #+# */ +/* Updated: 2022/11/15 19:57:30 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +void *ft_calloc(size_t nitems, size_t size) +{ + size_t i; + char *tmp; + + if (nitems == 0 || size == 0) + return (malloc(0)); + if (nitems * size < nitems) + return (NULL); + tmp = malloc(nitems * size); + if (tmp == NULL) + return (NULL); + i = 0; + while (i < nitems * size) + { + tmp[i] = '\0'; + i++; + } + return (tmp); +} + +size_t ft_strlen(const char *str) +{ + int i; + + if (str == NULL) + return (0); + i = 0; + while (str[i] != '\0') + i++; + return (i); +} + +int ft_strchr(const char *str, int search) +{ + int i; + + i = 0; + while (str[i] || str[i] == (unsigned char)search) + { + if (str[i] == (unsigned char)search) + return (i); + i++; + } + return (-1); +} + +void ft_strlcpy(char *dest, const char *src, size_t size) +{ + size_t i; + + i = 0; + if (!size || !dest || !src) + return ; + while (i < size - 1 && src[i] != '\0') + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; +} + +void *ft_realloc(void *ptr, size_t new_size) +{ + char *str; + char *p; + int i; + + i = -1; + p = ptr; + str = ft_calloc(ft_strlen(p) + 1, 1); + while (p[++i]) + str[i] = p[i]; + str[i] = '\0'; + free(ptr); + p = ft_calloc(new_size, 1); + if (p == NULL) + return (NULL); + i = 0; + while (str[i]) + { + p[i] = str[i]; + i++; + } + p[i] = '\0'; + ptr = p; + free(str); + str = NULL; + return (ptr); +}