diff --git a/Makefile b/Makefile index 16e73b9..1fddf37 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ WARNINGS = -Wall -Wextra -Werror CFLAGS = $(WARNINGS) VPATH = src:src/conversion:src/ft_printf:src/list:src/memory:src/output:\ - src/string + src/string:src/get_next_line SRC_CONVERSION = ft_atoi.c ft_itoa.c ft_tolower.c ft_toupper.c \ @@ -33,7 +33,7 @@ SRC_MEMORY = ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c \ SRC_STRING = ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c ft_isprint.c \ ft_split.c ft_strchr.c ft_strdup.c ft_striteri.c ft_strjoin.c \ ft_strlcat.c ft_strlcpy.c ft_strlen.c ft_strmapi.c ft_strncmp.c \ - ft_strnstr.c ft_strrchr.c ft_strtrim.c ft_substr.c + ft_strnstr.c ft_strrchr.c ft_strtrim.c ft_substr.c ft_strcmp.c SRC_LIST = ft_lstadd_back.c ft_lstadd_front.c ft_lstclear.c ft_lstdelone.c \ ft_lstiter.c ft_lstlast.c ft_lstmap.c ft_lstnew.c ft_lstsize.c @@ -45,8 +45,10 @@ SRC_FT_PRINTF = ft_isbase.c ft_putnbr_base.c ft_write_str.c parse_placeholder.c ft_putnbr_signed.c parse_conversion.c print_char.c \ print_number.c print_string.c +SRC_GET_NEXT_LINE = get_next_line.c + SOURCES = $(SRC_CONVERSION) $(SRC_MEMORY) $(SRC_STRING) \ - $(SRC_LIST) $(SRC_OUTPUT) $(SRC_FT_PRINTF) + $(SRC_LIST) $(SRC_OUTPUT) $(SRC_FT_PRINTF) $(SRC_GET_NEXT_LINE) OBJECTS = $(addprefix $(OBJ_DIR)/, $(SOURCES:.c=.o)) @@ -69,4 +71,7 @@ fclean: clean re: fclean all +debug: CFLAGS += -DDEBUG -g +debug: $(NAME) + .PHONY: all clean fclean re diff --git a/inc/ft_printf.h b/inc/ft_printf.h index 6e5740b..8801e4e 100644 --- a/inc/ft_printf.h +++ b/inc/ft_printf.h @@ -28,5 +28,4 @@ int print_pointer(va_list args); int parse_conversion(const char **format, va_list args); int parse_placeholder(const char **format, va_list args); - #endif diff --git a/inc/libft.h b/inc/libft.h index e859777..7814ae8 100644 --- a/inc/libft.h +++ b/inc/libft.h @@ -20,14 +20,6 @@ typedef struct s_list struct s_list *next; } t_list; -typedef struct s_clist -{ - void *content; - struct s_clist *next; - struct s_clist *prev; -} t_clist; - - int ft_isalpha(int c); int ft_isdigit(int c); int ft_isalnum(int c); @@ -45,6 +37,7 @@ int ft_tolower(int c); char *ft_strchr(const char *s, int c); char *ft_strrchr(const char *s, int c); int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_strcmp(const char *s1, const char *s2); void *ft_memchr(const void *s, int c, size_t n); int ft_memcmp(const void *s1, const void *s2, size_t n); char *ft_strnstr(const char *big, const char *little, size_t len); @@ -82,4 +75,13 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); */ int ft_printf(const char *format, ...); + +/* + * get_next_line +*/ +# define OPEN_MAX 1024 +# define BUFFER_SIZE 256 + +char *get_next_line(int fd); + #endif diff --git a/src/ft_printf/print_pointer.c b/src/ft_printf/print_pointer.c index aa25405..8c213d5 100644 --- a/src/ft_printf/print_pointer.c +++ b/src/ft_printf/print_pointer.c @@ -22,7 +22,7 @@ int print_pointer(va_list args) size_t len; char *str; int wrote; - + n = va_arg(args, unsigned long); if (!n) return (write(1, "(nil)", 5)); diff --git a/src/get_next_line/get_next_line.c b/src/get_next_line/get_next_line.c new file mode 100644 index 0000000..3c7edf0 --- /dev/null +++ b/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); +} diff --git a/src/string/ft_strcmp.c b/src/string/ft_strcmp.c new file mode 100644 index 0000000..de583cc --- /dev/null +++ b/src/string/ft_strcmp.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strcmp.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 20:06:19 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 20:07:08 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#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]); +}