-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
130 lines (119 loc) · 2.79 KB
/
get_next_line_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: meltremb <meltremb@student.42quebec> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 14:39:25 by meltremb #+# #+# */
/* Updated: 2022/05/05 12:13:32 by meltremb ### ########.fr */
/* */
/* ************************************************************************** */
#include"get_next_line.h"
/* for the other functions mostly */
size_t ft_strlen(char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
/* points the first instance of \n in the doc */
char *ft_strchr(char *s, int endline)
{
int i;
i = 0;
if (!s)
return (0);
if (endline == '\0')
return ((char *)&s[ft_strlen(s)]);
while (s[i] != '\0')
{
if (s[i] == (char)endline)
return ((char *)&s[i]);
i++;
}
return (0);
}
/* joins the doc with a string of the right length for
* the buffer and returns a copy */
char *ft_strjoin(char *doc_str, char *bufferlen)
{
char *str;
size_t i;
size_t j;
if (!doc_str)
{
doc_str = malloc(1 * sizeof(char));
doc_str[0] = '\0';
}
if (!doc_str || !bufferlen)
return (NULL);
str = malloc(sizeof(char) * ((ft_strlen(doc_str) + ft_strlen(bufferlen))
+ 1));
if (!str)
return (NULL);
i = -1;
j = 0;
if (doc_str)
while (doc_str[++i] != '\0')
str[i] = doc_str[i];
while (bufferlen[j] != '\0')
str[i++] = bufferlen[j++];
str[ft_strlen(doc_str) + ft_strlen(bufferlen)] = '\0';
free(doc_str);
return (str);
}
/* deletes the first line of the document to get the next one after */
char *delete_first_line(char *doc)
{
int i;
int j;
char *deldoc;
i = 0;
while (doc[i] && doc[i] != '\n')
i++;
if (!doc[i])
{
free(doc);
return (NULL);
}
deldoc = malloc(sizeof(char) * (ft_strlen(doc) - i + 1));
if (!deldoc)
return (NULL);
i++;
j = 0;
while (doc[i])
deldoc[j++] = doc[i++];
deldoc[j] = '\0';
free(doc);
return (deldoc);
}
/* to get the first line up until \n or \0 in the doc */
char *get_line(char *doc)
{
char *line;
int i;
i = 0;
if (!*doc)
return (NULL);
while (doc[i] != '\n' && doc[i])
i++;
line = malloc(sizeof(char) * (i + 1 + 1));
if (!line)
return (NULL);
i = 0;
while (doc[i] != '\n' && doc[i])
{
line[i] = doc[i];
i++;
}
if (doc[i] == '\n')
{
line[i] = doc[i];
i++;
}
line[i] = '\0';
return (line);
}