-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
99 lines (89 loc) · 2.33 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_utils.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.h"
int string_lenght(char *string)
{
int index;
index = 0;
while (string[index])
index++;
return (index);
}
int string_search(char *buffer, char c)
{
if (!buffer)
return (0);
while (*buffer)
{
if (*buffer == c)
return (1);
buffer++;
}
return (0);
}
int search_index(char *buffer, char c)
{
int index;
index = 0;
while (buffer[index])
{
if (buffer[index] == c)
return (index + 1);
index++;
}
return (index);
}
char *string_join(char *line, char *buffer)
{
int index_line;
int index_buff;
const int lenght = string_lenght(line) + search_index(buffer, '\n');
char *new_line;
new_line = malloc(sizeof(char) * (lenght + 1));
if (!new_line)
return (free(line), NULL);
index_line = -1;
while (line[++index_line])
new_line[index_line] = line[index_line];
index_buff = 0;
while (buffer[index_buff] && buffer[index_buff] != '\n')
{
new_line[index_line + index_buff] = buffer[index_buff];
index_buff++;
}
if (buffer[index_buff] == '\n')
new_line[index_line + index_buff++] = '\n';
new_line[index_line + index_buff] = '\0';
return (free(line), new_line);
}
void clean_buffer(char *buffer)
{
int index;
int index_reset;
index = 0;
index_reset = 0;
while (buffer[index] != '\n' && buffer[index])
index++;
if (buffer[index] == '\0')
{
buffer[0] = '\0';
return ;
}
index++;
while (buffer[index])
{
buffer[index_reset] = buffer[index];
index++;
index_reset++;
}
buffer[index_reset] = '\0';
}