-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
120 lines (109 loc) · 2.67 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: roaraujo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/17 18:30:10 by roaraujo #+# #+# */
/* Updated: 2021/09/18 10:51:48 by roaraujo ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
/**
* Returns the length of a valid string
*/
{
int qtd_caracteres;
qtd_caracteres = 0;
while (*s)
{
qtd_caracteres++;
s++;
}
return (qtd_caracteres);
}
char *ft_strdup(const char *s)
/**
* duplicates one string into another. allocates memory using malloc.
*/
{
unsigned int strlength;
char *newstr;
unsigned int i;
strlength = ft_strlen(s);
newstr = malloc((strlength + 1) * sizeof(const char));
if (newstr == NULL)
return (NULL);
newstr[strlength] = '\0';
i = 0;
while (i < strlength)
{
((char *) newstr)[i] = ((char *) s)[i];
i++;
}
return (newstr);
}
char *ft_strjoin(char const *s1, char const *s2)
/**
* concatenates two strings of fixed length into one.
* allocates memory using malloc.
*/
{
char *joint;
int i;
int length_s1;
int length_s2;
length_s1 = ft_strlen(s1);
length_s2 = ft_strlen(s2);
joint = malloc((length_s1 + length_s2 + 1) * sizeof(char));
if (joint == NULL)
return (NULL);
i = -1;
while (s1[++i])
joint[i] = s1[i];
i = -1;
while (s2[++i])
joint[i + length_s1] = s2[i];
joint[length_s1 + length_s2] = '\0';
free((char *)s1);
return (joint);
}
size_t ft_strlcpy(char *dst, const char *src, size_t size)
/**
* copies (up to size bytes) the content of one string (src) into another (dst).
* overwrites the contents of dst.
*/
{
unsigned int i;
if (size == 0)
return (ft_strlen(src));
i = 0;
while (src[i] && i < (size - 1))
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
return (ft_strlen(src));
}
int contains_nl(char *string)
/**
* scans a string for single line character (\n).
* if found, returns its position (>= 0).
* otherwise, returns -1.
*/
{
int i;
if (!string)
return (-1);
i = 0;
while (string[i])
{
if (string[i] == '\n')
return (i);
i++;
}
return (-1);
}