diff --git a/libft/src/get_next_line/get_next_line.c b/libft/src/get_next_line/get_next_line.c new file mode 100644 index 0000000..f74a6e6 --- /dev/null +++ b/libft/src/get_next_line/get_next_line.c @@ -0,0 +1,124 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* get_next_line.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/07/15 16:59:55 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/07/15 17:00:24 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include + +static void gnl_clean(void **ptr) +{ + if (*ptr) + { + free(*ptr); + *ptr = NULL; + } +} + +static int gnl_check_and_extract(char **line, char **stock) +{ + char *temp1; + char *temp2; + + if (!stock || !*stock) + return (0); + temp1 = *stock; + while (temp1 && *temp1 != '\n') + if (!*temp1++) + return (0); + if (temp1) + temp1++; + temp2 = ft_strdup(temp1); + *temp1 = '\0'; + *line = ft_strdup(*stock); + free(*stock); + *stock = temp2; + if (!*line || !*stock) + { + gnl_clean((void **)line); + gnl_clean((void **)stock); + return (-1); + } + return (1); +} + +static int gnl_read(int fd, char **buffer, size_t size) +{ + int bytes_read; + + if (!*buffer) + { + *buffer = malloc(sizeof(char) * (BUFFER_SIZE + 1)); + if (!*buffer) + return (-1); + } + bytes_read = read(fd, *buffer, size); + if (bytes_read >= 0) + (*buffer)[bytes_read] = '\0'; + return (bytes_read); +} + +static int gnl_read_file(int fd, char **stock, char **line) +{ + char *buffer; + char *temp; + int bytes_read; + + buffer = NULL; + bytes_read = gnl_read(fd, &buffer, BUFFER_SIZE); + while (bytes_read > 0) + { + if (!*stock) + *stock = ft_strdup(buffer); + else + { + temp = *stock; + *stock = ft_strjoin(temp, buffer); + free(temp); + } + if (gnl_check_and_extract(line, stock)) + break ; + bytes_read = gnl_read(fd, &buffer, BUFFER_SIZE); + } + free(buffer); + if (!*stock) + return (-1); + return (bytes_read); +} + +char *get_next_line(int fd) +{ + static char *stock[OPEN_MAX]; + char *line; + int status; + + if (fd < 0 || fd >= OPEN_MAX || BUFFER_SIZE <= 0) + return (NULL); + if (stock[fd] && gnl_check_and_extract(&line, &stock[fd])) + return (line); + status = gnl_read_file(fd, &stock[fd], &line); + if (status > 0) + return (line); + else if (status < 0) + { + gnl_clean((void **)&stock[fd]); + return (NULL); + } + if (stock[fd] && *stock[fd]) + { + line = ft_strdup(stock[fd]); + gnl_clean((void **)&stock[fd]); + if (line) + return (line); + } + gnl_clean((void **)&stock[fd]); + return (NULL); +} \ No newline at end of file diff --git a/libft/src/string/ft_strcmp.c b/libft/src/string/ft_strcmp.c new file mode 100644 index 0000000..3da5c32 --- /dev/null +++ b/libft/src/string/ft_strcmp.c @@ -0,0 +1,11 @@ +#include + +int ft_strcmp(const char *s1, const char *s2) +{ + size_t i; + + i = 0; + while (s1[i] && s2[i] && s1[i] == s2[i]) + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} \ No newline at end of file