This commit is contained in:
kinou-p 2020-12-16 13:14:04 +01:00
parent 5ce44645d5
commit 517d8158b5
3 changed files with 76 additions and 76 deletions

View File

@ -6,33 +6,21 @@
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 02:40:53 by apommier #+# #+# */ /* Created: 2020/12/14 02:40:53 by apommier #+# #+# */
/* Updated: 2020/12/16 07:56:59 by apommier ### ########.fr */ /* Updated: 2020/12/16 13:13:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdlib.h> #include "get_next_line.h"
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
int ft_strlen(char *s1) char *ft_strjoin(char *save, char *s2)
{ {
int i; char *dest;
i = 0; int i;
if (!s1) int j;
return (0);
while (s1[i])
i++;
return (i);
}
char *ft_strjoin(char *save, char *s2)
{
char *dest;
int i;
int j;
i = 0; i = 0;
j = 0; j = 0;
@ -58,33 +46,31 @@ char *ft_strjoin(char *save, char *s2)
return (dest); return (dest);
} }
static char *up_save(char **save, int fd, int *end) char *up_save(char **save, int fd, int *end)
{ {
char *dest; char *dest;
char *delete; char *delete;
int i;
dest = 0; dest = 0;
delete = *save; delete = *save;
if (!(dest = (char*)malloc(1 * (1 + BUFFER_SIZE)))) if (!(dest = (char*)malloc(1 + BUFFER_SIZE)))
{ {
if (*save) if (*save)
free(*save); free(*save);
return (0); return (0);
} }
i = read(fd, dest, BUFFER_SIZE); *end = read(fd, dest, BUFFER_SIZE);
*end = i; dest[*end] = 0;
dest[i] = 0; if (!(*save = ft_strjoin(*save, dest)))
if (!(*save = ft_strjoin(*save, dest))) {
{ free(dest);
free(dest); free(delete);
free(delete); return (0);
return (0); }
}
return (*save); return (*save);
} }
int is_line(char *save, int *end) int is_line(char *save, int *end)
{ {
int i; int i;
@ -92,7 +78,13 @@ int is_line(char *save, int *end)
if (save == 0) if (save == 0)
return (0); return (0);
if (*end < BUFFER_SIZE) if (*end < BUFFER_SIZE)
return (1); {
while (save[i] && save[i] != '\n')
i++;
if (save[i])
*end = BUFFER_SIZE;
}
i = 0;
while (save[i]) while (save[i])
{ {
if (save[i] == '\n') if (save[i] == '\n')
@ -105,24 +97,21 @@ int is_line(char *save, int *end)
return (0); return (0);
} }
char *is_next_line(char **save, int *end, int fd) char *is_next_line(char **save, int *end, int fd)
{ {
char *delete; char *delete;
int i; int i;
i = 0; i = 0;
delete = *save; delete = *save;
if (*save) while (*save && (*save)[i])
{
while ((*save)[i])
i++;
i++; i++;
if (!(*save = ft_strjoin(*save + i, 0))) if (*save && !(*save = ft_strjoin(*save + i + 1, 0)))
{ {
free(delete); free(delete);
return (0); return (0);
}
} }
up_save(save, fd, end);
while (!is_line(*save, end)) while (!is_line(*save, end))
{ {
up_save(save, fd, end); up_save(save, fd, end);
@ -137,10 +126,9 @@ char *is_next_line(char **save, int *end, int fd)
} }
int get_next_line(int fd, char **line) int get_next_line(int fd, char **line)
{ {
int j; int end;
int *end;
static char **save; static char **save;
if (!save) if (!save)
@ -149,19 +137,22 @@ int get_next_line(int fd, char **line)
return (-1); return (-1);
*save = 0; *save = 0;
} }
end = &j; end = BUFFER_SIZE;
j = BUFFER_SIZE;
if (fd < 0 || !line || BUFFER_SIZE < 1) if (fd < 0 || !line || BUFFER_SIZE < 1)
return (-1); return (-1);
is_next_line(save, end, fd); is_next_line(save, &end, fd);
if (!(*save)) if (!(*save))
return (-1); return (-1);
*line = ft_strjoin(*save, 0); *line = ft_strjoin(*save, 0);
if (*end < BUFFER_SIZE) if (!(*line))
{
free(*save);
return (0);
}
if (end < BUFFER_SIZE)
return (0); return (0);
return (1); return (1);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int fd; int fd;
@ -169,16 +160,17 @@ int main(int argc, char **argv)
char *buff; char *buff;
int line; int line;
printf("DEFINE BUFFER_SIZE=%d\n", BUFFER_SIZE);
line = 0; line = 0;
if (argc == 2) if (argc == 2)
{ {
fd = open(argv[1], O_RDONLY); fd = open(argv[1], O_RDONLY);
while ((ret = get_next_line(fd, &buff)) > 0) while ((ret = get_next_line(fd, &buff)) > 0)
{ {
printf("[Return: %d] Line ->%d : '%s'\n", ret, ++line, buff); printf("\033[38;5;2m" "[Return: %d] Line ->%d : '%s'\n", ret, ++line, buff);
free(buff); free(buff);
} }
printf("[Return: %d] Line ->%d : %s\n", ret, ++line, buff); printf("\033[38;5;2m" "[Return: %d] Line ->%d : %s\n", ret, ++line, buff);
close(fd); close(fd);
} }
if (argc == 1) if (argc == 1)

25
get_next_line.h Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 05:58:49 by apommier #+# #+# */
/* Updated: 2020/12/16 08:44:39 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
#include <stdlib.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);
char *is_nex_line(char **save, int *end, int fd);
#endif

View File

@ -6,36 +6,19 @@
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 06:05:54 by apommier #+# #+# */ /* Created: 2020/12/14 06:05:54 by apommier #+# #+# */
/* Updated: 2020/12/14 06:06:31 by apommier ### ########.fr */ /* Updated: 2020/12/16 08:36:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line.h" #include "get_next_line.h"
char *ft_strjoin(char const *s1, char const *s2) int ft_strlen(char *s1)
{ {
char *dest; int i;
int i;
int j;
i = 0; i = 0;
j = 0; if (!s1)
dest = (char*)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (dest == 0)
return (0); return (0);
while (s1[i]) while (s1[i])
{
dest[j] = s1[i];
j++;
i++; i++;
} return (i);
i = 0;
while (s2[i])
{
dest[j] = s2[i];
j++;
i++;
}
dest[j] = 0;
return (dest);
} }