-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_next_line_utils.c
113 lines (102 loc) · 2.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/28 11:43:17 by jaesjeon #+# #+# */
/* Updated: 2022/10/11 23:58:21 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t gft_strlen(const char *s)
{
size_t idx;
idx = 0;
if (s == NULL)
return (0);
while (*s != '\0')
{
idx++;
s++;
}
return (idx);
}
char *gft_strjoin(char const *s1, char const *s2, ssize_t s2_len)
{
size_t s1_len;
char *joined;
size_t idx;
s1_len = gft_strlen(s1);
joined = (char *)malloc(s1_len + s2_len + 1);
if (joined == NULL)
return (NULL);
idx = 0;
while (s1_len-- > 0)
{
joined[idx] = s1[idx];
idx++;
}
if (s1 != NULL)
free((void *)s1);
while (s2_len-- > 0)
joined[idx++] = *s2++;
joined[idx] = '\0';
return (joined);
}
char *gft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
size_t str_len;
size_t idx;
str_len = gft_strlen(s);
if ((str_len - start) < len && str_len > start)
len = str_len - start;
else if (str_len <= start)
len = 0;
sub = (char *)malloc(len + 1);
if (sub == NULL)
return (NULL);
if (start >= str_len)
{
*sub = '\0';
return (sub);
}
idx = 0;
while (s[start] != '\0' && len > 0)
{
sub[idx++] = s[start++];
len--;
}
sub[idx] = '\0';
return (sub);
}
int gft_isinnl(char *cont)
{
int idx;
idx = 0;
if (cont == NULL)
return (-1);
while (cont[idx])
{
if (cont[idx] == '\n')
return (idx);
idx++;
}
return (-1);
}
t_fdlist *gft_lstnew(int fd)
{
t_fdlist *node;
node = (t_fdlist *)malloc(sizeof(t_fdlist));
if (node == NULL)
return (NULL);
node->myfd = fd;
node->cont = NULL;
node->length = 0;
node->nlidx = -1;
node->prev = NULL;
node->next = NULL;
return (node);
}