-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
120 lines (109 loc) · 2.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acesar-l <acesar-l@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/29 11:44:15 by acesar-l #+# #+# */
/* Updated: 2021/10/29 12:07:11 by acesar-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *gnl_strchr(const char *str, int argument)
{
int i;
i = 0;
while (str[i])
{
if ((unsigned char) str[i] == (unsigned char)argument)
return ((char *) &str[i]);
i++;
}
if (argument == '\0')
return ((char *) &str[i]);
return ((void *) 0);
}
size_t gnl_strlcpy(char *dest, const char *src, size_t destsize)
{
size_t i;
size_t src_length;
i = 0;
src_length = 0;
while (src[src_length])
src_length++;
if (destsize <= 0)
return (src_length);
while (src[i] && i < destsize - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (src_length);
}
char *gnl_strdup(char *src)
{
size_t i;
size_t len;
char *dup;
i = 0;
len = 0;
if (!src)
return (NULL);
while (src[len])
len++;
dup = (char *)malloc(sizeof(char) * (len + 1));
if (!dup)
return (NULL);
while (src[i])
{
dup[i] = src[i];
++i;
}
dup[i] = '\0';
return (dup);
}
char *gnl_substr(char const *s, unsigned int start, size_t len)
{
size_t substr_length;
size_t s_length;
char *substr;
if (!s)
return (NULL);
s_length = 0;
while (s[s_length])
s_length++;
substr_length = s_length - start;
if (s_length > start)
{
if (substr_length > len)
substr = (char *) malloc(sizeof(char) * (len + 1));
else
substr = (char *) malloc(sizeof(char) * (substr_length + 1));
if (!substr)
return (NULL);
gnl_strlcpy(substr, &s[start], len + 1);
}
else
substr = gnl_strdup("");
return (substr);
}
char *gnl_strjoin(const char *s1, const char *s2)
{
char *str;
size_t s1_len;
size_t s2_len;
s1_len = 0;
s2_len = 0;
while (s1[s1_len])
s1_len++;
while (s2[s2_len])
s2_len++;
str = (char *)malloc(sizeof(char) * (s1_len + s2_len + 1));
if (!str)
return (NULL);
gnl_strlcpy(str, s1, s1_len + 1);
gnl_strlcpy(&str[s1_len], s2, s1_len + s2_len + 1);
return (str);
}