FIRST COMMIT

This commit is contained in:
David Gailleton
2025-12-05 17:37:02 +01:00
commit 86c2e68987
16 changed files with 9580 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */
/* Updated: 2025/12/05 14:04:02 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "get_next_char.h"
#include <unistd.h>
static int del_before_char(char buf[BUFFER_SIZE], char c)
{
int nl_i;
int i;
char *temp;
nl_i = index_of_char(buf, BUFFER_SIZE, c);
if (nl_i >= 0)
{
temp = ft_substr(buf, nl_i + 1, BUFFER_SIZE - nl_i);
if (!temp)
return (-1);
ft_bzero(buf, BUFFER_SIZE);
i = 0;
while (temp[i])
{
buf[i] = temp[i];
i++;
}
free(temp);
}
return (1);
}
static char *ft_read_one(char buf[BUFFER_SIZE], int fd, char *str)
{
int temp;
str = ft_strjoin_new(str, buf, BUFFER_SIZE);
if (!str)
return (NULL);
ft_bzero(buf, BUFFER_SIZE);
temp = read(fd, buf, BUFFER_SIZE);
if (temp < 0)
{
free(str);
return (NULL);
}
if ((!str || ft_strlen(str) == 0) && temp == 0)
{
free(str);
return (NULL);
}
if (temp == 0)
return (str);
return (str);
}
static char *extract_all_char(char buf[BUFFER_SIZE], int fd, char *str, int nl_i, char c)
{
int temp;
if (!str)
return (NULL);
while (nl_i < 0)
{
str = ft_read_one(buf, fd, str);
if (!str)
return (NULL);
nl_i = index_of_char(buf, BUFFER_SIZE, c);
if (nl_i < 0 && !ft_strlen(buf))
return (str);
}
str = ft_strjoin_new(str, buf, nl_i);
if (!str)
return (NULL);
temp = del_before_char(buf, c);
if (temp < 0)
{
free(str);
return (NULL);
}
return (str);
}
char *get_next_char(int fd, char c)
{
static char buf[BUFFER_SIZE + 1];
int nl_i;
int temp;
char *str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
nl_i = index_of_char(buf, BUFFER_SIZE, c);
if (nl_i >= 0)
{
str = ft_substr(buf, 0, nl_i + 1);
temp = del_before_char(buf, c);
if (temp < 0)
return (free(str), NULL);
}
else
str = extract_all_char(buf, fd, ft_substr("", 0, 1), nl_i, c);
return (str);
}

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_char.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */
/* Updated: 2025/12/05 16:18:29 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_CHAR_H
# define GET_NEXT_CHAR_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
# include <stdlib.h>
# include "libft.h"
char *get_next_char(int fd, char c);
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
int index_of_char(char *str, int limit, char c);
#endif

View File

@@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_char_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */
/* Updated: 2025/12/05 16:18:14 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "get_next_char.h"
#include <stdint.h>
int index_of_char(char *str, int limit, char c)
{
int i;
if (!str)
return (-1);
i = 0;
while (i < limit && str[i])
{
if (str[i] == c)
return (i);
i++;
}
return (-1);
}
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit)
{
char *str;
size_t i;
size_t j;
str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!str)
return (NULL);
i = 0;
j = 0;
while (s1[i])
{
str[i] = s1[i];
i++;
}
while (j <= limit && s2[j])
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
free((void *) s1);
return (str);
}