-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_next_line_utils_bonus.c
147 lines (135 loc) · 3.3 KB
/
get_next_line_utils_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajordan- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/01 13:24:32 by ajordan- #+# #+# */
/* Updated: 2021/10/20 10:06:30 by ajordan- ### ########.fr */
/* */
/* ************************************************************************** */
/*
* ---------
* GET_LINE
* ---------
* Extracts the line (ending in either line break and `\0` or only `\0` in EOF)
* from static variable.
* PARAMETERS
* #1. The pointer to the cumulative static variable from previous runs of get_next_line.
* RETURN VALUES
* The string with the full line ending in a line break (`\n`) + (`\0`).
* -------------
* NEW_LEFT_STR
* -------------
* Stores in the cumulative static variable the new updated variable with whatever
* is left from the original, minus the line extracted.
* PARAMETERS
* #1. The pointer to the cumulative static variable from previous runs of get_next_line.
* RETURN VALUES
* The new updated string with whatever is left from the original static, minus the
* line extracted.
*/
#include "get_next_line_bonus.h"
size_t ft_strlen(char *s)
{
size_t i;
i = 0;
if (!s)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
char *ft_strchr(char *s, int c)
{
int i;
i = 0;
if (!s)
return (0);
if (c == '\0')
return ((char *)&s[ft_strlen(s)]);
while (s[i] != '\0')
{
if (s[i] == (char) c)
return ((char *)&s[i]);
i++;
}
return (0);
}
char *ft_strjoin(char *left_str, char *buff)
{
size_t i;
size_t j;
char *str;
if (!left_str)
{
left_str = (char *)malloc(1 * sizeof(char));
left_str[0] = '\0';
}
if (!left_str || !buff)
return (NULL);
str = malloc(sizeof(char) * ((ft_strlen(left_str) + ft_strlen(buff)) + 1));
if (str == NULL)
return (NULL);
i = -1;
j = 0;
if (left_str)
while (left_str[++i] != '\0')
str[i] = left_str[i];
while (buff[j] != '\0')
str[i++] = buff[j++];
str[ft_strlen(left_str) + ft_strlen(buff)] = '\0';
free(left_str);
return (str);
}
char *ft_get_line(char *left_str)
{
int i;
char *str;
i = 0;
if (!left_str[i])
return (NULL);
while (left_str[i] && left_str[i] != '\n')
i++;
str = (char *)malloc(sizeof(char) * (i + 2));
if (!str)
return (NULL);
i = 0;
while (left_str[i] && left_str[i] != '\n')
{
str[i] = left_str[i];
i++;
}
if (left_str[i] == '\n')
{
str[i] = left_str[i];
i++;
}
str[i] = '\0';
return (str);
}
char *ft_new_left_str(char *left_str)
{
int i;
int j;
char *str;
i = 0;
while (left_str[i] && left_str[i] != '\n')
i++;
if (!left_str[i])
{
free(left_str);
return (NULL);
}
str = (char *)malloc(sizeof(char) * (ft_strlen(left_str) - i + 1));
if (!str)
return (NULL);
i++;
j = 0;
while (left_str[i])
str[j++] = left_str[i++];
str[j] = '\0';
free(left_str);
return (str);
}