-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
executable file
·134 lines (124 loc) · 3.57 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rrouille <rrouille@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/05 14:52:32 by rrouille #+# #+# */
/* Updated: 2022/12/15 18:34:29 by rrouille ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
/**
* @brief Finds the last element in a linked list.
* @return A pointer to the last element in the linked list, or NULL if the
* list is empty.
* @param t_list *lst : A pointer to the first element in the linked list
*/
t_list *ft_lstlast(t_list *lst)
{
if (lst == NULL)
return (NULL);
while (lst->next != NULL)
lst = lst->next;
return (lst);
}
/**
* @brief Trims the content of the last element in a linked list.
* @return A pointer to a new string containing the trimmed content of the
* last element in the list, or NULL if an error occurred.
* @param t_list *last : A pointer to the last element in the linked list.
* @param int i : The index at which to start trimming the last
* element's content.
*/
char *trim_last_list_element_helper(t_list *last, int i)
{
int j;
int len;
char *trimmed_content;
len = 0;
while (last->content[len])
len++;
trimmed_content = malloc(sizeof(char) * (len - i + 1));
if (trimmed_content == NULL)
return (NULL);
j = 0;
while (last->content[i])
trimmed_content[j++] = last->content[i++];
trimmed_content[j] = '\0';
return (trimmed_content);
}
/**
* @brief Determines if a newline character exists in the last element of a
* linked list.
* @return A boolean indicating whether or not a newline character was found
* in the last element of the list.
* @param t_list *stash : A pointer to the first element in the linked list.
*/
bool found_new_line(t_list *stash)
{
int i;
if (stash == NULL)
return (false);
i = 0;
if (stash == NULL)
return (false);
while (stash->next != NULL)
stash = stash->next;
while (stash->content[i])
{
if (stash->content[i] == '\n')
return (true);
i++;
}
return (false);
}
/**
* @brief Generates a string from the content of a linked list.
* @return Nothing needs to be returned.
* @param char **line : A double pointer to a character that will be used
* to store the generated string.
* @param t_list *stash : A pointer to the first element in the linked list.
*/
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));
}
/**
* @brief Frees all memory occupied by the given linked list and its
* elements.
* @return Nothing needs to be returned.
* @param t_list *stash : Pointer to the head of the linked list to free.
*/
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;
}
}