-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
164 lines (152 loc) · 4.46 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_utils.c :+: :+: */
/* +:+ */
/* By: cwesseli <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/11/07 11:44:03 by cwesseli #+# #+# */
/* Updated: 2022/11/18 17:41:26 by cwesseli ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/*
This function joines the existing staic variable string with the content
of buffer to create a long string to later split up in a part before the
new-line char and a part behind the new-line char.
Memmory is allocated for an empty string. The next step is
to iterate though the exisiting string and then the buffer and copy all
values to the return string.
*/
char *ft_strjoin_gnl(char *passed_line, char *buffer)
{
int i;
int j;
int len_1;
int len_2;
char *joined_str;
len_1 = ft_strlen(passed_line);
len_2 = ft_strlen(buffer);
if (len_1 == 0 && len_2 == 0)
return (NULL);
joined_str = (char *)malloc(sizeof(char) * (len_1 + len_2 + 1));
if (joined_str == NULL)
return (NULL);
j = 0;
i = 0;
while (i < len_1)
joined_str[i++] = passed_line[j++];
j = 0;
while (j < len_2)
joined_str[i++] = buffer[j++];
joined_str[i] = '\0';
return (free(passed_line), joined_str);
}
/*
This function takes the joined string and copies the
the part before the (first)new-line char to a new sting and
returns it.
The first step is to determine end of the string to return. This
is either a new_line character of en null terminiator.
in case of a null terminator the return line is the passed line.
Otherwise the next step is to iterate through the string once
more and copy the values to the return string with some extra
attention for the new line character.
*/
char *get_new_line(char *passed_line)
{
char *new_line;
int i;
i = 0;
while (passed_line[i] != '\n' && passed_line[i])
i++;
if (passed_line[i] != '\n')
return (passed_line);
else
{
new_line = (char *)malloc(sizeof(char) * (i + 2));
if (!new_line)
return (NULL);
}
i = 0;
while (passed_line[i] != '\n' && passed_line[i])
{
new_line[i] = passed_line[i];
i++;
}
new_line[i++] = '\n';
new_line[i] = '\0';
return (new_line);
}
/*
This function takes the joined string and copies and returns
the part after the new-line char if it exists. The return is the
new value of the static variable to use in the next function call.
The first step is to iterate through the joined string to
find either the end of the string or a new-line char.
In case of the end of the string there is no string
left to return and the previously mallocated string is freed
and NULL is returned. Otherwise memmory is allocated for the found
return value based on the total string lenght - the distance
to the new-line char.
The next step is to iterate through the joiend string from the
new-line char till the end of the string and copy the values to
the return string. Finally free the 'old' string as it is now
replaced with the return value.
*/
char *get_new_string(char *passed_line)
{
char *new_str;
int i;
int j;
i = 0;
while (passed_line[i] != '\n' && passed_line[i])
i++;
if (!passed_line[i] || passed_line == NULL)
return (NULL);
new_str = (char *)malloc(sizeof(char) * (ft_strlen(passed_line) - i));
if (!new_str)
{
free(passed_line);
passed_line = NULL;
return (NULL);
}
j = 0;
i += 1;
while (i < ft_strlen(passed_line))
new_str[j++] = passed_line[i++];
new_str[j] = '\0';
free(passed_line);
return (new_str);
}
/*
This is a basic helper function to determine the lenght
of a string till the null terminator. In case of an
empty string it will return '0'.
*/
int ft_strlen(char *str)
{
int i;
if (!str)
return (0);
i = 0;
while (str[i])
i++;
return (i);
}
/*
This is a helper function to determine the presence of
a character in a string.
*/
int ft_findchr(char *str, char c)
{
int i;
if (!str)
return (0);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == c)
return (1);
return (0);
}