-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
94 lines (83 loc) · 2.06 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chris <chris@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 16:01:03 by chris #+# #+# */
/* Updated: 2023/02/01 21:06:45 by chris ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int gnl_strlen(const char *s, char end)
{
int i;
i = 0;
if (s == NULL)
return (0);
while (*(s + i) != end && *(s + i) != '\0')
{
i++;
}
if (end == '\n' && *(s + i) == '\n')
{
return (i + 1);
}
return (i);
}
int line_counter(const char *str)
{
int i;
i = 0;
if (str == NULL)
return (0);
while (*str != '\0')
{
if (*str == '\n')
i++;
str++;
}
return (i);
}
void cpy_len(const char *src, char *dest, int len)
{
int i;
i = 0;
while (i < len)
{
*dest = *src;
dest++;
src++;
i++;
}
*dest = '\0';
}
char *gnl_strdup(const char *s, char end)
{
char *dup;
char *dup_start;
int len;
len = gnl_strlen((char *)s, end);
dup = malloc((len * sizeof(char)) + 1);
if (dup == NULL)
return (NULL);
dup_start = dup;
cpy_len(s, dup, len);
return (dup_start);
}
char *gnl_strjoin(char *s1, char *s2)
{
char *joined;
joined = (char *) malloc((gnl_strlen(s1, '\0') + gnl_strlen(s2, '\0') + 1)
* sizeof(char));
if (joined == NULL)
return (NULL);
if (s1 != NULL)
cpy_len(s1, joined, gnl_strlen(s1, '\0'));
cpy_len(s2, joined + gnl_strlen(s1, '\0'), gnl_strlen(s2, '\0'));
if (s1 != NULL)
free(s1);
free(s2);
return (joined);
}