From b9a5e7c9ae556988c62dd9efeaf17060ad34bab0 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 13 Nov 2019 15:11:04 +0100 Subject: Added get_next_line --- Makefile | 2 +- get_next_line.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ get_next_line.h | 35 +++++++++++++++++ libft.h | 1 + 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 get_next_line.c create mode 100644 get_next_line.h diff --git a/Makefile b/Makefile index f713b34..f7caf07 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \ ft_strncmp.c ft_strncpy.c ft_strnequ.c ft_strnew.c ft_strnstr.c \ ft_strrchr.c ft_split.c ft_strstr.c ft_substr.c ft_strtrim.c \ ft_tolower.c ft_toupper.c ft_strlcpy.c ft_calloc.c ft_strndup.c \ - ft_strjoin_free.c ft_strjoin_free_snd.c + ft_strjoin_free.c ft_strjoin_free_snd.c get_next_line.c OBJ = $(SRC:.c=.o) INCLUDE = libft.h diff --git a/get_next_line.c b/get_next_line.c new file mode 100644 index 0000000..61d74c0 --- /dev/null +++ b/get_next_line.c @@ -0,0 +1,119 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */ +/* Updated: 2019/11/04 00:00:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "libft.h" +#include "get_next_line.h" + +/* +** if has rest: +** if rest has newline: +** push rest until newline in line, shift rest +** return LINE_READ +** else: +** push rest in line +** +** while can read fd in buf +** if buf has newline: +** push buf until newline in line +** push buf after newline in rest +** return LINE_READ +** push buf in line +** +** return GNL_EOF +*/ + +#define HAS_NEWLINE(str, split_at) ((split_at = find_newline(str)) != -1) + +int get_next_line(int fd, char **line) +{ + int split_at; + static char rest[OPEN_MAX][BUFFER_SIZE + 1] = {{0}}; + + if (fd < 0 || fd > OPEN_MAX || line == NULL || BUFFER_SIZE <= 0) + return (STATUS_ERROR); + if ((*line = ft_strdup("")) == NULL) + return (STATUS_ERROR); + if (rest[fd][0] == '\0') + return (read_line(fd, line, rest[fd])); + if (HAS_NEWLINE(rest[fd], split_at)) + { + free(*line); + if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) + return (STATUS_ERROR); + ft_strncpy(*line, rest[fd], split_at); + (*line)[split_at] = '\0'; + ft_strcpy(rest[fd], rest[fd] + split_at + 1); + return (STATUS_LINE); + } + free(*line); + if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) + return (STATUS_ERROR); + ft_strcpy(*line, rest[fd]); + rest[fd][0] = '\0'; + return (read_line(fd, line, rest[fd])); +} + +int read_line(int fd, char **line, char *rest) +{ + int ret; + int split_at; + char *buf; + + if ((buf = malloc(sizeof(char) * (BUFFER_SIZE + 1))) == NULL) + return (free_return(line, NULL, STATUS_ERROR)); + while ((ret = read(fd, buf, BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if (HAS_NEWLINE(buf, split_at)) + { + ft_strcpy(rest, buf + split_at + 1); + buf[split_at] = '\0'; + if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL) + return (free_return(&buf, NULL, STATUS_ERROR)); + return (free_return(&buf, NULL, STATUS_LINE)); + } + if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL) + return (free_return(&buf, NULL, STATUS_ERROR)); + } + if (ret == -1) + return (free_return(&buf, line, STATUS_ERROR)); + return (free_return(&buf, NULL, ret)); +} + +int find_newline(char *str) +{ + int i; + + i = -1; + while (str[++i]) + if (str[i] == '\n') + return (i); + return (-1); +} + +int free_return(char **ptr, char **ptr2, int ret) +{ + if (ptr != NULL) + { + free(*ptr); + *ptr = NULL; + } + if (ptr2 != NULL) + { + free(*ptr2); + *ptr2 = NULL; + } + return (ret); +} diff --git a/get_next_line.h b/get_next_line.h new file mode 100644 index 0000000..4afd0fb --- /dev/null +++ b/get_next_line.h @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */ +/* Updated: 2019/11/03 22:43:18 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 32 +# endif + +# define STATUS_LINE 1 +# define STATUS_EOF 0 +# define STATUS_ERROR -1 + +/* +** get_next_line.c +*/ + +int get_next_line(int fd, char **line); +int read_line(int fd, char **line, char *rest); +int find_newline(char *str); +int free_return(char **ptr, char **rest, int ret); + +#endif diff --git a/libft.h b/libft.h index 7303028..1dff1c0 100644 --- a/libft.h +++ b/libft.h @@ -14,6 +14,7 @@ # define LIBFT_H # include +# include "get_next_line.h" # define TRUE 1 # define FALSE 0 -- cgit From d8e5d376244c6cf92e52666634cddaaf4e492aad Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 14 Nov 2019 10:25:21 +0100 Subject: ft_strjoin free variante using ft_strjoin --- ft_strjoin_free.c | 9 +++------ ft_strjoin_free_snd.c | 7 ++----- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/ft_strjoin_free.c b/ft_strjoin_free.c index 18ce83d..4050b77 100644 --- a/ft_strjoin_free.c +++ b/ft_strjoin_free.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/05 21:32:48 by cacharle #+# #+# */ -/* Updated: 2019/11/05 22:17:03 by cacharle ### ########.fr */ +/* Updated: 2019/11/14 10:07:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,10 @@ char *ft_strjoin_free(char const *s1, char const *s2, int free_nb) { char *joined; - if (s1 == NULL || s2 == NULL) + if (s1 == NULL || s2 == NULL || free_nb < 0 || free_nb > 2) return (NULL); - if ((joined = (char*)malloc(sizeof(char) - * (ft_strlen(s1) + ft_strlen(s2) + 1))) == NULL) + if ((joined = ft_strjoin(s1, s2)) == NULL) return (NULL); - joined = ft_strcpy(joined, s1); - joined = ft_strcat(joined, s2); if (free_nb == 1) free((void*)s1); if (free_nb == 2) diff --git a/ft_strjoin_free_snd.c b/ft_strjoin_free_snd.c index 46e36ae..0503211 100644 --- a/ft_strjoin_free_snd.c +++ b/ft_strjoin_free_snd.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/05 22:12:56 by cacharle #+# #+# */ -/* Updated: 2019/11/05 22:17:15 by cacharle ### ########.fr */ +/* Updated: 2019/11/14 10:07:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,11 +19,8 @@ char *ft_strjoin_free_snd(char const *s1, char const *s2) if (s1 == NULL || s2 == NULL) return (NULL); - if ((joined = (char*)malloc(sizeof(char) - * (ft_strlen(s1) + ft_strlen(s2) + 1))) == NULL) + if ((joined = ft_strjoin(s1, s2)) == NULL) return (NULL); - joined = ft_strcpy(joined, s1); - joined = ft_strcat(joined, s2); free((void*)s2); return (joined); } -- cgit From e0f11e486518930e82e67f2dc305595671c074b5 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 15 Nov 2019 09:34:14 +0100 Subject: Added ft_strcount --- Makefile | 4 ++-- ft_strcount.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 ft_strcount.c diff --git a/Makefile b/Makefile index f7caf07..b7ad478 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/08 15:45:53 by cacharle #+# #+# # -# Updated: 2019/11/05 22:16:32 by cacharle ### ########.fr # +# Updated: 2019/11/15 09:19:31 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -28,7 +28,7 @@ SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \ ft_strncmp.c ft_strncpy.c ft_strnequ.c ft_strnew.c ft_strnstr.c \ ft_strrchr.c ft_split.c ft_strstr.c ft_substr.c ft_strtrim.c \ ft_tolower.c ft_toupper.c ft_strlcpy.c ft_calloc.c ft_strndup.c \ - ft_strjoin_free.c ft_strjoin_free_snd.c get_next_line.c + ft_strjoin_free.c ft_strjoin_free_snd.c get_next_line.c ft_strcount.c OBJ = $(SRC:.c=.o) INCLUDE = libft.h diff --git a/ft_strcount.c b/ft_strcount.c new file mode 100644 index 0000000..87e756d --- /dev/null +++ b/ft_strcount.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcount.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:17:48 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:19:36 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcount(char *str, char c) +{ + int counter; + + if (c == '\0') + return (1); + counter = 0; + while (*str) + if (*str++ == c) + counter++; + return (counter); +} -- cgit From d0c146c2274198814e106b17ea1f9461a1b0b81a Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 15 Nov 2019 16:23:11 +0100 Subject: Linux OPEN_MAX --- Makefile | 5 ++++- ft_putchar_fd.c | 3 +-- ft_putendl_fd.c | 1 - ft_putnbr_fd.c | 2 -- ft_putstr_fd.c | 3 +-- get_next_line.c | 4 ---- get_next_line.h | 3 +++ libft.h | 10 +++++++++- 8 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index b7ad478..b7f8641 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,10 @@ RM = rm -f CC = gcc CCFLAGS = -Wall -Wextra -Werror +ifeq ($(shell uname),Linux) + CCFLAGS += -D LINUX +endif + NAME = libft.a SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \ ft_isprint.c ft_itoa.c ft_memalloc.c ft_memccpy.c ft_memchr.c \ @@ -29,7 +33,6 @@ SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \ ft_strrchr.c ft_split.c ft_strstr.c ft_substr.c ft_strtrim.c \ ft_tolower.c ft_toupper.c ft_strlcpy.c ft_calloc.c ft_strndup.c \ ft_strjoin_free.c ft_strjoin_free_snd.c get_next_line.c ft_strcount.c - OBJ = $(SRC:.c=.o) INCLUDE = libft.h diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c index a06b8b1..78d94a8 100644 --- a/ft_putchar_fd.c +++ b/ft_putchar_fd.c @@ -10,8 +10,7 @@ /* */ /* ************************************************************************** */ -#include -#include +#include "libft.h" void ft_putchar_fd(char c, int fd) { diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c index 8b77884..399b03e 100644 --- a/ft_putendl_fd.c +++ b/ft_putendl_fd.c @@ -10,7 +10,6 @@ /* */ /* ************************************************************************** */ -#include #include "libft.h" void ft_putendl_fd(char *s, int fd) diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c index 562d289..89cae15 100644 --- a/ft_putnbr_fd.c +++ b/ft_putnbr_fd.c @@ -10,8 +10,6 @@ /* */ /* ************************************************************************** */ -#include -#include #include "libft.h" void ft_putnbr_fd(int n, int fd) diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c index 1f2bbda..645c133 100644 --- a/ft_putstr_fd.c +++ b/ft_putstr_fd.c @@ -10,8 +10,7 @@ /* */ /* ************************************************************************** */ -#include -#include +#include "libft.h" void ft_putstr_fd(char *s, int fd) { diff --git a/get_next_line.c b/get_next_line.c index 61d74c0..f7d9c0e 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -10,10 +10,6 @@ /* */ /* ************************************************************************** */ -#include -#include -#include -#include "libft.h" #include "get_next_line.h" /* diff --git a/get_next_line.h b/get_next_line.h index 4afd0fb..c3962e3 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -13,7 +13,10 @@ #ifndef GET_NEXT_LINE_H # define GET_NEXT_LINE_H +# include +# include # include +# include "libft.h" # ifndef BUFFER_SIZE # define BUFFER_SIZE 32 diff --git a/libft.h b/libft.h index 1dff1c0..aedf621 100644 --- a/libft.h +++ b/libft.h @@ -6,16 +6,23 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */ -/* Updated: 2019/11/05 22:19:15 by cacharle ### ########.fr */ +/* Updated: 2019/11/16 08:12:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef LIBFT_H # define LIBFT_H +# include # include +# include # include "get_next_line.h" +# ifdef LINUX +# include +# define OPEN_MAX FOPEN_MAX +# endif + # define TRUE 1 # define FALSE 0 @@ -107,5 +114,6 @@ void ft_lstpop_front(t_list **lst, void (*del)(void *)); char *ft_strjoin_free(char const *s1, char const *s2, int free_nb); char *ft_strjoin_free_snd(char const *s1, char const *s2); +int ft_strcount(char *str, char c); #endif -- cgit