-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
138 lines (127 loc) · 2.94 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vportell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/13 14:49:24 by vportell #+# #+# */
/* Updated: 2016/11/29 14:48:05 by vportell ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void free_file(t_file **file, t_file *temp, char **line)
{
int i;
t_file *prev;
t_file *curr;
i = 0;
*line = ft_strdup("");
curr = *file;
prev = *file;
while (curr)
{
if (curr == temp)
break ;
prev = curr;
curr = curr->next;
}
if (prev == curr)
*file = curr->next;
else if (curr->next)
prev->next = curr->next;
else if (!(curr->next))
prev->next = NULL;
ft_strdel(&(temp->str));
free(curr);
}
t_file *create_file(int fd)
{
t_file *file;
file = (t_file *)malloc(sizeof(t_file));
file->fd = fd;
file->s = 0;
file->str = 0;
file->next = NULL;
return (file);
}
t_file *get_file(t_file **file, int fd)
{
t_file *temp;
if (*file)
{
temp = *file;
while (temp->next)
{
if (temp->fd == fd)
break ;
temp = temp->next;
}
if (!(temp->next) && temp->fd != fd)
temp->next = create_file(fd);
}
else
*file = create_file(fd);
temp = *file;
while (temp)
{
if (temp->fd == fd)
break ;
temp = temp->next;
}
return (temp);
}
int read_str(char **str, const int fd, int i)
{
int count;
char *temp;
char *buf;
temp = 0;
if (*str)
return (0);
buf = ft_strnew(BUFF_SIZE);
while ((count = read(fd, buf, BUFF_SIZE)) > -1)
{
if (*str)
{
i = ft_strlen(*str);
temp = ft_strsub(*str, 0, i);
ft_strdel(str);
}
*str = ft_strnew(count + i + 1);
temp ? ft_strcpy(*str, temp) : temp;
ft_strncat(*str, buf, count);
ft_strdel(&temp);
count < 1 ? ft_strdel(&buf) : 0;
if (count < 1)
return (0);
}
return (-1);
}
int get_next_line(const int fd, char **line)
{
int i;
int c;
int error;
t_file *t;
static t_file *file;
c = 0;
t = get_file(&file, fd);
if (fd < 0 || line == NULL || (error = read_str(&(t->str), fd, c)) < 0)
return (-1);
if (t->s < (int)ft_strlen(t->str))
{
i = t->s - 1;
while ((t->str)[++i])
if ((t->str)[i] == '\n' && (c = 1))
break ;
if (i > 0 && (t->str)[i - 1] != 0 && (t->str)[i] == 0)
c = 1;
*line = (!(i - t->s)) ? ft_strdup("") :
ft_strsub(t->str, t->s, i - t->s);
t->s = i + 1;
}
else
free_file(&file, t, line);
return (c);
}