mirror of
https://github.com/Ladebeze66/getnextline.git
synced 2026-11-09 13:17:08 +01:00
getnextline
This commit is contained in:
BIN
fr.subject.pdf
Normal file
BIN
fr.subject.pdf
Normal file
Binary file not shown.
127
get_next_line.c
Executable file
127
get_next_line.c
Executable file
@@ -0,0 +1,127 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/12 15:39:29 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/04/12 17:40:44 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
char *rec_line_before_nl(const char *s)
|
||||
{
|
||||
char *res;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i] && s[i] != '\n')
|
||||
i++;
|
||||
if (s[i] && s[i] == '\n')
|
||||
i++;
|
||||
res = ft_calloc_gnl(i + 1, sizeof(char));
|
||||
if (!res)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i] && s[i] != '\n')
|
||||
{
|
||||
res[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
if (s[i] == '\n')
|
||||
{
|
||||
res[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
char *rec_after_nl(const char *s)
|
||||
{
|
||||
char *res;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
j = 0;
|
||||
while (s && s[j])
|
||||
j++;
|
||||
i = 0;
|
||||
while (s[i] && s[i] != '\n')
|
||||
i++;
|
||||
if (s[i] && s[i] == '\n')
|
||||
i++;
|
||||
res = ft_calloc_gnl((j - i) + 1, sizeof(char));
|
||||
if (!res)
|
||||
return (NULL);
|
||||
j = 0;
|
||||
while (s[i + j])
|
||||
{
|
||||
res[j] = s[i + j];
|
||||
j++;
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
void ft_read_line(int fd, char **keep, char **tmp)
|
||||
{
|
||||
char *buf;
|
||||
int byte_read;
|
||||
|
||||
buf = malloc(sizeof(char) * (BUFFER_SIZE + 1));
|
||||
if (!buf)
|
||||
return ;
|
||||
byte_read = 1;
|
||||
while (byte_read > 0)
|
||||
{
|
||||
byte_read = read(fd, buf, BUFFER_SIZE);
|
||||
if (byte_read == -1)
|
||||
{
|
||||
ft_free(&buf, keep, tmp);
|
||||
return ;
|
||||
}
|
||||
buf[byte_read] = '\0';
|
||||
*tmp = ft_strdup(*keep);
|
||||
ft_free(keep, 0, 0);
|
||||
*keep = ft_strjoin_gnl(*tmp, buf);
|
||||
ft_free(tmp, 0, 0);
|
||||
if (find_newline(*keep))
|
||||
break ;
|
||||
}
|
||||
ft_free(&buf, 0, 0);
|
||||
}
|
||||
|
||||
char *ft_gnl_recal(char **keep, char **tmp)
|
||||
{
|
||||
char *line;
|
||||
|
||||
*tmp = ft_strdup(*keep);
|
||||
ft_free(keep, 0, 0);
|
||||
*keep = rec_after_nl(*tmp);
|
||||
line = rec_line_before_nl(*tmp);
|
||||
ft_free(tmp, 0, 0);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char *keep = NULL;
|
||||
char *tmp;
|
||||
char *line;
|
||||
|
||||
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||
return (NULL);
|
||||
line = NULL;
|
||||
tmp = NULL;
|
||||
ft_read_line(fd, &keep, &tmp);
|
||||
if (keep != NULL && *keep != '\0')
|
||||
line = ft_gnl_recal(&keep, &tmp);
|
||||
if (!line || *line == '\0')
|
||||
{
|
||||
ft_free(&keep, &line, &tmp);
|
||||
return (NULL);
|
||||
}
|
||||
return (line);
|
||||
}
|
||||
43
get_next_line.h
Executable file
43
get_next_line.h
Executable file
@@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/12 17:41:38 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/04/12 17:45:17 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
|
||||
# include <fcntl.h>
|
||||
# include <stddef.h>
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <stdio.h>
|
||||
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE BUFSIZ
|
||||
# endif
|
||||
|
||||
# if BUFFER_SIZE > 9223372036854775806
|
||||
# undef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 0
|
||||
# endif
|
||||
|
||||
char *get_next_line(int fd);
|
||||
void ft_read_line(int fd, char **keep, char **tmp);
|
||||
char *ft_gnl_recal(char **keep, char **tmp);
|
||||
char *rec_line_before_nl(const char *s);
|
||||
char *rec_after_nl(const char *s);
|
||||
|
||||
int find_newline(const char *s);
|
||||
char *ft_strdup(const char *s1);
|
||||
char *ft_strjoin_gnl(const char *s1, const char *s2);
|
||||
void *ft_calloc_gnl(size_t count, size_t size);
|
||||
void ft_free(char **str, char **str2, char **str3);
|
||||
|
||||
#endif
|
||||
127
get_next_line_bonus.c
Executable file
127
get_next_line_bonus.c
Executable file
@@ -0,0 +1,127 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/12 18:02:03 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/04/12 18:08:04 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line_bonus.h"
|
||||
|
||||
char *rec_line_before_nl(const char *s)
|
||||
{
|
||||
char *res;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i] && s[i] != '\n')
|
||||
i++;
|
||||
if (s[i] && s[i] == '\n')
|
||||
i++;
|
||||
res = ft_calloc_gnl(i + 1, sizeof(char));
|
||||
if (!res)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i] && s[i] != '\n')
|
||||
{
|
||||
res[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
if (s[i] == '\n')
|
||||
{
|
||||
res[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
char *rec_after_nl(const char *s)
|
||||
{
|
||||
char *res;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
j = 0;
|
||||
while (s && s[j])
|
||||
j++;
|
||||
i = 0;
|
||||
while (s[i] && s[i] != '\n')
|
||||
i++;
|
||||
if (s[i] && s[i] == '\n')
|
||||
i++;
|
||||
res = ft_calloc_gnl((j - i) + 1, sizeof(char));
|
||||
if (!res)
|
||||
return (NULL);
|
||||
j = 0;
|
||||
while (s[i + j])
|
||||
{
|
||||
res[j] = s[i + j];
|
||||
j++;
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
void ft_read_line(int fd, char **keep, char **tmp)
|
||||
{
|
||||
char *buf;
|
||||
int byte_read;
|
||||
|
||||
buf = malloc(sizeof(char) * (BUFFER_SIZE + 1));
|
||||
if (!buf)
|
||||
return ;
|
||||
byte_read = 1;
|
||||
while (byte_read > 0)
|
||||
{
|
||||
byte_read = read(fd, buf, BUFFER_SIZE);
|
||||
if (byte_read == -1)
|
||||
{
|
||||
ft_free(&buf, keep, tmp);
|
||||
return ;
|
||||
}
|
||||
buf[byte_read] = '\0';
|
||||
*tmp = ft_strdup(*keep);
|
||||
ft_free(keep, 0, 0);
|
||||
*keep = ft_strjoin_gnl(*tmp, buf);
|
||||
ft_free(tmp, 0, 0);
|
||||
if (find_newline(*keep))
|
||||
break ;
|
||||
}
|
||||
ft_free(&buf, 0, 0);
|
||||
}
|
||||
|
||||
char *ft_gnl_recal(char **keep, char **tmp)
|
||||
{
|
||||
char *line;
|
||||
|
||||
*tmp = ft_strdup(*keep);
|
||||
ft_free(keep, 0, 0);
|
||||
*keep = rec_after_nl(*tmp);
|
||||
line = rec_line_before_nl(*tmp);
|
||||
ft_free(tmp, 0, 0);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char *keep[1024];
|
||||
char *tmp;
|
||||
char *line;
|
||||
|
||||
if (fd < 0 || fd >= 1024 || BUFFER_SIZE <= 0)
|
||||
return (NULL);
|
||||
line = NULL;
|
||||
tmp = NULL;
|
||||
ft_read_line(fd, &keep[fd], &tmp);
|
||||
if (keep[fd] != NULL && *keep[fd] != '\0')
|
||||
line = ft_gnl_recal(&keep[fd], &tmp);
|
||||
if (!line || *line == '\0')
|
||||
{
|
||||
ft_free(&keep[fd], &line, &tmp);
|
||||
return (NULL);
|
||||
}
|
||||
return (line);
|
||||
}
|
||||
43
get_next_line_bonus.h
Executable file
43
get_next_line_bonus.h
Executable file
@@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_bonus.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/12 18:05:29 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/04/12 18:05:45 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_BONUS_H
|
||||
# define GET_NEXT_LINE_BONUS_H
|
||||
|
||||
# include <fcntl.h>
|
||||
# include <stddef.h>
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <stdio.h>
|
||||
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE BUFSIZ
|
||||
# endif
|
||||
|
||||
# if BUFFER_SIZE > 9223372036854775806
|
||||
# undef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 0
|
||||
# endif
|
||||
|
||||
char *get_next_line(int fd);
|
||||
void ft_read_line(int fd, char **keep, char **tmp);
|
||||
char *ft_gnl_recal(char **keep, char **tmp);
|
||||
char *rec_line_before_nl(const char *s);
|
||||
char *rec_after_nl(const char *s);
|
||||
|
||||
int find_newline(const char *s);
|
||||
char *ft_strdup(const char *s1);
|
||||
char *ft_strjoin_gnl(const char *s1, const char *s2);
|
||||
void *ft_calloc_gnl(size_t count, size_t size);
|
||||
void ft_free(char **str, char **str2, char **str3);
|
||||
|
||||
#endif
|
||||
115
get_next_line_utils.c
Executable file
115
get_next_line_utils.c
Executable file
@@ -0,0 +1,115 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/12 17:03:41 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/04/12 17:47:26 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
int find_newline(const char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == '\n')
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *ft_strjoin_gnl(char const *s1, char const *s2)
|
||||
{
|
||||
int i;
|
||||
int len;
|
||||
char *new_s;
|
||||
|
||||
len = 0;
|
||||
if (!s1 && !s2)
|
||||
return (NULL);
|
||||
while (s1 && s1[len])
|
||||
len++;
|
||||
i = 0;
|
||||
while (s2 && s2[i])
|
||||
i++;
|
||||
new_s = ft_calloc_gnl(len + i + 1, sizeof(char));
|
||||
if (!new_s)
|
||||
return (NULL);
|
||||
len = -1;
|
||||
while (s1 && s1[++len])
|
||||
new_s[len] = s1[len];
|
||||
i = -1;
|
||||
while (s2 && s2[++i])
|
||||
new_s[len + i] = s2[i];
|
||||
return (new_s);
|
||||
}
|
||||
|
||||
char *ft_strdup(const char *s1)
|
||||
{
|
||||
char *s2;
|
||||
int i;
|
||||
|
||||
if (!s1)
|
||||
return (ft_strdup("\0"));
|
||||
i = 0;
|
||||
while (s1[i])
|
||||
i++;
|
||||
s2 = ft_calloc_gnl(i + 1, sizeof(char));
|
||||
if (!s2)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s1[i])
|
||||
{
|
||||
s2[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
return (s2);
|
||||
}
|
||||
|
||||
void *ft_calloc_gnl(size_t count, size_t size)
|
||||
{
|
||||
void *r;
|
||||
unsigned char *p;
|
||||
size_t total;
|
||||
size_t i;
|
||||
|
||||
total = count * size;
|
||||
r = malloc(total);
|
||||
if (!r)
|
||||
return (NULL);
|
||||
p = (unsigned char *)r;
|
||||
i = 0;
|
||||
while (i < total)
|
||||
{
|
||||
p[i] = '\0';
|
||||
i++;
|
||||
}
|
||||
return (r);
|
||||
}
|
||||
|
||||
void ft_free(char **str, char **str2, char **str3)
|
||||
{
|
||||
if (str && *str)
|
||||
{
|
||||
free(*str);
|
||||
*str = NULL;
|
||||
}
|
||||
if (str2 && *str2)
|
||||
{
|
||||
free(*str2);
|
||||
*str2 = NULL;
|
||||
}
|
||||
if (str3 && *str3)
|
||||
{
|
||||
free(*str3);
|
||||
*str3 = NULL;
|
||||
}
|
||||
}
|
||||
115
get_next_line_utils_bonus.c
Executable file
115
get_next_line_utils_bonus.c
Executable file
@@ -0,0 +1,115 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/12 18:04:48 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/04/12 18:05:03 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line_bonus.h"
|
||||
|
||||
int find_newline(const char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == '\n')
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *ft_strjoin_gnl(char const *s1, char const *s2)
|
||||
{
|
||||
int i;
|
||||
int len;
|
||||
char *new_s;
|
||||
|
||||
len = 0;
|
||||
if (!s1 && !s2)
|
||||
return (NULL);
|
||||
while (s1 && s1[len])
|
||||
len++;
|
||||
i = 0;
|
||||
while (s2 && s2[i])
|
||||
i++;
|
||||
new_s = ft_calloc_gnl(len + i + 1, sizeof(char));
|
||||
if (!new_s)
|
||||
return (NULL);
|
||||
len = -1;
|
||||
while (s1 && s1[++len])
|
||||
new_s[len] = s1[len];
|
||||
i = -1;
|
||||
while (s2 && s2[++i])
|
||||
new_s[len + i] = s2[i];
|
||||
return (new_s);
|
||||
}
|
||||
|
||||
char *ft_strdup(const char *s1)
|
||||
{
|
||||
char *s2;
|
||||
int i;
|
||||
|
||||
if (!s1)
|
||||
return (ft_strdup("\0"));
|
||||
i = 0;
|
||||
while (s1[i])
|
||||
i++;
|
||||
s2 = ft_calloc_gnl(i + 1, sizeof(char));
|
||||
if (!s2)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s1[i])
|
||||
{
|
||||
s2[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
return (s2);
|
||||
}
|
||||
|
||||
void *ft_calloc_gnl(size_t count, size_t size)
|
||||
{
|
||||
void *r;
|
||||
unsigned char *p;
|
||||
size_t total;
|
||||
size_t i;
|
||||
|
||||
total = count * size;
|
||||
r = malloc(total);
|
||||
if (!r)
|
||||
return (NULL);
|
||||
p = (unsigned char *)r;
|
||||
i = 0;
|
||||
while (i < total)
|
||||
{
|
||||
p[i] = '\0';
|
||||
i++;
|
||||
}
|
||||
return (r);
|
||||
}
|
||||
|
||||
void ft_free(char **str, char **str2, char **str3)
|
||||
{
|
||||
if (str && *str)
|
||||
{
|
||||
free(*str);
|
||||
*str = NULL;
|
||||
}
|
||||
if (str2 && *str2)
|
||||
{
|
||||
free(*str2);
|
||||
*str2 = NULL;
|
||||
}
|
||||
if (str3 && *str3)
|
||||
{
|
||||
free(*str3);
|
||||
*str3 = NULL;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user