diff --git a/alum1 b/alum1 new file mode 100755 index 0000000..14f3bb6 Binary files /dev/null and b/alum1 differ diff --git a/check_input.c b/check_input.c deleted file mode 100644 index f96e177..0000000 --- a/check_input.c +++ /dev/null @@ -1,90 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parsing.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: dgaillet +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2026/03/28 13:01:37 by dgaillet #+# #+# */ -/* Updated: 2026/03/28 13:01:38 by dgaillet ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -#include "./libft/libft.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; - - content = ft_split(read_file(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); -} diff --git a/src/check_input.c b/src/check_input.c index 639b4ff..eae2013 100644 --- a/src/check_input.c +++ b/src/check_input.c @@ -14,88 +14,26 @@ #include #include +#include "../inc/get_next_line.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 check_input(int fd) { int i; + int j; + char *line = NULL; - i = 0; - while (tab[i]) { - free(tab[i]); - i++; + line = get_next_line(fd); + j = 0; + while (line != NULL) { + i = -1; + while (line[++i]) + if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n') + return (free(line), -1); + if (ft_atoi(line) < 1 || ft_atoi(line) > 10000) + return (free(line), -1); + free(line); + j++; + line = get_next_line(fd); } - 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); + return (j); } diff --git a/src/get_next_line.c b/src/get_next_line.c index 0b3a76d..3da4584 100644 --- a/src/get_next_line.c +++ b/src/get_next_line.c @@ -13,125 +13,113 @@ #include "get_next_line.h" #include "../libft/libft.h" -size_t ft_strlen_with_c(char *str, char c) -{ - size_t i; +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); + i = 0; + if (!str) + return (0); + while (str[i] && str[i] != c) + i++; + return (i); } -size_t detect_newline(char *str) -{ - size_t 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); + 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; +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; + 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; +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); + 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 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; +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); + i = 0; + nb_read = 0; + line = NULL; + if (buffer[0] != '\0') { + line = build_line(buffer, NULL, &i); + if (!line) + return (NULL); + ft_memcpy(buffer, buffer + i + 1, 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, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0')); + } + return (line); } -char *get_next_line(int fd) -{ - static char buffer[BUFFER_SIZE + 1] = ""; - char *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); + if (fd < 0 || BUFFER_SIZE <= 0) + return (NULL); + line = get_the_line(fd, buffer); + if (!line) + return (NULL); + return (line); } diff --git a/src/main.c b/src/main.c index d71e907..b0c7a17 100644 --- a/src/main.c +++ b/src/main.c @@ -10,35 +10,32 @@ /* */ /* ************************************************************************** */ -#include #include +#include +#include #include "../libft/libft.h" #include "alcu.h" -int main(int argc, char* argv[]) -{ - if (argc != 2) - { - ft_putstr_fd("ERROR", 2); - return (1); - } +int main(int argc, char *argv[]) { + if (argc != 2) { + ft_putstr_fd("ERROR", 2); + return (1); + } - int fd = open(argv[1], O_RDONLY); + int fd = open(argv[1], O_RDONLY); - if (fd == -1) - { - ft_putstr_fd("ERROR", 2); - return (1); - } + 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); + int size = check_input(fd); + printf("size %d\n", size); + if (size < 0) { + ft_putstr_fd("ERROR", 2); + return (1); + } + return (0); + close(fd); } -