-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
99 lines (90 loc) · 2.26 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mimarque <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/14 15:07:00 by mimarque #+# #+# */
/* Updated: 2022/04/05 23:41:35 by mimarque ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/*
Locates the first occurrence of 'c' in the string pointed to by 's'.
The terminating null character is considered to be part of the string,
therefore if 'c' is '\0', locates the terminating '\0'
*/
char *ft_strchr(const char *s, int c)
{
char chr;
chr = (char)c;
if (s == NULL)
return (NULL);
while (*s != '\0')
{
if (*s == chr)
return ((char *)s);
s++;
}
return (NULL);
}
/*
Duplicate string s1 from index start utill index end.
Memory for the new string is obtained with malloc, and can be freed with free.
*/
char *ft_strldup(char *s1, int start, int end)
{
char *ptr;
int i;
i = 0;
ptr = malloc(end - start + 1);
if (!ptr)
return (NULL);
while (start < end)
ptr[i++] = s1[start++];
ptr[i] = 0;
return (ptr);
}
/*
Allocates (with malloc) and returns a new string,
result of the concatenation of s1 and s2
S1 is freed
*/
char *ft_strjoinfree(char *s1, char *s2)
{
int i;
char *str;
char *hld;
size_t size;
if (!s1 && !s2)
return (NULL);
i = 0;
size = (ft_strlen(s1) + ft_strlen(s2) + 1);
str = malloc(sizeof(char) * size);
if (!str)
return (NULL);
hld = s1;
if (s1)
while (*s1)
str[i++] = *s1++;
if (s2)
while (*s2)
str[i++] = *s2++;
str[i] = '\0';
if (hld)
free(hld);
return (str);
}
size_t ft_strlen(const char *s)
{
size_t i;
if (!s)
return (0);
i = 0;
while (s[i])
{
i++;
}
return (i);
}