So long...

This commit is contained in:
Angel Ortigosa Perez
2025-09-07 09:58:53 +02:00
commit 805edbf130
87 changed files with 11865 additions and 0 deletions

112
get_next_line/get_next_line.c Executable file
View File

@@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 12:07:52 by aortigos #+# #+# */
/* Updated: 2025/02/15 09:56:00 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_free(char *buffer, char *buf)
{
char *temp;
temp = ft_strjoin(buffer, buf);
free(buffer);
return (temp);
}
char *ft_get_line(char *save)
{
int i;
char *new;
i = 0;
if (!save[i])
return (NULL);
while (save[i] && save[i] != '\n')
i++;
new = ft_calloc(sizeof(char), i + 2);
if (new == NULL)
return (NULL);
i = 0;
while (save[i] && save[i] != '\n')
{
new[i] = save[i];
i++;
}
if (save[i] && save[i] == '\n')
new[i++] = '\n';
return (new);
}
char *ft_next(char *save)
{
int i;
int j;
char *line;
i = 0;
while (save[i] && save[i] != '\n')
i++;
if (!save[i])
{
free(save);
return (NULL);
}
line = ft_calloc((ft_strlen(save) - i + 1), sizeof(char));
i++;
j = 0;
while (save[i])
line[j++] = save[i++];
free(save);
return (line);
}
char *read_file(int fd, char *save)
{
int bytes_read;
char *buffer;
if (!save)
save = ft_calloc(1, 1);
buffer = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (buffer == NULL)
return (NULL);
bytes_read = 1;
while (bytes_read > 0)
{
bytes_read = read(fd, buffer, BUFFER_SIZE);
if (bytes_read == -1)
{
free(buffer);
return (NULL);
}
buffer[bytes_read] = 0;
save = ft_free(save, buffer);
if (ft_strchr(buffer, '\n'))
break ;
}
free(buffer);
return (save);
}
char *get_next_line(int fd)
{
static char *save;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0)
return (0);
save = read_file(fd, save);
if (save == NULL)
return (NULL);
line = ft_get_line(save);
save = ft_next(save);
return (line);
}

31
get_next_line/get_next_line.h Executable file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 12:07:56 by aortigos #+# #+# */
/* Updated: 2025/02/15 09:56:03 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 1
# endif
# include <stdlib.h>
# include <fcntl.h>
# include <unistd.h>
char *get_next_line(int fd);
size_t ft_strlen(const char *s);
void *ft_calloc(size_t n, size_t size);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strchr(const char *s, int c);
void ft_bzero(void *s, size_t n);
#endif

View File

@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 12:07:48 by aortigos #+# #+# */
/* Updated: 2025/02/15 09:55:55 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void ft_bzero(void *s, size_t n)
{
char *str;
size_t i;
str = (char *)s;
i = 0;
while (i < n)
{
str[i] = '\0';
i++;
}
}
void *ft_calloc(size_t n, size_t size)
{
char *ptr;
ptr = malloc(n * size);
if (!ptr)
return (NULL);
ft_bzero(ptr, n * size);
return (ptr);
}
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
i++;
return (i);
}
char *ft_strchr(const char *s, int c)
{
char *str;
str = (char *)s;
while (*str != c && *str != 0)
str++;
if (*str == c)
return (str);
else
return (NULL);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *new;
int i;
int len1;
int len2;
len1 = ft_strlen(s1);
len2 = ft_strlen(s2);
new = (char *)malloc(sizeof(char) * (len1 + len2 + 1));
if (!new || !s1 || !s2)
return (NULL);
i = 0;
while (s1[i] != 0)
{
new[i] = s1[i];
i++;
}
i = 0;
while (s2[i] != 0)
{
new[len1 + i] = s2[i];
i++;
}
new[len1 + len2] = '\0';
return (new);
}