-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_next_line_utils_bonus.c
178 lines (167 loc) · 4.69 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cjackows <[email protected]. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 16:00:50 by cjackows #+# #+# */
/* Updated: 2022/09/25 22:35:43 by cjackows ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
/**
* @brief function searches for the FIRST occurrence of
* character 'c' in string 's'.If 's' doesnt end with
* NULL program might cras!
* @param s This is the string to be scanned.
* @param c This is the character to be searched in 's'.
* @return char* Returns a pointer to the first
* occurrence of c that is converted to a character in string.
* The function returns NULL if the specified character is not found.
*/
char *ft_strchr(char *str, int c)
{
int i;
i = 0;
if (str)
{
while (str[i])
{
if ((char)c == str[i])
return ((char *)&str[i]);
i++;
}
if ((char)c == str[i])
return ((char *)&str[i]);
}
return (NULL);
}
/**
* @brief Allocates (with malloc) and returns a substring from the string 's'.
* The substring begins at index ’start’ and is of maximum size ’len’.
* @param s The string from which to create the substring.
* @param start The start index of the substring in the string ’s’.
* @param len The maximum length of the substring.
* @return char* to allocated with subtring memory.
**/
char *ft_substr(char *s, unsigned int start, size_t len)
{
char *sub;
size_t i;
i = 0;
if (!s)
return (NULL);
if (s[0] == 0 || ft_strlen(s) <= start)
return (ft_strdup(""));
sub = NULL;
if (len > ft_strlen(s))
sub = malloc(ft_strlen(s) + 1);
else
sub = malloc(len + 1);
if (!sub)
return (NULL);
while (i < len && s[start])
{
sub[i] = s[start];
i++;
start++;
}
sub[i] = 0;
return (sub);
}
/**
* @brief Function duplicates a string and returns a pointer
* pointing to the first byte of copied string. ||
* It tries to allocate (with malloc) enough memory to hold the old string
* (plus a '\0' character to mark the end of the string).
* @param s1 pointer to string that gets duplicated.
* @return char* Return the new address (which the caller is responsible for
* freeing at some point).
* If the allocation failed returns NULL.
*/
char *ft_strdup(char *s1)
{
char *output_str;
int i;
output_str = (char *)malloc(sizeof (char) * ft_strlen(s1) + 1);
i = 0;
if (!output_str)
return (NULL);
while (s1[i])
{
output_str[i] = s1[i];
i++;
}
output_str[i] = '\0';
return (output_str);
}
/**
* @brief Function appends the NUL-terminated string 'src' to the end of 'dst'.
* Takes whole size of destination buffer not only the size
* It garantees null termination (as long size > 0 && dest
* has at least onefree space)
* Function does not check if 'dts' or 'src' is NULL!
* @param dst String where 'src' gets appended to.
* @param src String that gets appended to the end of 'dst'
* @param dstsize How many characters of 'dst' gets appended.
* @return Returns size_t of src + dst (without NUL) OR NULL if fails.
*/
size_t ft_strlcat(char *dst, const char *src, size_t dstsize)
{
size_t ldst;
size_t lsrc;
size_t i;
size_t j;
ldst = ft_strlen(dst);
lsrc = ft_strlen(src);
i = 0;
j = ldst;
if (ldst < dstsize - 1 && dstsize > 0)
{
while (src[i] && (ldst + i < dstsize - 1))
{
dst[j] = src[i];
j++;
i++;
}
dst[j] = 0;
}
if (ldst >= dstsize)
ldst = dstsize;
return (ldst + lsrc);
}
/**
* @brief Allocates (with malloc(3)) and returns a new
* string, which is the result of the concatenation of ’s1’ and ’s2’.
* @param s1 The prefix string.
* @param s2 The suffix string.
* @return Char pointer to the new string.
* NULL if the allocation fails.
*/
char *ft_strjoin(char *s1, char *s2)
{
char *new;
size_t size_s1;
size_t size_s2;
size_t i;
if (s1 == NULL)
{
s1 = (char *)malloc(sizeof(char) * 1);
s1[0] = 0;
}
i = 0;
size_s1 = ft_strlen(s1);
size_s2 = ft_strlen(s2);
new = malloc(size_s1 + size_s2 + 1);
if (!new)
return (NULL);
while (i < size_s1 + 1)
{
((char *)new)[i] = ((char *)s1)[i];
i++;
}
ft_strlcat(new, s2, size_s1 + size_s2 + 1);
free(s1);
return (new);
}