aboutsummaryrefslogtreecommitdiff
path: root/src/io/ft_getline.c
blob: 067e66c0177e958073b448ddb4a8690a16f3f0fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_getline.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/01/31 10:39:38 by cacharle          #+#    #+#             */
/*   Updated: 2020/05/12 21:32:50 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

static int		st_read_line(int fd, char **line, char *rest)
{
	int			ret;
	char		*cut;
	static char	buf[FT_GETLINE_BUFFER_SIZE + 1] = {'\0'};

	while ((ret = read(fd, buf, FT_GETLINE_BUFFER_SIZE)) > 0)
	{
		buf[ret] = '\0';
		if ((cut = ft_strchr(buf, '\n')) != NULL)
		{
			ft_strcpy(rest, cut + 1);
			*cut = '\0';
		}
		if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL)
			return (FT_ERROR);
		if (cut != NULL)
			return (FT_LINE);
	}
	if (ret == -1)
		free(line);
	return (ret);
}

/*
** if has rest:
**     if rest has newline:
**         push rest until newline in line, shift rest
**         return LINE_READ
**     else:
**         push rest in line
**
** while can read fd in buf
**     if buf has newline:
**         push buf until newline in line
**         push buf after newline in rest
**         return LINE_READ
**     push buf in line
**
** return FTNL_EOF
*/

int				ft_getline(int fd, char **line)
{
	char		*cut;
	static char	rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{'\0'}};

	if (fd < 0 || fd > OPEN_MAX || line == NULL)
		return (FT_ERROR);
	if (rest[fd][0] == '\0')
	{
		if ((*line = ft_strdup("")) == NULL)
			return (FT_ERROR);
		return (st_read_line(fd, line, rest[fd]));
	}
	if ((cut = ft_strchr(rest[fd], '\n')) != NULL)
	{
		*cut = '\0';
		if ((*line = ft_strdup(rest[fd])) == NULL)
			return (FT_ERROR);
		ft_strmove(rest[fd], cut + 1);
		return (FT_LINE);
	}
	if ((*line = ft_strdup(rest[fd])) == NULL)
		return (FT_ERROR);
	rest[fd][0] = '\0';
	return (st_read_line(fd, line, rest[fd]));
}