Connected parsing to rest of the game

This commit is contained in:
Philippe BLAGOJEVIC
2022-04-23 20:12:13 +02:00
parent fac1f78230
commit f53e66556b
19 changed files with 1010 additions and 23 deletions

65
srcs/gnl/get_next_line.c Normal file
View File

@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/27 12:42:20 by pblagoje #+# #+# */
/* Updated: 2022/04/18 17:01:06 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void ft_free_gnl(char **s)
{
if (*s != NULL)
{
free(*s);
*s = NULL;
}
}
char *ft_strcpy(char *s1, char *s2)
{
int i;
i = 0;
while (s2[i])
{
s1[i] = s2[i];
i++;
}
s1[i] = '\0';
return (s1);
}
int get_next_line(int fd, char **line)
{
int ret;
static char *mem;
char *buf;
ret = 1;
buf = NULL;
if (BUFFER_SIZE <= 0 || !line || read(fd, buf, 0) == -1)
return (-1);
buf = malloc(sizeof(char) * (BUFFER_SIZE + 1));
while (!ft_is_newline(mem, '\n') && ret != 0)
{
ret = read(fd, buf, BUFFER_SIZE);
buf[ret] = '\0';
mem = ft_strjoin_gnl(mem, buf);
}
if (!ret)
{
ft_free_gnl(&mem);
ft_free_gnl(&buf);
return (0);
}
*line = ft_strtrim_gnl(mem);
mem = ft_strchr_gnl(mem, '\n');
ft_free_gnl(&buf);
return (1);
}

30
srcs/gnl/get_next_line.h Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/27 12:43:50 by pblagoje #+# #+# */
/* Updated: 2022/04/18 17:01:22 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# define BUFFER_SIZE 100
# include <unistd.h>
# include <stdlib.h>
# include <stdio.h>
int get_next_line(int fd, char **line);
int ft_strlen_gnl(char *s);
char *ft_strchr_gnl(char *s, int c);
char *ft_strjoin_gnl(char *s1, char *s2);
char *ft_strtrim_gnl(char *s);
int ft_is_newline(char *s, int c);
char *ft_strcpy(char *s1, char *s2);
void ft_free_gnl(char **s);
#endif

View File

@@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/04 10:35:41 by pblagoje #+# #+# */
/* Updated: 2022/04/18 17:00:41 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int ft_strlen_gnl(char *s)
{
int i;
if (!s)
return (0);
i = 0;
while (s[i])
i++;
return (i);
}
char *ft_strjoin_gnl(char *s1, char *s2)
{
char *res;
int i;
int j;
if (!s1 && !s2)
return (NULL);
i = 0;
j = 0;
res = malloc(sizeof(char) * (ft_strlen_gnl(s1) + ft_strlen_gnl(s2) + 1));
if (!res)
return (NULL);
while (s1 != NULL && s1[i])
{
res[i] = s1[i];
i++;
}
while (s2[j])
{
res[i + j] = s2[j];
j++;
}
res[i + j] = '\0';
ft_free_gnl(&s1);
return (res);
}
int ft_is_newline(char *s, int c)
{
int i;
if (!s)
return (0);
i = 0;
while (s[i])
{
if (s[i] == (unsigned char)c)
return (1);
i++;
}
return (0);
}
char *ft_strchr_gnl(char *s, int c)
{
int i;
int is_newline;
char *tmp;
if (!s)
return (NULL);
i = 0;
is_newline = 0;
while (s[i] && is_newline == 0)
{
if (s[i] == (unsigned char)c)
is_newline = 1;
i++;
}
tmp = malloc(sizeof(char) * (ft_strlen_gnl(&s[i]) + 1));
if (!tmp)
return (NULL);
tmp = ft_strcpy(tmp, &s[i]);
ft_free_gnl(&s);
return (tmp);
}
char *ft_strtrim_gnl(char *s)
{
char *res;
int size;
int i;
size = 0;
i = 0;
while (s[size] != '\n')
size++;
res = malloc(sizeof(char) * (size + 1));
if (!res)
return (NULL);
while (s[i] != '\n')
{
res[i] = s[i];
i++;
}
res[i] = '\0';
return (res);
}