This commit is contained in:
minimonster 2022-10-26 00:44:33 +02:00
commit 375ca0d803
6 changed files with 202 additions and 0 deletions

BIN
a.out Executable file

Binary file not shown.

90
get_next_line.c Normal file
View File

@ -0,0 +1,90 @@
#include "get_next_line.h"
char *ft_getstash(int fd)
{
char *str;
int readed;
str = ft_calloc(BUFFER_SIZE, sizeof(char));
if (str == NULL)
return (NULL);
readed = read(fd, str, BUFFER_SIZE);
if (readed < 0)
{
free(str);
return (NULL);
}
return (str);
}
ssize_t ft_strchr(char *str, char c)
{
size_t i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0' && c != '\0')
return (-1);
return (i);
}
char *ft_getline(int fd)
{
char *stash;
char *buf;
size_t i;
stash = NULL;
i = 0;
while (ft_strchr(stash + i * BUFFER_SIZE, '\n'))
{
stash = ft_realloc(stash, (i + 1) * BUFFER_SIZE);
if (stash == NULL)
return (NULL);
buf = ft_getstash(fd);
if (buf == NULL)
{
free(stash);
return (NULL);
}
ft_strcat(stash, buf);
free(buf);
if (stash[(i + 1) * BUFFER_SIZE -1] == '\0')
break ;
}
return (stash);
}
char *get_next_line(int fd)
{
static char *stash = NULL;
char *buf1;
char *buf2;
buf1 = NULL;
if (ft_strchr(stash, '\n') == -1)
{
buf1 = ft_getline(fd);
if (buf1 == NULL)
return (NULL);
stash = ft_realloc(stash, ft_strchr(buf1, '\0') + ft_strchr(stash, '\0') + 1);
if (stash == NULL)
return (NULL);
ft_strcat(stash, buf1);
free(buf1);
}
buf1 = ft_strndup(stash, ft_strchr(stash, '\n'));
buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strchr(stash, '\0'));
free(stash);
stash = buf2;
if (stash == NULL || buf1 == NULL)
{
free(stash);
free(buf1);
return (NULL);
}
return (buf1);
}

12
get_next_line.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <stdlib.h>
# include <unistd.h>
# define BUFFER_SIZE 42
void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size);
char *ft_strcat(char *dst, char *src);
char *ft_strndup(char *src, size_t n);
char *get_next_line(int fd);
#endif

80
get_next_line_utils.c Normal file
View File

@ -0,0 +1,80 @@
#include "get_next_line.h"
void *ft_calloc(size_t nmemb, size_t size)
{
char *tab;
size_t i;
if (nmemb == 0 || size * nmemb / nmemb != size)
return (NULL);
tab = malloc(nmemb * size);
if (tab == NULL)
return (NULL);
i = 0;
while (i < size * nmemb)
{
tab[i] = 0;
i++;
}
return ((void *) tab);
}
void *ft_realloc(void *ptr, size_t size)
{
char *tab;
char *in;
size_t i;
in = ptr;
tab = ft_calloc(1, size);
if (tab == NULL)
return (NULL);
if (ptr == NULL)
return (tab);
i = 0;
while (in[i] != 0 && i < size)
{
tab[i] = in[i];
i++;
}
free(ptr);
return (tab);
}
char *ft_strcat(char *dst, char *src)
{
size_t i;
size_t j;
j = 0;
while (dst[j] != '\0')
j++;
i = 0;
if (src != NULL)
{
while (src[i] != '\0')
{
dst[i + j] = src[i];
i++;
}
}
dst[i] = '\0';
return (dst);
}
char *ft_strndup(char *src, size_t n)
{
char *out;
size_t i;
out = ft_calloc(n + 1, sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (src[i] != '\0' && i < n)
{
out[i] = src[i];
i++;
}
return (out);
}

15
main.c Normal file
View File

@ -0,0 +1,15 @@
#include "get_next_line.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd = open("tester", O_RDONLY);
printf("%s\n", get_next_line(fd));
printf("%s\n", get_next_line(fd));
printf("%s\n", get_next_line(fd));
printf("%s\n", get_next_line(fd));
}

5
tester Normal file
View File

@ -0,0 +1,5 @@
test1
test2
test3