-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
115 lines (106 loc) · 2.37 KB
/
get_next_line.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dzui <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 15:31:22 by dzui #+# #+# */
/* Updated: 2017/01/11 16:05:05 by dzui ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <time.h>
void make_string(char **s1, char *s2, int n1, int n2)
{
char *temp;
int i;
int j;
temp = ft_strdup(*s1);
*s1 = ft_strnew(ft_strlen(*s1) + (n2 - n1));
ft_strcpy(*s1, temp);
j = n1;
i = ft_strlen(*s1);
while (j < n2)
{
(*s1)[i] = s2[j];
j++;
i++;
}
free(temp);
}
int find_newl(char *str, int *i)
{
while (str[*i])
{
if (str[*i] == '\n')
return (1);
(*i)++;
}
return (0);
}
void set_var(char *buff, int *i, int *prev, int flag)
{
if (flag == 1)
{
*i = 0;
*prev = *i;
}
else
{
if (buff[*i] == '\n')
{
(*i)++;
*prev = (*i);
}
}
}
int write_line(char **buff, char **line, int *i, const int fd)
{
static int prev;
static int ch_r;
if (!(*buff)[*i])
{
if ((ch_r = read(fd, *buff, BUFF_SIZE)) < 0)
return (-1);
if ((ch_r == 0) && !((*buff)[*i]))
return (0);
set_var(*buff, i, &prev, 1);
}
*line = ft_strnew(1);
while (!find_newl(*buff, i))
{
make_string(line, *buff, prev, *i);
ft_strclr(*buff);
if ((ch_r = read(fd, *buff, BUFF_SIZE)) < 0)
return (-1);
if (ch_r == 0)
break ;
set_var(*buff, i, &prev, 1);
}
make_string(line, *buff, prev, *i);
set_var(*buff, i, &prev, 0);
return (1);
}
int get_next_line(const int fd, char **line)
{
static char *buff;
static int i;
int res;
if (fd < 0 || !(line))
return (-1);
if (!buff)
{
buff = ft_strnew(BUFF_SIZE);
}
res = write_line(&buff, line, &i, fd);
if (res == 1)
{
return (1);
}
if (res == -1)
{
return (-1);
}
return (0);
}