github to gitea

This commit is contained in:
lfirmin 2025-11-16 10:02:54 +01:00
commit 7163b18f11
8 changed files with 486 additions and 0 deletions

60
Makefile Normal file
View File

@ -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

32
include/get_next_line.h Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abelghou <abelghou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
# include <unistd.h>
# include <stdio.h>
# include <fcntl.h>
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

42
readme.md Normal file
View File

@ -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 <fcntl.h>
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

60
srcs/Makefile Normal file
View File

@ -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

113
srcs/get_next_line.c Normal file
View File

@ -0,0 +1,113 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abelghou <abelghou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
// }

106
srcs/get_next_line_utils.c Normal file
View File

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abelghou <abelghou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abelghou <abelghou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
# include <unistd.h>
# include <stdio.h>
# include <fcntl.h>
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

41
srcs/readme.md Normal file
View File

@ -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 <fcntl.h>
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