-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
127 lines (117 loc) · 2.09 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
/*
** get_next_line.c for vatout_a in /home/vatout_a/rendu/CPE/CPE_2015_Allum1
**
** Made by Vatoutine
** Login <vatout_a@epitech.net>
**
** Started on Fri Feb 26 17:29:32 2016 Vatoutine
** Last update Sat Feb 27 20:24:58 2016 Vatoutine Artem
*/
#include "get_next_line.h"
#include "my.h"
char *my_fill_buffer(char *buff)
{
int x;
int y;
x = 0;
y = 0;
while ((buff[x] != '\n'))
x++;
x++;
while (buff[x] != '\0')
{
buff[y] = buff[x];
x++;
y++;
}
buff[y] = '\0';
return (buff);
}
int nbr_len(char *str, int a)
{
int i;
int j;
i = 0;
j = 0;
if (a == 1)
{
while (str[i] != '\0')
i++;
return (i);
}
if (a == 2)
{
while (str[i] != '\0')
{
if (str[i] == '\n')
j++;
i++;
}
return (j);
}
return (-1);
}
char *my_realloc(char *buff, char *stock, int size)
{
char *tmp;
int i;
int j;
j = i = 0;
if ((tmp = malloc(size + 1)) == NULL)
return (NULL);
while (i <= size)
tmp[i++] = 0;
i = 0;
if (stock != NULL)
{
while (stock[i] != 0)
{
tmp[i] = stock[i];
i++;
}
free(stock);
}
i = j = 0;
while (tmp[i] != 0)
i++;
while ((buff[j] != 0) && (buff[j] != '\n'))
tmp[i++] = buff[j++];
return (tmp);
}
char *my_switch_buffer(char *buffer, char *stack)
{
if (nbr_len(buffer, 2) <= 0)
return (NULL);
buffer = my_fill_buffer(buffer);
if ((stack = my_realloc(buffer, stack, READ_SIZE)) == NULL)
return (NULL);
if (nbr_len(buffer, 2) < 0)
return (stack);
return (stack);
}
char *get_next_line(const int fd)
{
static char buffer[READ_SIZE + 1];
char *stack;
int inc;
int ret;
stack = NULL;
ret = inc = 0;
stack = my_switch_buffer(buffer, stack);
if (nbr_len(buffer, 2) > 0)
return (stack);
while ((inc = read(fd, buffer, READ_SIZE)) > 0)
{
buffer[inc] = '\0';
ret = ret + inc + READ_SIZE;
stack = my_realloc(buffer, stack, ret);
if (nbr_len(buffer, 2) > 0)
return (stack);
}
if (inc == 0)
return (NULL);
if (buffer[0] != 0)
return (stack);
free(stack);
return (NULL);
}