-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
96 lines (86 loc) · 2.1 KB
/
get_next_line_utils_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yorlians <yorlians@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/07 17:23:05 by yorlians #+# #+# */
/* Updated: 2023/03/16 15:55:55 by yorlians ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t copy_string(char *dst, const char *src, size_t dstsize)
{
size_t i;
if (dstsize == 0)
return (length_of_string(src));
i = 0;
while (i < dstsize - 1 && src[i])
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
return (length_of_string(src));
}
void *gnl_calloc(size_t count, size_t size)
{
size_t i;
char *ptr;
i = 0;
ptr = malloc(count * size);
if (!ptr)
return (ptr);
while (i < count * size)
{
ptr[i] = '\0';
i++;
}
return (ptr);
}
int length_of_string(const char *string)
{
int i;
i = 0;
while (string[i] != '\0')
{
i++;
}
return (i);
}
char *search_for_nl(char *line)
{
if (!line)
return (line);
while (*line && *line != '\n')
line++;
if (*line == '\n')
return (line + 1);
else
return (NULL);
}
char *join_string(char *line, char *buffer)
{
char *new_line;
int total;
int i;
int length;
i = 0;
length = 0;
total = length_of_string(buffer) + 1;
if (line)
total += length_of_string(line);
new_line = gnl_calloc(total, sizeof(char));
while (line && line[i])
new_line[length++] = line[i++];
i = 0;
while (buffer && buffer[i])
{
new_line[length++] = buffer[i++];
buffer[i - 1] = '\0';
}
new_line[length] = '\0';
free(line);
return (new_line);
}