commit 7163b18f116aedebc13990a96db998d4569b01fb Author: lfirmin Date: Sun Nov 16 10:02:54 2025 +0100 github to gitea diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c166fae --- /dev/null +++ b/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin lfirmim@student.42.fr> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/08/12 07:07:31 by lfirmin #+# #+# # +# Updated: 2024/08/12 09:15:05 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +NAME = gnl.a +SRCS_DIR = srcs/ +INCS_DIR = includes/ +OBJ_DIR = obj/ + +SRC = get_next_line.c get_next_line_utils.c +CC = cc +CFLAGS = -Wall -Wextra -Werror -fPIE +INCLUDE = -I $(INCS_DIR) +SRCS = $(addprefix $(SRCS_DIR), $(SRC)) +OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) + +# Colors +GREEN = \033[0;32m +YELLOW = \033[0;33m +RESET = \033[0m +WHITE = \033[0;97m + +# Loading animation +LOADING_CHARS = ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏ + +all: $(NAME) + +$(NAME): $(OBJ) + @printf "$(YELLOW)Compiling get_next_line, Please wait...$(RESET)" + @for char in $(LOADING_CHARS); do \ + printf "\r$(YELLOW)Compiling get_next_line, Please wait... $$char$(RESET)"; \ + sleep 0.1; \ + done + @ar rc $(NAME) $(OBJ) + @ranlib $(NAME) + @printf "\r$(GREEN)fine ! $(WHITE)get_next_line compiled successfully ! $(RESET)\n" + +$(OBJ_DIR)%.o: $(SRCS_DIR)%.c + @mkdir -p $(OBJ_DIR) + @$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +clean: + @rm -rf $(OBJ_DIR) + @printf "$(WHITE)Clean process completed for $(GREEN)get_next_line.$(RESET)\n" + +fclean: clean + @rm -f $(NAME) + @printf "$(WHITE)Full clean process completed for $(GREEN)get_next_line.$(RESET)\n" + +re: fclean all + +.PHONY: all clean fclean re diff --git a/include/get_next_line.h b/include/get_next_line.h new file mode 100644 index 0000000..9ffb3e2 --- /dev/null +++ b/include/get_next_line.h @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: abelghou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/21 19:32:47 by abelghou #+# #+# */ +/* Updated: 2024/07/23 20:47:15 by abelghou ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 1000 +# endif + +# include +# include +# include +# include + +char *ft_substr(char const *s, unsigned int start, size_t len); +int ft_strlen_g(const char *str); +char *ft_strchr_gnl(const char *s, int i); +char *ft_strdup_g(const char *s); +char *ft_strjoin(char const *s1, char const *s2); +char *get_next_line(int fd); + +#endif \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..dcb9e4b --- /dev/null +++ b/readme.md @@ -0,0 +1,42 @@ +# get_next_line + +A function that reads a line from a file descriptor. + +## 💡 About +The get_next_line project is a programming function that reads a line ending with a newline character from a file descriptor. When calling the function in a loop, it will then allow you to read the text file pointed to by the file descriptor, one line at a time until the end of the file. + +## 🛠️ Function Prototype +```c +char *get_next_line(int fd); +``` +## 📋 Usage +```c +#include "get_next_line.h" +#include + +int main(void) +{ + int fd; + char *line; + + fd = open("test.txt", O_RDONLY); + while ((line = get_next_line(fd)) != NULL) + { + printf("%s", line); + free(line); + } + close(fd); + return (0); +} +``` + +## ⚠️ Return Value +* The line that was read when successful +* NULL if there is nothing else to read or if an error occurred + +## 📊 Expected Behavior +* Repeated calls to get_next_line() should let you read the text file pointed to by the file descriptor, one line at a time +* The function should return the line that was read +* If there is nothing else to read or if an error occurred, it should return NULL +* Call gnl with fd -2 for clear buffer +* The returned line should include the terminating \n character, except if the end of file was reached and does not end with a \n character diff --git a/srcs/Makefile b/srcs/Makefile new file mode 100644 index 0000000..c166fae --- /dev/null +++ b/srcs/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin lfirmim@student.42.fr> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/08/12 07:07:31 by lfirmin #+# #+# # +# Updated: 2024/08/12 09:15:05 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +NAME = gnl.a +SRCS_DIR = srcs/ +INCS_DIR = includes/ +OBJ_DIR = obj/ + +SRC = get_next_line.c get_next_line_utils.c +CC = cc +CFLAGS = -Wall -Wextra -Werror -fPIE +INCLUDE = -I $(INCS_DIR) +SRCS = $(addprefix $(SRCS_DIR), $(SRC)) +OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) + +# Colors +GREEN = \033[0;32m +YELLOW = \033[0;33m +RESET = \033[0m +WHITE = \033[0;97m + +# Loading animation +LOADING_CHARS = ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏ + +all: $(NAME) + +$(NAME): $(OBJ) + @printf "$(YELLOW)Compiling get_next_line, Please wait...$(RESET)" + @for char in $(LOADING_CHARS); do \ + printf "\r$(YELLOW)Compiling get_next_line, Please wait... $$char$(RESET)"; \ + sleep 0.1; \ + done + @ar rc $(NAME) $(OBJ) + @ranlib $(NAME) + @printf "\r$(GREEN)fine ! $(WHITE)get_next_line compiled successfully ! $(RESET)\n" + +$(OBJ_DIR)%.o: $(SRCS_DIR)%.c + @mkdir -p $(OBJ_DIR) + @$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +clean: + @rm -rf $(OBJ_DIR) + @printf "$(WHITE)Clean process completed for $(GREEN)get_next_line.$(RESET)\n" + +fclean: clean + @rm -f $(NAME) + @printf "$(WHITE)Full clean process completed for $(GREEN)get_next_line.$(RESET)\n" + +re: fclean all + +.PHONY: all clean fclean re diff --git a/srcs/get_next_line.c b/srcs/get_next_line.c new file mode 100644 index 0000000..94167e1 --- /dev/null +++ b/srcs/get_next_line.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: abelghou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 21:31:24 by abelghou #+# #+# */ +/* Updated: 2024/07/24 18:17:55 by abelghou ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../include/get_next_line.h" + +static char *sort_and_store(int fd, char *buf, char *backup) +{ + int read_line; + char *char_temp; + + read_line = 1; + while (read_line != 0) + { + read_line = read(fd, buf, BUFFER_SIZE); + if (read_line == -1) + return (0); + else if (read_line == 0) + break ; + buf[read_line] = '\0'; + if (!backup) + backup = ft_strdup_g(""); + if (!backup) + return (NULL); + char_temp = backup; + backup = ft_strjoin(char_temp, buf); + free(char_temp); + char_temp = NULL; + if (ft_strchr_gnl (buf, '\n')) + break ; + } + return (backup); +} + +static char *extract(char *line) +{ + size_t count; + char *backup; + + count = 0; + while (line[count] != '\n' && line[count] != '\0') + count++; + if (line[count] == '\0' || line[1] == '\0') + return (0); + backup = ft_substr(line, count + 1, ft_strlen_g(line) - count); + if (!backup) + return (NULL); + if (*backup == '\0') + { + free (backup); + backup = NULL; + } + line[count + 1] = '\0'; + return (backup); +} + +static char *process_line(char *line, char **backup) +{ + char *temp; + + *backup = extract(line); + temp = ft_strdup_g(line); + free(line); + if (!temp) + { + free(*backup); + *backup = NULL; + return (NULL); + } + return (temp); +} + +char *get_next_line(int fd) +{ + char *line; + char *buf; + static char *backup; + + if (fd == -2) + return (free(backup), backup = NULL, NULL); + if (fd < 0 || BUFFER_SIZE <= 0) + return (free(backup), backup = NULL, NULL); + buf = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1)); + if (!buf) + return (free(backup), backup = NULL, NULL); + line = sort_and_store(fd, buf, backup); + free(buf); + if (!line) + return (free(backup), backup = NULL, NULL); + return (process_line(line, &backup)); +} + +// int main() +// { +// int fd; +// char *line; +// fd = open("fd.txt", O_RDONLY); +// while((line = get_next_line(fd)) != NULL) +// { +// printf("%s", line); +// free(line); +// } +// line = get_next_line(fd); +// close(fd); +// } \ No newline at end of file diff --git a/srcs/get_next_line_utils.c b/srcs/get_next_line_utils.c new file mode 100644 index 0000000..5ba6d2d --- /dev/null +++ b/srcs/get_next_line_utils.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: abelghou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/21 19:34:18 by abelghou #+# #+# */ +/* Updated: 2024/07/23 20:06:41 by abelghou ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../include/get_next_line.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + size_t i; + size_t j; + char *str; + + str = (char *)malloc(sizeof(*s) * (len + 1)); + if (str == 0) + return (NULL); + i = 0; + j = 0; + while (s[i]) + { + if (i >= start && j < len) + { + str[j] = s[i]; + j++; + } + i++; + } + str[j] = '\0'; + return (str); +} + +int ft_strlen_g(const char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +char *ft_strchr_gnl(const char *s, int i) +{ + while (*s) + { + if (*s == i) + return ((char *)s); + s++; + } + if (i == '\0') + return ((char *)s); + return (0); +} + +char *ft_strdup_g(const char *s) +{ + int i; + int j; + char *str; + + i = 0; + j = ft_strlen_g(s); + str = (char *)malloc(sizeof(*str) * (j + 1)); + if (!str) + return (NULL); + while (i < j) + { + str[i] = s[i]; + i++; + } + str[i] = '\0'; + return (str); +} + +char *ft_strjoin(char const *s1, char const *s2) +{ + int i; + int j; + char *str; + + i = 0; + j = 0; + str = (char *)malloc(sizeof(char) * (ft_strlen_g(s1) \ + + ft_strlen_g(s2) + 1)); + if (str == NULL) + return (NULL); + while (s1[i] != '\0') + { + str[i] = s1[i]; + i++; + } + while (s2[j] != '\0') + { + str[i + j] = s2[j]; + j++; + } + str[i + j] = '\0'; + return (str); +} diff --git a/srcs/include/get_next_line.h b/srcs/include/get_next_line.h new file mode 100644 index 0000000..9ffb3e2 --- /dev/null +++ b/srcs/include/get_next_line.h @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: abelghou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/21 19:32:47 by abelghou #+# #+# */ +/* Updated: 2024/07/23 20:47:15 by abelghou ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 1000 +# endif + +# include +# include +# include +# include + +char *ft_substr(char const *s, unsigned int start, size_t len); +int ft_strlen_g(const char *str); +char *ft_strchr_gnl(const char *s, int i); +char *ft_strdup_g(const char *s); +char *ft_strjoin(char const *s1, char const *s2); +char *get_next_line(int fd); + +#endif \ No newline at end of file diff --git a/srcs/readme.md b/srcs/readme.md new file mode 100644 index 0000000..8e0d256 --- /dev/null +++ b/srcs/readme.md @@ -0,0 +1,41 @@ +# get_next_line + +A function that reads a line from a file descriptor. + +## 💡 About +The get_next_line project is a programming function that reads a line ending with a newline character from a file descriptor. When calling the function in a loop, it will then allow you to read the text file pointed to by the file descriptor, one line at a time until the end of the file. + +## 🛠️ Function Prototype +```c +char *get_next_line(int fd); +``` +## 📋 Usage +```c +#include "get_next_line.h" +#include + +int main(void) +{ + int fd; + char *line; + + fd = open("test.txt", O_RDONLY); + while ((line = get_next_line(fd)) != NULL) + { + printf("%s", line); + free(line); + } + close(fd); + return (0); +} +``` + +## ⚠️ Return Value +* The line that was read when successful +* NULL if there is nothing else to read or if an error occurred + +## 📊 Expected Behavior +* Repeated calls to get_next_line() should let you read the text file pointed to by the file descriptor, one line at a time +* The function should return the line that was read +* If there is nothing else to read or if an error occurred, it should return NULL +* The returned line should include the terminating \n character, except if the end of file was reached and does not end with a \n character \ No newline at end of file