-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line_utils.c
More file actions
103 lines (90 loc) · 2.39 KB
/
get_next_line_utils.c
File metadata and controls
103 lines (90 loc) · 2.39 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thopgood <thopgood@student.42lisboa.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 19:39:10 by thopgood #+# #+# */
/* Updated: 2024/05/09 19:47:53 by thopgood ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *ft_dealloc(char **ptr)
{
if (ptr == NULL || *ptr == NULL)
return (NULL);
free(*ptr);
*ptr = NULL;
return (NULL);
}
size_t ft_strlen(const char *s)
{
size_t len;
len = 0;
while (*s++)
len++;
return (len);
}
/*
* Returns a pointer to the first occurrence of c in string s incl. '\0'.
* Returns NULL if c is not found.
* *l = also returns null if !s
*/
char *ft_strchr_l(const char *s, int c)
{
size_t i;
size_t str_len;
if (!s)
return (NULL);
i = -1;
str_len = ft_strlen(s);
while (++i < str_len + 1)
if (s[i] == (char)c)
return ((char *)&s[i]);
return (NULL);
}
/*
* String duplication. Allocates memory using malloc for new string and copies
contents of original string incl. null termination.
*/
char *ft_strdup(const char *s)
{
char *dup_s;
size_t i;
dup_s = malloc(sizeof(char) * (ft_strlen(s) + 1));
if (dup_s == NULL)
return (NULL);
i = -1;
while (s[++i])
dup_s[i] = s[i];
dup_s[i] = '\0';
return (dup_s);
}
/*
* Allocates with malloc and returns new string, s1 + s2.
* Returns ptr to new string or NULL if fails.
* *** if !s1, returns dup of s2
*/
char *ft_strjoin_l(char const *s1, char const *s2)
{
char *res;
int i;
int j;
if (s2 == NULL)
return (NULL);
if (s1 == NULL)
return (ft_strdup(s2));
res = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (res == NULL)
return (NULL);
i = -1;
while (s1[++i])
res[i] = s1[i];
j = 0;
while (s2[j])
res[i++] = s2[j++];
res[i] = '\0';
ft_dealloc((char **)&s1);
return (res);
}