-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
120 lines (108 loc) · 2.54 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: camunozg <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/09 14:12:41 by cmunoz-g #+# #+# */
/* Updated: 2024/09/26 14:25:34 by camunozg ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/**
* Utility functions for string manipulation used in file operations.
* Includes implementations for string concatenation, joining, searching,
* length calculation, and duplication. These functions facilitate the handling
* of dynamic strings in other file processing logic.
*/
size_t ft_strlcat(char *dest, const char *src, size_t destsize)
{
size_t i;
size_t j;
size_t destlen;
size_t srclen;
srclen = ft_strlen(src);
destlen = ft_strlen(dest);
j = destlen;
i = 0;
if (destsize <= destlen)
return (srclen + destsize);
else
{
while (src[i] && j < (destsize - 1))
dest[j++] = src[i++];
dest[j] = '\0';
return (srclen + destlen);
}
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *s3;
size_t s3size;
size_t i;
size_t size;
s3size = ft_strlen(s1) + ft_strlen(s2) + 1;
s3 = (char *)malloc(s3size);
if (s3 == NULL)
return (NULL);
size = ft_strlen(s1);
i = 0;
if (size > 0)
{
while (s1[i] && i < size)
{
s3[i] = s1[i];
i++;
}
s3[i] = '\0';
}
ft_strlcat(s3, s2, s3size);
free((void *)s1);
return (s3);
}
char *ft_strchr(const char *str, int c)
{
char ch;
ch = (char)c;
if (!str)
return (NULL);
while (*str != '\0')
{
if (*str == ch)
return ((char *)str);
str++;
}
if (ch == '\0')
return ((char *)str);
return (0);
}
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
i++;
return (i);
}
char *ft_strdup(const char *s)
{
size_t size;
char *cpy;
size_t i;
size = ft_strlen(s) + 1;
cpy = (char *)malloc(size);
if (!cpy)
return (NULL);
i = 0;
if (size > 0)
{
while (s[i] && i < size)
{
cpy[i] = s[i];
i++;
}
cpy[i] = '\0';
}
return (cpy);
}