-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
128 lines (117 loc) · 2.92 KB
/
get_next_line.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: psevilla <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 16:55:07 by psevilla #+# #+# */
/* Updated: 2024/11/20 21:01:18 by psevilla ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static char *ft_new_line(int fd, char *buf, char *next_line)
{
int bytes_red;
char *temp;
bytes_red = 1;
while (bytes_red)
{
bytes_red = read(fd, buf, BUFFER_SIZE);
if (bytes_red == -1)
return (NULL);
else if (bytes_red == 0)
break ;
buf [bytes_red] = '\0';
if (!next_line)
next_line = ft_strdup("");
temp = next_line;
next_line = ft_strjoin(temp, buf);
free(temp);
temp = NULL;
if (ft_strchr(buf, '\n'))
break ;
}
return (next_line);
}
static char *ft_next_line(char *line)
{
size_t i;
char *next_line;
i = 0;
while (line[i] != '\n' && line[i])
i++;
if (!line[i] || !line[1])
return (NULL);
next_line = ft_substr(line, i + 1, ft_strlen(line) - i);
if (!next_line)
free(next_line);
line[i + 1] = '\0';
return (next_line);
}
char *get_next_line(int fd)
{
char *line;
char *buf;
static char *next_line;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
buf = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buf)
return (NULL);
line = ft_new_line(fd, buf, next_line);
free(buf);
buf = NULL;
if (!line)
return (NULL);
next_line = ft_next_line(line);
return (line);
}
/* #include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define RED "\033[31m"
#define RESET "\033[0m"
void print_line(const char *line)
{
if (line)
{
for (int i = 0; line[i] != '\0'; i++)
{
if (line[i] == '\n')
{
write(1, RED, 5); // Comienza el texto rojo
write(1, "\\", 1);
write(1, "n", 1); // Imprime "n" en rojo
write(1, RESET, 4); // Resetea el color
write(1, "\n", 1); // Imprime un salto de línea real
}
else
{
write(1, &line[i], 1);
}
}
}
}
int main(void)
{
int fd;
char *line;
fd = open("prueba.txt", 0); // Asegúrate de que el archivo "test.txt" exista
if (fd < 0)
{
perror("Error opening file");
return (1);
}
line = get_next_line(fd);
while (line)
{
print_line(line);
free(line); // Liberar la memoria asignada para la línea
line = get_next_line(fd);
}
print_line(line);
printf("%s", line);
close(fd);
return (0);
} */