FEAT: add gnl makefile, have to fix check input

This commit is contained in:
LucasCodeur
2026-03-28 15:15:44 +01:00
parent b673d52c6b
commit d918306a68
8 changed files with 412 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2025/07/18 12:51:21 by lud-adam #+# #+# #
# Updated: 2026/03/28 15:07:38 by lud-adam ### ########.fr #
# #
# **************************************************************************** #
.PHONY: all clean fclean re debug force
CC = cc
CFLAGS = -Wall -Wextra -Werror -MMD
CFLAGS_DEBUG = -Wall -Wextra -MMD -g3 -D DEBUG=1
NAME = alum1
P_SRC = src/
P_OBJ = .obj/
P_INC = inc/
P_LIBFT = libft/
SRC = main.c \
get_next_line.c \
fill_array.c \
check_input.c
SRCS = $(addprefix $(P_SRC), $(SRC))
OBJS = $(patsubst $(P_SRC)%.c,$(P_OBJ)%.o,$(SRCS))
DEPS = $(OBJS:.o=.d)
LIBFT = $(P_LIBFT)libft.a
LIBS = -L$(P_LIBFT) -lft
all: $(NAME)
$(NAME): $(OBJS) $(LIBFT)
$(CC) $(CFLAGS) $(OBJS) $(LIBS) -o $(NAME)
$(P_OBJ)%.o: $(P_SRC)%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -I$(P_INC) -I$(P_LIBFT) -c $< -o $@
$(LIBFT):
$(MAKE) -C $(P_LIBFT)
clean:
rm -rf $(P_OBJ)
fclean: clean
rm -f $(NAME)
re: fclean all
debug:
$(MAKE) CFLAGS="$(CFLAGS_DEBUG)" all
-include $(DEPS)
+5
View File
@@ -0,0 +1,5 @@
8
5
3
2
1
+19
View File
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alcu.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 13:27:29 by lud-adam #+# #+# */
/* Updated: 2026/03/28 13:29:45 by lud-adam ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALCU_H
# define ALCU_H
char *read_file(int fd);
int check_input(int fd);
#endif
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/18 10:46:55 by lud-adam #+# #+# */
/* Updated: 2026/03/28 14:53:44 by lud-adam ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <stdlib.h>
# include <stddef.h>
# include <fcntl.h>
# include <unistd.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 1024
# endif
void ft_strcpy(char *src, char *dest);
char *build_line(char *src, char *c_remaining, size_t *index);
char *get_next_line(int fd);
#endif
+101
View File
@@ -0,0 +1,101 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 13:01:37 by dgaillet #+# #+# */
/* Updated: 2026/03/28 15:14:49 by lud-adam ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "../libft/libft.h"
#include "get_next_line.h"
// char *str_append(char *str, char *to_append) {
// char *new_str;
// int i;
// int j;
//
// if (!str)
// return to_append;
// new_str = malloc(sizeof(char) * (ft_strlen(str) + ft_strlen(to_append) + 1));
// if (!new_str)
// return NULL;
// i = -1;
// while (str[++i])
// new_str[i] = str[i];
// j = -1;
// while (to_append[++j])
// new_str[i + j] = to_append[j];
// new_str[i + j] = '\0';
// free(str);
// return (new_str);
// }
//
// char *read_file(int fd) {
// char *buf;
// int char_readed;
// char *content;
//
// content = NULL;
// buf = malloc(sizeof(char) * 2);
// if (!buf)
// return (NULL);
// buf[1] = '\0';
// char_readed = read(fd, buf, 1);
// while (char_readed > 0) {
// str_append(content, buf);
// char_readed = read(fd, buf, 1);
// }
// free(buf);
// return (content);
// }
//
void free_split(char **tab) {
int i;
i = 0;
while (tab[i]) {
free(tab[i]);
i++;
}
free(tab);
}
int check_input(int fd)
{
char **content;
int i;
int j;
char * line = NULL;
line = get_next_line(fd);
while (line != NULL)
{
}
content = ft_split(get_next_line(fd), '\n');
if (!content)
return (-2);
i = -1;
while (content[++i])
{
j = -1;
while (content[i][++j])
{
if (content[i][j] <= '0' && content[i][j] >= '9')
{
free_split(content);
return (-1);
}
}
}
free_split(content);
return (i);
}
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fill_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 13:21:46 by lud-adam #+# #+# */
/* Updated: 2026/03/28 14:02:21 by lud-adam ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft/libft.h"
void fill_array(char* file)
{
}
+137
View File
@@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 15:07:17 by lud-adam #+# #+# */
/* Updated: 2026/03/28 15:13:54 by lud-adam ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "../libft/libft.h"
size_t ft_strlen_with_c(char *str, char c)
{
size_t i;
i = 0;
if (!str)
return (0);
while (str[i] && str[i] != c)
i++;
return (i);
}
size_t detect_newline(char *str)
{
size_t i;
i = 0;
if (!str)
return (0);
while (str[i])
{
if (str[i] == '\n')
return (1);
i++;
}
return (0);
}
static void fill_line(char *line, char *c_remaining, char *src, size_t *index)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (c_remaining && c_remaining[i])
{
line[i] = c_remaining[i];
i++;
}
while (src[j] && src[j] != '\n')
{
line[i + j] = src[j];
src[j] = 0;
j++;
}
if (src[j] == '\n')
line[i + j] = '\n';
*index = j;
}
char *build_line(char *src, char *c_remaining, size_t *index)
{
char *line;
size_t size;
size_t is_jump;
is_jump = detect_newline(src);
size = ft_strlen_with_c(src, '\n') + ft_strlen_with_c(c_remaining, '\0') + is_jump + 1;
line = ft_calloc(size, sizeof(char));
if (!line)
{
free(c_remaining);
return (NULL);
}
if (!src)
return (NULL);
fill_line(line, c_remaining, src, index);
if (c_remaining)
free(c_remaining);
return (line);
}
static void read_file(int fd, char *buffer, ssize_t *nb_read)
{
*nb_read = read(fd, buffer, BUFFER_SIZE);
if (*nb_read < 0)
return ;
buffer[*nb_read] = '\0';
}
static char *get_the_line(int fd, char *buffer)
{
char *line;
size_t i;
ssize_t nb_read;
i = 0;
nb_read = 0;
line = NULL;
if (buffer[0] != '\0')
{
line = build_line(buffer, NULL, &i);
if (!line)
return (NULL);
ft_memcpy(buffer + i + 1, buffer, ft_strlen_with_c(buffer + i, '\0'));
}
while (detect_newline(line) != 0)
{
read_file(fd, buffer, &nb_read);
if (nb_read < 0)
return (free(line), NULL);
if (nb_read == 0)
break ;
line = build_line(buffer, line, &i);
ft_memcpy(buffer + i + 1, buffer, ft_strlen_with_c(buffer + i, '\0'));
}
return (line);
}
char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE + 1] = "";
char *line;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
line = get_the_line(fd, buffer);
if (!line)
return (NULL);
return (line);
}
+44
View File
@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 12:30:29 by lud-adam #+# #+# */
/* Updated: 2026/03/28 15:07:41 by lud-adam ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <fcntl.h>
#include "../libft/libft.h"
#include "alcu.h"
int main(int argc, char* argv[])
{
if (argc != 2)
{
ft_putstr_fd("ERROR", 2);
return (1);
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1)
{
ft_putstr_fd("ERROR", 2);
return (1);
}
int size = check_input(fd);
printf("size %d\n", size);
if (size < 0)
{
ft_putstr_fd("ERROR", 2);
return (1);
}
return (0);
}