-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_next_line_bonus.c
139 lines (127 loc) · 3.48 KB
/
get_next_line_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
135
136
137
138
139
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cjackows <[email protected]. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 16:00:50 by cjackows #+# #+# */
/* Updated: 2022/09/25 22:42:40 by cjackows ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *str);
char *read_file(int fd, char *main_str);
char *return_func(char *main_str);
char *reset(char *main_str);
/**
* @brief Read buffer size from fd (up untill string end) and returns a line.
*
* @param fd file descriptor
* @return char* Read line: correct behavior
* NULL: there is nothing else to read, or an error occurred
*/
char *get_next_line(int fd)
{
static char *main_str[1024];
char *return_str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
main_str[fd] = read_file(fd, main_str[fd]);
if (!main_str[fd])
return (NULL);
return_str = return_func(main_str[fd]);
main_str[fd] = reset(main_str[fd]);
return (return_str);
}
/**
* @brief Calculates lenght of the passed string
*
* @param str
* @return size_t
*/
size_t ft_strlen(const char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
/**
* @brief Reads buffer number of characters from file descriptor and allocates
* a string to store them. Stops at the end of the file.
* @param fd file descriptor
* @param main_str string in which everything gets saved
* @return char* string
*/
char *read_file(int fd, char *main_str)
{
int i;
char *buffer_str;
i = 1;
buffer_str = malloc(BUFFER_SIZE + 1);
if (!buffer_str)
return (NULL);
while (!ft_strchr(main_str, '\n') && i != 0)
{
i = read(fd, buffer_str, BUFFER_SIZE);
if (i == -1)
{
free(buffer_str);
return (NULL);
}
buffer_str[i] = '\0';
main_str = ft_strjoin(main_str, buffer_str);
}
free(buffer_str);
return (main_str);
}
/**
* @brief Function returns everything up to new line or end of the file
* i = chars before new line or end of the file
* substr cuts the line in place of index
* i++ before return is for new line char or null
* null creates buffer overflow but ft_substr() has hardcoded check for that
* @param main_str
* @return char*
*/
char *return_func(char *main_str)
{
char *next_line;
int i;
i = 0;
if (!main_str[i])
return (NULL);
while (main_str[i] && main_str[i] != '\n')
i++;
i++;
next_line = ft_substr(main_str, 0, i);
return (next_line);
}
/**
* @brief Function substract returned line from main_str and returns the rest
* of characters that are left in main_str
* which later on gets reasigned to the main_function if there
* still is something left
*
* @param main_str
* @return char*
*/
char *reset(char *main_str)
{
char *rest;
int i;
i = 0;
while (main_str[i] && main_str[i] != '\n')
i++;
i++;
rest = ft_substr(main_str, i, ft_strlen(main_str));
if (rest[0] == '\0')
{
free(rest);
rest = NULL;
}
free(main_str);
return (rest);
}