diff --git a/Makefile b/Makefile index 30aa0a2..955dbc9 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,13 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/12/01 17:10:39 by adjoly #+# #+# # -# Updated: 2023/12/01 17:20:36 by adjoly ### ########.fr # +# Updated: 2023/12/04 17:04:21 by adjoly ### ########.fr # # # # **************************************************************************** # NAME = get_next_line.a -CC = cc +CC = clang SRCS = get_next_line.c \ get_next_line_utils.c \ @@ -21,13 +21,13 @@ OBJS = $(SRCS:.c=.o) FLAGS = -Werror -Wall -Wextra -HEADER = get_next_line.h \ +HEADER = get_next_line.h $(NAME): $(OBJS) - ar -rcs $(NAME) $(OBJS) + $(CC) -o $@ $(SRCS) -g -%.o: %.cc - $(CC) $(FLAGS) $(HEADER) $< -c -o $@ +%.o: %.c + $(CC) $(FLAGS) -I $(HEADER) $< -c -o $@ -g all: $(NAME) @@ -39,4 +39,4 @@ fclean: clean re: fclean all -.PHONY: clean all re fclean \ No newline at end of file +.PHONY: clean all re fclean diff --git a/a.out b/a.out deleted file mode 100755 index ac0d755..0000000 Binary files a/a.out and /dev/null differ diff --git a/get_next_line.c b/get_next_line.c index 1cf47b1..618f435 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ -/* Updated: 2023/12/01 23:03:24 by adjoly ### ########.fr */ +/* Updated: 2023/12/04 18:44:34 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,78 +17,81 @@ int check_line(char *buf) int i; i = 0; - while (buf[i] || buf[i] != '\n') + while (buf[i] && buf[i] != '\n') i++; if (buf[i] == '\n') - return (1); + return (i + (buf[i] == '\n')); return (0); } -void read_line(int fd, char *buf) -{ - char *tmp; - size_t read_nb; - - while (!check_line(buf) && read_nb != 0) - { - tmp = malloc((BUFFER_SIZE + 1) * sizeof(char)); - if (!tmp) - { - buf = NULL; - return ; - } - read_nb = read(fd, tmp, BUFFER_SIZE); - buf = ft_strjoin(buf, tmp); - free(tmp); - } -} - -char *line_to_str(char *buf) -{ - char *res; - size_t i; - size_t size_buf; - - i = 0; - size_buf = ft_strlen_til_nl(buf); - res = malloc((size_buf + 1) * sizeof(char)); - while (i < size_buf) - { - res[i] = buf[i]; - if (buf[i] == '\n') - break ; - i++; - } - free (buf); - return (res); -} - char *get_next_line(int fd) { static char *buf; char *res; + char *tmp; + int size; + int bytes_read; - if (BUFFER_SIZE <= 0 || read(fd, &res, 0) < 0 || fd < 0) + if (BUFFER_SIZE <= 0 || fd < 0) return (NULL); - read_line(fd, buf); - res = line_to_str(buf); - return (res); + res = malloc(1); + if (!res) + return (NULL); + res[0] = '\0'; + if (!buf) + { + buf = malloc((BUFFER_SIZE + 1) * sizeof(char)); + if (!buf) + return (NULL); + buf[0] = '\0'; + } + while (1) + { + tmp = ft_strjoin(res, buf); + if (!tmp) + return (NULL); + free(res); + res = tmp; + size = check_line(res); + bytes_read = read(fd, buf, BUFFER_SIZE); + buf[bytes_read] = 0; + if (bytes_read == 0 && ft_strlen(res) != 0) + { + free(buf); + buf = NULL; + return (res); + } + else if (bytes_read == 0) + return (NULL); + if (size != 0) + { + tmp = ft_strjoin(res, buf); + if (!tmp) + return (NULL); + free(res); + res = tmp; + ft_strlcpy(buf, &res[size], ft_strlen(&res[size])); + buf[ft_strlen(&res[size])] = 0; + res[size] = 0; + return (res); + } + } } #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 (1) { ln = get_next_line(fd); if (ln == NULL) - break ; + return (0); ft_putstr(ln); free(ln); } diff --git a/get_next_line.h b/get_next_line.h index bfd8eb9..dfa8270 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/01 22:31:30 by adjoly ### ########.fr */ +/* Updated: 2023/12/04 18:38:21 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,5 +24,6 @@ char *get_next_line(int fd); char *ft_strjoin(char *s1, char *s2); size_t ft_strlen_til_nl(char *s); size_t ft_strlen(char *s); +size_t ft_strlcpy(char *dst, char *src, size_t size); #endif \ No newline at end of file diff --git a/get_next_line.h.gch b/get_next_line.h.gch new file mode 100644 index 0000000..d5bd7b4 Binary files /dev/null and b/get_next_line.h.gch differ diff --git a/get_next_line_utils.c b/get_next_line_utils.c index 9beb70e..dfc42f0 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/01 22:31:36 by adjoly ### ########.fr */ +/* Updated: 2023/12/04 18:42:35 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ size_t ft_strlen(char *s) size_t i; i = 0; + if (s[i] == 0) + return (0); while (s[i]) i++; return (i); @@ -59,3 +61,20 @@ char *ft_strjoin(char *s1, char *s2) 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 - 1 && src[i]) + { + dst[i] = src[i]; + i++; + } + if (i < size) + dst[i] = '\0'; + return (ft_strlen(src)); +}