-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line_bonus.c
More file actions
194 lines (170 loc) · 5.26 KB
/
Copy pathget_next_line_bonus.c
File metadata and controls
194 lines (170 loc) · 5.26 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkojima <nkojima@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/03 18:39:04 by nkojima #+# #+# */
/* Updated: 2025/08/03 19:34:07 by nkojima ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
/**
* Extract a line that contains a newline character
*
* @param stored_data Pointer to the stored data
* @param newline_pos Position of the newline character
* @return char* Extracted line (including newline), NULL on error
*/
static char *extract_line_with_newline(char **stored_data, char *newline_pos)
{
char *line;
char *temp;
size_t line_len;
line_len = newline_pos - *stored_data + 1;
line = malloc(line_len + 1);
if (!line)
return (free(*stored_data), *stored_data = NULL, NULL);
ft_memcpy(line, *stored_data, line_len);
line[line_len] = '\0';
temp = *stored_data;
*stored_data = ft_strdup(newline_pos + 1);
if (!*stored_data)
return (free(line), free(temp), *stored_data = NULL, NULL);
free(temp);
return (line);
}
/**
* Extract a line from the stored data
*
* @param stored_data Pointer to the stored data
* @return char* Extracted line, NULL if no data or error
*/
static char *extract_line(char **stored_data)
{
char *newline_pos;
char *line;
if (!*stored_data || **stored_data == '\0')
return (free(*stored_data), *stored_data = NULL, NULL);
newline_pos = ft_strchr(*stored_data, '\n');
if (newline_pos)
return (extract_line_with_newline(stored_data, newline_pos));
line = ft_strdup(*stored_data);
return (free(*stored_data), *stored_data = NULL, line);
}
/**
* Read data from file into buffer and append to stored data
*
* @param fd File descriptor
* @param buffer Buffer for reading
* @param stored_data Pointer to stored data
* @return int Read status (negative: error, 0: newline found or EOF,
positive: continue)
*/
static int handle_buffer_read(int fd, char *buffer, char **stored_data)
{
ssize_t bytes_read;
char *temp;
bytes_read = read(fd, buffer, BUFFER_SIZE);
if (bytes_read <= 0)
return (bytes_read);
buffer[bytes_read] = '\0';
temp = ft_strjoin(*stored_data, buffer);
if (!temp)
return (*stored_data = NULL, -1);
*stored_data = temp;
if (ft_strchr(*stored_data, '\n'))
return (0);
return (1);
}
/**
* Read from file until a newline is found
*
* @param fd File descriptor
* @param stored_data Stored data
* @return char* Read data, NULL on error
*/
static char *read_until_newline(int fd, char *stored_data)
{
char *buffer;
int read_result;
buffer = malloc(sizeof(*buffer) * (BUFFER_SIZE + 1));
if (!buffer)
return (free(stored_data), NULL);
while (1)
{
read_result = handle_buffer_read(fd, buffer, &stored_data);
if (read_result <= 0)
break ;
}
free(buffer);
if (read_result < 0)
return (free(stored_data), NULL);
return (stored_data);
}
/**
* Get the next line from a file
*
* @param fd File descriptor
* @return char* Next line from file (including newline), NULL on error or EOF
*
* This function reads the next line from the specified file descriptor.
* Lines are delimited by newline characters, which are included in the output.
*/
char *get_next_line(int fd)
{
static char *stored_data[OPEN_MAX];
if (fd < 0 || BUFFER_SIZE <= 0 || fd >= OPEN_MAX)
return (NULL);
if (!stored_data[fd] || !ft_strchr(stored_data[fd], '\n'))
stored_data[fd] = read_until_newline(fd, stored_data[fd]);
return (extract_line(&stored_data[fd]));
}
// #include <fcntl.h>
// #include <stdio.h>
// int main(void)
// {
// int fd_sample;
// int fd_sam;
// char *line;
// int i;
// // fd = 0;
// fd_sample = open("sample.txt", O_RDONLY);
// fd_sam = open("sam.txt", O_RDONLY);
// if (fd_sam == -1 || fd_sample == -1)
// {
// perror("open");
// return (1);
// }
// i = 0;
// printf("%d\n",fd_sample);
// printf("%d\n",fd_sam);
// line = get_next_line(fd_sample);
// printf("sample:%s", line);
// free(line);
// line = get_next_line(fd_sample);
// printf("sample:%s", line);
// free(line);
// line = get_next_line(fd_sam);
// printf("sam :%s", line);
// free(line);
// line = get_next_line(fd_sample);
// printf("sample:%s", line);
// free(line);
// line = get_next_line(fd_sample);
// printf("sample:%s", line);
// free(line);
// line = get_next_line(fd_sam);
// printf("sam :%s", line);
// free(line);
// line = get_next_line(fd_sample);
// printf("sample:%s", line);
// free(line);
// line = get_next_line(fd_sam);
// printf("sam :%s", line);
// free(line);
// close(fd_sample);
// close(fd_sam);
// return (0);
// }