-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
110 lines (101 loc) · 2.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: omoudni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/07 22:45:28 by omoudni #+# #+# */
/* Updated: 2021/12/08 21:56:06 by omoudni ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int get_index(char *str, char c)
{
int i;
i = 0;
if (str == NULL)
return (-1);
if (c == '\0')
{
while (str[i])
i++;
return (i);
}
while (str[i])
{
if (str[i] == c)
return (i);
i++;
}
return (-1);
}
char *ft_strndup(char *str, int len)
{
char *ret;
int i;
i = 0;
if (!str)
return (NULL);
ret = malloc(len +1);
if (!ret)
return (NULL);
while (i < len && str[i])
{
ret[i] = str[i];
i++;
}
ret[i] = '\0';
return (ret);
}
char *ft_strjoin(char *str1, char *str2)
{
char *ret;
int i;
int j;
if (!str1 && !str2)
return (NULL);
if (!str1 || !str2)
ret = malloc(sizeof(char)
* (get_index(str1, '\0') + get_index(str2, '\0') + 2));
else
ret = malloc(sizeof(char)
* (get_index(str1, '\0') + get_index(str2, '\0') + 1));
if (!ret)
return (NULL);
i = -1;
j = -1;
while (str1[++i])
ret[i] = str1[i];
while (str2[++j])
ret[i + j] = str2[j];
ret[i + j] = '\0';
free(str1);
return (ret);
}
char *b_buff_to_line(char **b_buff)
{
char *line;
char *tmp;
line = NULL;
if (get_index(*b_buff, '\n') > -1)
{
line = ft_strndup(*b_buff, get_index(*b_buff, '\n') + 1);
tmp = ft_strndup(&(*b_buff)[get_index(*b_buff, '\n') + 1],
get_index(*b_buff, '\0'));
free(*b_buff);
*b_buff = tmp;
return (line);
}
else if (*b_buff)
{
if (*b_buff[0])
line = ft_strndup(*b_buff, get_index(*b_buff, '\0'));
free(*b_buff);
*b_buff = NULL;
}
if (line)
return (line);
else
return (NULL);
}