mirror of
https://github.com/LucasCodeur/alcu.git
synced 2026-04-28 17:44:34 +02:00
working check_input
This commit is contained in:
@@ -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);
|
||||
}
|
||||
+15
-77
@@ -14,88 +14,26 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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 i;
|
||||
|
||||
i = 0;
|
||||
while (tab[i]) {
|
||||
free(tab[i]);
|
||||
i++;
|
||||
}
|
||||
free(tab);
|
||||
}
|
||||
|
||||
int check_input(int fd)
|
||||
{
|
||||
char **content;
|
||||
int check_input(int fd) {
|
||||
int i;
|
||||
int j;
|
||||
char * line = NULL;
|
||||
line = get_next_line(fd);
|
||||
while (line != NULL)
|
||||
{
|
||||
char *line = NULL;
|
||||
|
||||
}
|
||||
content = ft_split(get_next_line(fd), '\n');
|
||||
if (!content)
|
||||
return (-2);
|
||||
line = get_next_line(fd);
|
||||
j = 0;
|
||||
while (line != NULL) {
|
||||
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);
|
||||
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_split(content);
|
||||
return (i);
|
||||
return (j);
|
||||
}
|
||||
|
||||
+19
-31
@@ -13,8 +13,7 @@
|
||||
#include "get_next_line.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;
|
||||
|
||||
i = 0;
|
||||
@@ -25,15 +24,13 @@ size_t ft_strlen_with_c(char *str, char c)
|
||||
return (i);
|
||||
}
|
||||
|
||||
size_t detect_newline(char *str)
|
||||
{
|
||||
size_t detect_newline(char *str) {
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!str)
|
||||
return (0);
|
||||
while (str[i])
|
||||
{
|
||||
while (str[i]) {
|
||||
if (str[i] == '\n')
|
||||
return (1);
|
||||
i++;
|
||||
@@ -41,20 +38,17 @@ size_t detect_newline(char *str)
|
||||
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 j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (c_remaining && c_remaining[i])
|
||||
{
|
||||
while (c_remaining && c_remaining[i]) {
|
||||
line[i] = c_remaining[i];
|
||||
i++;
|
||||
}
|
||||
while (src[j] && src[j] != '\n')
|
||||
{
|
||||
while (src[j] && src[j] != '\n') {
|
||||
line[i + j] = src[j];
|
||||
src[j] = 0;
|
||||
j++;
|
||||
@@ -64,17 +58,16 @@ static void fill_line(char *line, char *c_remaining, char *src, size_t *index)
|
||||
*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;
|
||||
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;
|
||||
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)
|
||||
{
|
||||
if (!line) {
|
||||
free(c_remaining);
|
||||
return (NULL);
|
||||
}
|
||||
@@ -86,16 +79,14 @@ char *build_line(char *src, char *c_remaining, size_t *index)
|
||||
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);
|
||||
if (*nb_read < 0)
|
||||
return ;
|
||||
return;
|
||||
buffer[*nb_read] = '\0';
|
||||
}
|
||||
|
||||
static char *get_the_line(int fd, char *buffer)
|
||||
{
|
||||
static char *get_the_line(int fd, char *buffer) {
|
||||
char *line;
|
||||
size_t i;
|
||||
ssize_t nb_read;
|
||||
@@ -103,28 +94,25 @@ static char *get_the_line(int fd, char *buffer)
|
||||
i = 0;
|
||||
nb_read = 0;
|
||||
line = NULL;
|
||||
if (buffer[0] != '\0')
|
||||
{
|
||||
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'));
|
||||
ft_memcpy(buffer, buffer + i + 1, 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)
|
||||
return (free(line), NULL);
|
||||
if (nb_read == 0)
|
||||
break ;
|
||||
break;
|
||||
line = build_line(buffer, line, &i);
|
||||
ft_memcpy(buffer + i + 1, buffer, ft_strlen_with_c(buffer + i, '\0'));
|
||||
ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0'));
|
||||
}
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
char *get_next_line(int fd) {
|
||||
static char buffer[BUFFER_SIZE + 1] = "";
|
||||
char *line;
|
||||
|
||||
|
||||
+7
-10
@@ -10,35 +10,32 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../libft/libft.h"
|
||||
#include "alcu.h"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (fd == -1) {
|
||||
ft_putstr_fd("ERROR", 2);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int size = check_input(fd);
|
||||
printf("size %d\n", size);
|
||||
if (size < 0)
|
||||
{
|
||||
if (size < 0) {
|
||||
ft_putstr_fd("ERROR", 2);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user