push
This commit is contained in:
parent
5ce44645d5
commit
517d8158b5
@ -6,27 +6,15 @@
|
||||
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include "get_next_line.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int ft_strlen(char *s1)
|
||||
{
|
||||
int i;
|
||||
i = 0;
|
||||
if (!s1)
|
||||
return (0);
|
||||
while (s1[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
#include <stdio.h>
|
||||
|
||||
char *ft_strjoin(char *save, char *s2)
|
||||
{
|
||||
@ -58,23 +46,21 @@ char *ft_strjoin(char *save, char *s2)
|
||||
return (dest);
|
||||
}
|
||||
|
||||
static char *up_save(char **save, int fd, int *end)
|
||||
char *up_save(char **save, int fd, int *end)
|
||||
{
|
||||
char *dest;
|
||||
char *delete;
|
||||
int i;
|
||||
|
||||
dest = 0;
|
||||
delete = *save;
|
||||
if (!(dest = (char*)malloc(1 * (1 + BUFFER_SIZE))))
|
||||
if (!(dest = (char*)malloc(1 + BUFFER_SIZE)))
|
||||
{
|
||||
if (*save)
|
||||
free(*save);
|
||||
return (0);
|
||||
}
|
||||
i = read(fd, dest, BUFFER_SIZE);
|
||||
*end = i;
|
||||
dest[i] = 0;
|
||||
*end = read(fd, dest, BUFFER_SIZE);
|
||||
dest[*end] = 0;
|
||||
if (!(*save = ft_strjoin(*save, dest)))
|
||||
{
|
||||
free(dest);
|
||||
@ -92,7 +78,13 @@ int is_line(char *save, int *end)
|
||||
if (save == 0)
|
||||
return (0);
|
||||
if (*end < BUFFER_SIZE)
|
||||
return (1);
|
||||
{
|
||||
while (save[i] && save[i] != '\n')
|
||||
i++;
|
||||
if (save[i])
|
||||
*end = BUFFER_SIZE;
|
||||
}
|
||||
i = 0;
|
||||
while (save[i])
|
||||
{
|
||||
if (save[i] == '\n')
|
||||
@ -112,17 +104,14 @@ char *is_next_line(char **save, int *end, int fd)
|
||||
|
||||
i = 0;
|
||||
delete = *save;
|
||||
if (*save)
|
||||
{
|
||||
while ((*save)[i])
|
||||
while (*save && (*save)[i])
|
||||
i++;
|
||||
i++;
|
||||
if (!(*save = ft_strjoin(*save + i, 0)))
|
||||
if (*save && !(*save = ft_strjoin(*save + i + 1, 0)))
|
||||
{
|
||||
free(delete);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
up_save(save, fd, end);
|
||||
while (!is_line(*save, end))
|
||||
{
|
||||
up_save(save, fd, end);
|
||||
@ -139,8 +128,7 @@ char *is_next_line(char **save, int *end, int fd)
|
||||
|
||||
int get_next_line(int fd, char **line)
|
||||
{
|
||||
int j;
|
||||
int *end;
|
||||
int end;
|
||||
static char **save;
|
||||
|
||||
if (!save)
|
||||
@ -149,19 +137,22 @@ int get_next_line(int fd, char **line)
|
||||
return (-1);
|
||||
*save = 0;
|
||||
}
|
||||
end = &j;
|
||||
j = BUFFER_SIZE;
|
||||
end = BUFFER_SIZE;
|
||||
if (fd < 0 || !line || BUFFER_SIZE < 1)
|
||||
return (-1);
|
||||
is_next_line(save, end, fd);
|
||||
is_next_line(save, &end, fd);
|
||||
if (!(*save))
|
||||
return (-1);
|
||||
*line = ft_strjoin(*save, 0);
|
||||
if (*end < BUFFER_SIZE)
|
||||
if (!(*line))
|
||||
{
|
||||
free(*save);
|
||||
return (0);
|
||||
}
|
||||
if (end < BUFFER_SIZE)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int fd;
|
||||
@ -169,16 +160,17 @@ int main(int argc, char **argv)
|
||||
char *buff;
|
||||
int line;
|
||||
|
||||
printf("DEFINE BUFFER_SIZE=%d\n", BUFFER_SIZE);
|
||||
line = 0;
|
||||
if (argc == 2)
|
||||
{
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
if (argc == 1)
|
||||
|
||||
25
get_next_line.h
Normal file
25
get_next_line.h
Normal 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
|
||||
@ -6,36 +6,19 @@
|
||||
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
char *ft_strjoin(char const *s1, char const *s2)
|
||||
int ft_strlen(char *s1)
|
||||
{
|
||||
char *dest;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
dest = (char*)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
|
||||
if (dest == 0)
|
||||
if (!s1)
|
||||
return (0);
|
||||
while (s1[i])
|
||||
{
|
||||
dest[j] = s1[i];
|
||||
j++;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (s2[i])
|
||||
{
|
||||
dest[j] = s2[i];
|
||||
j++;
|
||||
i++;
|
||||
}
|
||||
dest[j] = 0;
|
||||
return (dest);
|
||||
return (i);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user