work
This commit is contained in:
parent
0c29f08378
commit
3ba1886eee
187
get_next_line.c
187
get_next_line.c
@ -3,173 +3,98 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* get_next_line.c :+: :+: :+: */
|
/* get_next_line.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
/* By: kinou <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/14 02:40:53 by apommier #+# #+# */
|
/* Created: 2021/10/23 07:55:17 by kinou #+# #+# */
|
||||||
/* Updated: 2020/12/18 10:05:11 by apommier ### ########.fr */
|
/* Updated: 2021/11/01 10:21:35 by kinou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "get_next_line.h"
|
#include "get_next_line.h"
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
char *ft_strjoin(char *save, char *s2)
|
char *ft_free(char *save, int *end)
|
||||||
|
{
|
||||||
|
if (!*end)
|
||||||
|
{
|
||||||
|
free(save);
|
||||||
|
free(end);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
free(end);
|
||||||
|
return (save);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *set_line(char *line, char *save)
|
||||||
{
|
{
|
||||||
char *dest;
|
|
||||||
int i;
|
int i;
|
||||||
int j;
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
j = 0;
|
line = ft_strjoin(save, 0);
|
||||||
if (!save && !s2)
|
while (line[i] && line[i] != '\n')
|
||||||
return(0);
|
|
||||||
if(!(dest = (char*)malloc(ft_strlen(save) + ft_strlen(s2) + 1)))
|
|
||||||
return (0);
|
|
||||||
while (save && save[i])
|
|
||||||
{
|
|
||||||
dest[j] = save[i];
|
|
||||||
j++;
|
|
||||||
i++;
|
i++;
|
||||||
}
|
if (line[i] == '\n')
|
||||||
i = 0;
|
|
||||||
while (s2 && s2[i])
|
|
||||||
{
|
{
|
||||||
dest[j] = s2[i];
|
while (line[i++])
|
||||||
j++;
|
line[i] = '\0';
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
dest[j] = 0;
|
return (line);
|
||||||
return (dest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *up_save(char **save, int fd, int *end)
|
char *set_save(char *save)
|
||||||
{
|
{
|
||||||
char *dest;
|
|
||||||
char *delete;
|
char *delete;
|
||||||
|
|
||||||
dest = 0;
|
|
||||||
delete = *save;
|
|
||||||
if (!(dest = (char*)malloc(1 + BUFFER_SIZE)))
|
|
||||||
{
|
|
||||||
if (*save)
|
|
||||||
free(*save);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*end = read(fd, dest, BUFFER_SIZE);
|
|
||||||
if (*end == -1)
|
|
||||||
return (0);
|
|
||||||
dest[*end] = 0;
|
|
||||||
if (!(*save = ft_strjoin(*save, dest)))
|
|
||||||
{
|
|
||||||
free(dest);
|
|
||||||
free(delete);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
free(delete);
|
|
||||||
free(dest);
|
|
||||||
return (*save);
|
|
||||||
}
|
|
||||||
|
|
||||||
int is_line(char *save, int *end)
|
|
||||||
{
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (save == 0)
|
delete = save;
|
||||||
return (1);
|
|
||||||
if (*end < BUFFER_SIZE)
|
|
||||||
{
|
|
||||||
while (save[i] && save[i] != '\n')
|
while (save[i] && save[i] != '\n')
|
||||||
i++;
|
i++;
|
||||||
if (save[i] == '\n')
|
if (save[i] != '\n')
|
||||||
*end = BUFFER_SIZE;
|
|
||||||
}
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (save[i])
|
save = ft_strjoin((save + i + 1), 0);
|
||||||
{
|
free(delete);
|
||||||
if (save[i] == '\n')
|
return (save);
|
||||||
{
|
|
||||||
save[i] = 0;
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (*end < BUFFER_SIZE)
|
|
||||||
return (1);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *is_next_line(char **save, int *end, int fd)
|
char *next_line(char *save, int *end, int fd)
|
||||||
{
|
{
|
||||||
char *delete;
|
char *delete;
|
||||||
int i;
|
char *dest;
|
||||||
|
|
||||||
i = 0;
|
while (!ft_strchr(save, '\n') && *end > 0)
|
||||||
delete = *save;
|
|
||||||
while (*save && (*save)[i])
|
|
||||||
i++;
|
|
||||||
if (*save && !(*save = ft_strjoin(*save + i + 1, 0)))
|
|
||||||
{
|
{
|
||||||
|
dest = ft_calloc(1, BUFFER_SIZE + 1);
|
||||||
|
*end = read(fd, dest, BUFFER_SIZE);
|
||||||
|
delete = save;
|
||||||
|
save = ft_strjoin(save, dest);
|
||||||
free(delete);
|
free(delete);
|
||||||
return (0);
|
free(dest);
|
||||||
}
|
}
|
||||||
up_save(save, fd, end);
|
return (save);
|
||||||
while (!is_line(*save, end))
|
|
||||||
{
|
|
||||||
up_save(save, fd, end);
|
|
||||||
if (!(*save))
|
|
||||||
{
|
|
||||||
free(delete);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
free(delete);
|
|
||||||
return (*save);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *get_next_line(int fd)
|
||||||
int get_next_line(int fd, char **line)
|
|
||||||
{
|
{
|
||||||
int end;
|
static char *save = NULL;
|
||||||
static char **save;
|
int *end;
|
||||||
|
char *line;
|
||||||
|
|
||||||
if (!save)
|
line = 0;
|
||||||
|
if (fd < 0 || BUFFER_SIZE < 1)
|
||||||
|
return (0);
|
||||||
|
end = malloc(sizeof(int *));
|
||||||
|
*end = 1;
|
||||||
|
if (save == NULL)
|
||||||
|
save = ft_calloc(1, 1);
|
||||||
|
save = next_line(save, end, fd);
|
||||||
|
line = set_line(line, save);
|
||||||
|
if (ft_strlen(line) > 0)
|
||||||
{
|
{
|
||||||
if(!(save = malloc(sizeof(char*))))
|
save = set_save(save);
|
||||||
return (-1);
|
save = ft_free(save, end);
|
||||||
*save = 0;
|
return (line);
|
||||||
}
|
}
|
||||||
else if (*save == 0)
|
free(line);
|
||||||
{
|
save = ft_free(save, end);
|
||||||
*line = *save;
|
|
||||||
free(*save);
|
|
||||||
free(save);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
end = BUFFER_SIZE;
|
|
||||||
if (fd < 0 || !line || BUFFER_SIZE < 1)
|
|
||||||
{
|
|
||||||
return (-1);
|
|
||||||
free(save);
|
|
||||||
}
|
|
||||||
is_next_line(save, &end, fd);
|
|
||||||
if (!(*save))
|
|
||||||
return (-1);
|
|
||||||
free(*line);
|
|
||||||
*line = ft_strjoin(*save, 0);
|
|
||||||
if (!(*line))
|
|
||||||
{
|
|
||||||
free(*save);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
if (end < BUFFER_SIZE)
|
|
||||||
{
|
|
||||||
free(*save);
|
|
||||||
free(save);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/14 05:58:49 by apommier #+# #+# */
|
/* Created: 2020/12/14 05:58:49 by apommier #+# #+# */
|
||||||
/* Updated: 2020/12/16 08:44:39 by apommier ### ########.fr */
|
/* Updated: 2021/11/02 18:16:14 by kinou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -15,11 +15,11 @@
|
|||||||
|
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
int get_next_line(int fd, char **line);
|
|
||||||
int is_line(char *save, int *end);
|
|
||||||
char *up_save(char **save, int fd, int *end);
|
|
||||||
char *ft_strjoin(char *savee, char *s2);
|
|
||||||
int ft_strlen(char *s1);
|
int ft_strlen(char *s1);
|
||||||
char *is_nex_line(char **save, int *end, int fd);
|
char *get_next_line(int fd);
|
||||||
|
char *ft_strjoin(char *savee, char *s2);
|
||||||
|
char *ft_strchr(const char *s, int c);
|
||||||
|
void *ft_calloc(size_t nmemb, size_t size);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
int ft_strlen(char *s1)
|
int ft_strlen(char *s1)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!s1)
|
if (!s1)
|
||||||
return (0);
|
return (0);
|
||||||
@ -22,3 +23,64 @@ int ft_strlen(char *s1)
|
|||||||
i++;
|
i++;
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *ft_strjoin(char *save, char *s2)
|
||||||
|
{
|
||||||
|
char *dest;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
if (!save && !s2)
|
||||||
|
return (0);
|
||||||
|
dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1);
|
||||||
|
while (save && save[i])
|
||||||
|
{
|
||||||
|
dest[j] = save[i];
|
||||||
|
j++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while (s2 && s2[i])
|
||||||
|
{
|
||||||
|
dest[j] = s2[i];
|
||||||
|
j++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dest[j] = 0;
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_strchr(const char *s, int c)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
if (!s)
|
||||||
|
return ("no s");
|
||||||
|
str = (char *)s;
|
||||||
|
while ((*str != c) && (*str != 0))
|
||||||
|
str++;
|
||||||
|
if (*str == c)
|
||||||
|
return ((char *)str);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *ft_calloc(size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
char *new;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
new = malloc(size * nmemb);
|
||||||
|
if (new)
|
||||||
|
{
|
||||||
|
while (size * nmemb - i)
|
||||||
|
{
|
||||||
|
new[i] = 0;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ((void *)new);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user