working check_input

This commit is contained in:
2026-03-28 16:04:39 +01:00
parent d918306a68
commit e55cc7624f
5 changed files with 128 additions and 295 deletions
Executable
BIN
View File
Binary file not shown.
-90
View File
@@ -1,90 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 13:01:37 by dgaillet #+# #+# */
/* Updated: 2026/03/28 13:01:38 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#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);
}
+17 -79
View File
@@ -14,88 +14,26 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "../inc/get_next_line.h"
#include "../libft/libft.h" #include "../libft/libft.h"
#include "get_next_line.h"
// char *str_append(char *str, char *to_append) { int check_input(int fd) {
// 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; int i;
int j;
char *line = NULL;
i = 0; line = get_next_line(fd);
while (tab[i]) { j = 0;
free(tab[i]); while (line != NULL) {
i++; 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); return (j);
}
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);
} }
+91 -103
View File
@@ -13,125 +13,113 @@
#include "get_next_line.h" #include "get_next_line.h"
#include "../libft/libft.h" #include "../libft/libft.h"
size_t ft_strlen_with_c(char *str, char c) size_t ft_strlen_with_c(char *str, char c) {
{ size_t i;
size_t i;
i = 0; i = 0;
if (!str) if (!str)
return (0); return (0);
while (str[i] && str[i] != c) while (str[i] && str[i] != c)
i++; i++;
return (i); return (i);
} }
size_t detect_newline(char *str) size_t detect_newline(char *str) {
{ size_t i;
size_t i;
i = 0; i = 0;
if (!str) if (!str)
return (0); return (0);
while (str[i]) while (str[i]) {
{ if (str[i] == '\n')
if (str[i] == '\n') return (1);
return (1); i++;
i++; }
} return (0);
return (0);
} }
static void fill_line(char *line, char *c_remaining, char *src, size_t *index) static void fill_line(char *line, char *c_remaining, char *src, size_t *index) {
{ size_t i;
size_t i; size_t j;
size_t j;
i = 0; i = 0;
j = 0; j = 0;
while (c_remaining && c_remaining[i]) while (c_remaining && c_remaining[i]) {
{ line[i] = c_remaining[i];
line[i] = c_remaining[i]; i++;
i++; }
} while (src[j] && src[j] != '\n') {
while (src[j] && src[j] != '\n') line[i + j] = src[j];
{ src[j] = 0;
line[i + j] = src[j]; j++;
src[j] = 0; }
j++; if (src[j] == '\n')
} line[i + j] = '\n';
if (src[j] == '\n') *index = j;
line[i + j] = '\n';
*index = j;
} }
char *build_line(char *src, char *c_remaining, size_t *index) char *build_line(char *src, char *c_remaining, size_t *index) {
{ char *line;
char *line; size_t size;
size_t size; size_t is_jump;
size_t is_jump;
is_jump = detect_newline(src); is_jump = detect_newline(src);
size = ft_strlen_with_c(src, '\n') + ft_strlen_with_c(c_remaining, '\0') + is_jump + 1; size = ft_strlen_with_c(src, '\n') + ft_strlen_with_c(c_remaining, '\0') +
line = ft_calloc(size, sizeof(char)); is_jump + 1;
if (!line) line = ft_calloc(size, sizeof(char));
{ if (!line) {
free(c_remaining); free(c_remaining);
return (NULL); return (NULL);
} }
if (!src) if (!src)
return (NULL); return (NULL);
fill_line(line, c_remaining, src, index); fill_line(line, c_remaining, src, index);
if (c_remaining) if (c_remaining)
free(c_remaining); free(c_remaining);
return (line); return (line);
} }
static void read_file(int fd, char *buffer, ssize_t *nb_read) static void read_file(int fd, char *buffer, ssize_t *nb_read) {
{ *nb_read = read(fd, buffer, BUFFER_SIZE);
*nb_read = read(fd, buffer, BUFFER_SIZE); if (*nb_read < 0)
if (*nb_read < 0) return;
return ; buffer[*nb_read] = '\0';
buffer[*nb_read] = '\0';
} }
static char *get_the_line(int fd, char *buffer) static char *get_the_line(int fd, char *buffer) {
{ char *line;
char *line; size_t i;
size_t i; ssize_t nb_read;
ssize_t nb_read;
i = 0; i = 0;
nb_read = 0; nb_read = 0;
line = NULL; line = NULL;
if (buffer[0] != '\0') if (buffer[0] != '\0') {
{ line = build_line(buffer, NULL, &i);
line = build_line(buffer, NULL, &i); if (!line)
if (!line) return (NULL);
return (NULL); ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0'));
ft_memcpy(buffer + i + 1, buffer, ft_strlen_with_c(buffer + i, '\0')); }
} while (detect_newline(line) == 0) {
while (detect_newline(line) != 0) read_file(fd, buffer, &nb_read);
{ if (nb_read < 0)
read_file(fd, buffer, &nb_read); return (free(line), NULL);
if (nb_read < 0) if (nb_read == 0)
return (free(line), NULL); break;
if (nb_read == 0) line = build_line(buffer, line, &i);
break ; ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0'));
line = build_line(buffer, line, &i); }
ft_memcpy(buffer + i + 1, buffer, ft_strlen_with_c(buffer + i, '\0')); return (line);
}
return (line);
} }
char *get_next_line(int fd) char *get_next_line(int fd) {
{ static char buffer[BUFFER_SIZE + 1] = "";
static char buffer[BUFFER_SIZE + 1] = ""; char *line;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0) if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL); return (NULL);
line = get_the_line(fd, buffer); line = get_the_line(fd, buffer);
if (!line) if (!line)
return (NULL); return (NULL);
return (line); return (line);
} }
+20 -23
View File
@@ -10,35 +10,32 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include "../libft/libft.h" #include "../libft/libft.h"
#include "alcu.h" #include "alcu.h"
int main(int argc, char* argv[]) int main(int argc, char *argv[]) {
{ if (argc != 2) {
if (argc != 2) ft_putstr_fd("ERROR", 2);
{ return (1);
ft_putstr_fd("ERROR", 2); }
return (1);
}
int fd = open(argv[1], O_RDONLY); int fd = open(argv[1], O_RDONLY);
if (fd == -1) if (fd == -1) {
{ ft_putstr_fd("ERROR", 2);
ft_putstr_fd("ERROR", 2); return (1);
return (1); }
}
int size = check_input(fd); int size = check_input(fd);
printf("size %d\n", size); printf("size %d\n", size);
if (size < 0) if (size < 0) {
{ ft_putstr_fd("ERROR", 2);
ft_putstr_fd("ERROR", 2); return (1);
return (1); }
} return (0);
return (0); close(fd);
} }