-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line_utils.c
More file actions
122 lines (110 loc) · 2.45 KB
/
get_next_line_utils.c
File metadata and controls
122 lines (110 loc) · 2.45 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_utils.c :+: :+: */
/* +:+ */
/* By: mde-cloe <mde-cloe@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/03/31 19:19:25 by mde-cloe #+# #+# */
/* Updated: 2022/08/02 20:44:36 by mde-cloe ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *buff_to_line(char *buff, char *line)
{
size_t line_len;
size_t buff_end;
char *line2;
line_len = delimlen(line, '\0');
buff_end = delimlen(buff, '\n');
if (!buff_end)
buff_end = delimlen(buff, '\0');
line2 = stralloc(buff_end + line_len + 1);
if (!line2)
return (NULL);
strcopycat(line2, line, buff, buff_end);
free(line);
return (line2);
}
void buff_update(char *buff)
{
size_t i;
size_t nl_len;
size_t bufflen;
i = 0;
nl_len = delimlen(buff, '\n');
bufflen = delimlen(buff, '\0');
if (buff[nl_len] == '\0')
{
while (i < BUFFER_SIZE)
{
buff[i] = '\0';
i++;
}
return ;
}
while (i <= bufflen - nl_len)
{
buff[i] = buff[nl_len + i];
i++;
}
buff[i] = '\0';
}
//wish functions could take 5 args, then i wouldnt need to strlen again
void strcopycat(char *dst, char *cpysrc, char *catsrc, \
size_t catlen)
{
size_t cpylen;
size_t i;
size_t j;
cpylen = delimlen(cpysrc, '\0');
i = 0;
j = 0;
while (i < cpylen)
{
dst[i] = cpysrc[i];
i++;
}
while (j < catlen)
{
dst[i + j] = catsrc[j];
j++;
}
}
/*
Will return len+1 for newlines (and any delim besides \0)
since the newline should be included in the file
*/
size_t delimlen(char *str, char delim)
{
size_t i;
i = 0;
if (delim != '\0')
{
while (str[i])
{
if (str[i] == delim)
return (i + 1);
i++;
}
return (0);
}
while (str[i])
i++;
return (i);
}
char *stralloc(size_t space)
{
char *str;
size_t i;
str = malloc(space * sizeof(char));
i = 0;
if (!str)
return (NULL);
while (i < space)
{
str[i] = '\0';
i++;
}
return (str);
}