-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
99 lines (89 loc) · 2.15 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
97
98
99
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_utils_bonus.c :+: :+: */
/* +:+ */
/* By: cter-maa <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/12/09 11:19:03 by cter-maa #+# #+# */
/* Updated: 2023/03/07 14:01:28 by cter-maa ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
int string_lenght(char *str)
{
int index;
index = 0;
while (str[index])
index++;
return (index);
}
int string_search(char *buff, char c)
{
if (!buff)
return (0);
while (*buff)
{
if (*buff == c)
return (1);
buff++;
}
return (0);
}
int search_index(char *buff, char c)
{
int index;
index = 0;
while (buff[index])
{
if (buff[index] == c)
return (index + 1);
index++;
}
return (index);
}
char *string_join(char *line, char *buff)
{
int index;
int j;
const int len = string_lenght(line) + search_index(buff, '\n');
char *new_line;
new_line = malloc(sizeof(char) * (len + 1));
if (!new_line)
return (free(line), NULL);
index = -1;
while (line[++index])
new_line[index] = line[index];
j = 0;
while (buff[j] && buff[j] != '\n')
{
new_line[index + j] = buff[j];
j++;
}
if (buff[j] == '\n')
new_line[index + j++] = '\n';
new_line[index + j] = '\0';
return (free(line), new_line);
}
void clean_buffer(char *buff)
{
int index;
int index_reset;
index = 0;
index_reset = 0;
while (buff[index] != '\n' && buff[index])
index++;
if (buff[index] == '\0')
{
buff[0] = '\0';
return ;
}
index++;
while (buff[index])
{
buff[index_reset] = buff[index];
index++;
index_reset++;
}
buff[index_reset] = '\0';
}