-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
99 lines (84 loc) · 2.14 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: lgaume <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/29 03:48:09 by lgaume #+# #+# */
/* Updated: 2023/11/01 11:16:58 by lgaume ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/* Looks for a newline character in the given linked list. */
int found_newline(t_list *stash)
{
int i;
t_list *current;
if (!stash)
return (0);
current = ft_lst_get_last(stash);
i = 0;
while (current->content[i])
{
if (current->content[i] == '\n')
return (1);
i++;
}
return (0);
}
/* Returns a pointer to the last element in the stash */
t_list *ft_lst_get_last(t_list *stash)
{
t_list *current;
current = stash;
while (current && current->next)
current = current->next;
return (current);
}
/* Calculates the number of chars in the current line,
* including the trailing \n if there is one, and allocates memoru. */
void generate_line(char **line, t_list *stash)
{
int i;
int len;
len = 0;
while (stash)
{
i = 0;
while (stash->content[i])
{
if (stash->content[i] == '\n')
{
len++;
break ;
}
len++;
i++;
}
stash = stash ->next;
}
*line = malloc(sizeof(char) * (len + 1));
}
/* Frees the entire stash. */
void free_stash(t_list *stash)
{
t_list *current;
t_list *next;
current = stash;
while (current)
{
free(current->content);
next = current->next;
free(current);
current = next;
}
}
int ft_strlen(const char *str)
{
int len;
len = 0;
while (str[len])
len++;
return (len);
}