It seems to work!
This commit is contained in:
parent
b6e406cc7d
commit
fd2c903d48
2 changed files with 53 additions and 9 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */
|
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */
|
||||||
/* Updated: 2024/10/26 10:27:01 by kcolin ### ########.fr */
|
/* Updated: 2024/10/31 12:45:27 by kcolin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
char *ft_strchr(const char *s, int c)
|
char *ft_strchr(const char *s, int c)
|
||||||
{
|
{
|
||||||
int i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (s[i] != '\0')
|
while (s[i] != '\0')
|
||||||
|
|
@ -31,7 +31,7 @@ char *ft_strchr(const char *s, int c)
|
||||||
|
|
||||||
size_t ft_strlen(const char *s)
|
size_t ft_strlen(const char *s)
|
||||||
{
|
{
|
||||||
int i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (s[i] != '\0')
|
while (s[i] != '\0')
|
||||||
|
|
@ -85,7 +85,7 @@ size_t ft_strlcat(char *dst, const char *src, size_t size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char *ft_strjoin(char const *s1, char const *s2)
|
char *ft_strjoin_free_s1(char const *s1, char const *s2)
|
||||||
{
|
{
|
||||||
char *out;
|
char *out;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
@ -96,13 +96,37 @@ char *ft_strjoin(char const *s1, char const *s2)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
ft_strlcpy(out, s1, len);
|
ft_strlcpy(out, s1, len);
|
||||||
ft_strlcat(out, s2, len);
|
ft_strlcat(out, s2, len);
|
||||||
|
free((void *)s1);
|
||||||
return (out);
|
return (out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *shorten_buffer(char *buffer)
|
||||||
|
{
|
||||||
|
char *outbuf;
|
||||||
|
char *addr_of_newline;
|
||||||
|
size_t index_after_newline;
|
||||||
|
size_t outlen;
|
||||||
|
|
||||||
|
addr_of_newline = ft_strchr(buffer, '\n');
|
||||||
|
if (addr_of_newline == 0)
|
||||||
|
{
|
||||||
|
outbuf = malloc(1 * sizeof(char));
|
||||||
|
outbuf[0] = '\0';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
index_after_newline = addr_of_newline + 1 - buffer;
|
||||||
|
outlen = ft_strlen(buffer + index_after_newline) + 1;
|
||||||
|
outbuf = malloc(outlen * sizeof(char));
|
||||||
|
ft_strlcpy(outbuf, buffer + index_after_newline, outlen);
|
||||||
|
}
|
||||||
|
free(buffer);
|
||||||
|
return (outbuf);
|
||||||
|
}
|
||||||
|
|
||||||
char *get_next_line(int fd)
|
char *get_next_line(int fd)
|
||||||
{
|
{
|
||||||
static char *buffer = "";
|
static char *buffer = NULL;
|
||||||
char *read_buffer;
|
char *read_buffer;
|
||||||
int num_bytes_read;
|
int num_bytes_read;
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
@ -111,6 +135,11 @@ char *get_next_line(int fd)
|
||||||
read_buffer = malloc(BUFFER_SIZE * sizeof(char));
|
read_buffer = malloc(BUFFER_SIZE * sizeof(char));
|
||||||
num_bytes_read = 1;
|
num_bytes_read = 1;
|
||||||
length = 0;
|
length = 0;
|
||||||
|
if (buffer == NULL)
|
||||||
|
{
|
||||||
|
buffer = malloc(sizeof(char));
|
||||||
|
buffer[0] = '\0';
|
||||||
|
}
|
||||||
while (num_bytes_read != 0)
|
while (num_bytes_read != 0)
|
||||||
{
|
{
|
||||||
// DONE: if newline found,
|
// DONE: if newline found,
|
||||||
|
|
@ -119,11 +148,18 @@ char *get_next_line(int fd)
|
||||||
// DONE: copy string until newline to new buffer,
|
// DONE: copy string until newline to new buffer,
|
||||||
outbuf = malloc((ft_strchr(buffer, '\n') - buffer + 2) * sizeof(char));
|
outbuf = malloc((ft_strchr(buffer, '\n') - buffer + 2) * sizeof(char));
|
||||||
if (outbuf == NULL)
|
if (outbuf == NULL)
|
||||||
|
{
|
||||||
|
free(read_buffer);
|
||||||
|
free(buffer);
|
||||||
|
buffer = NULL;
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
}
|
||||||
ft_strlcpy(outbuf, buffer, ft_strchr(buffer, '\n') - buffer + 2);
|
ft_strlcpy(outbuf, buffer, ft_strchr(buffer, '\n') - buffer + 2);
|
||||||
// TODO: shorten current buffer,
|
// DONE: shorten current buffer,
|
||||||
buffer += ft_strchr(buffer, '\n') - buffer + 1;
|
// buffer += ft_strchr(buffer, '\n') - buffer + 1;
|
||||||
|
buffer = shorten_buffer(buffer);
|
||||||
// DONE: and return.
|
// DONE: and return.
|
||||||
|
free(read_buffer);
|
||||||
return (outbuf);
|
return (outbuf);
|
||||||
}
|
}
|
||||||
num_bytes_read = read(fd, read_buffer + length, BUFFER_SIZE - length - 1);
|
num_bytes_read = read(fd, read_buffer + length, BUFFER_SIZE - length - 1);
|
||||||
|
|
@ -138,8 +174,13 @@ char *get_next_line(int fd)
|
||||||
length += num_bytes_read;
|
length += num_bytes_read;
|
||||||
// DONE: join with big buffer
|
// DONE: join with big buffer
|
||||||
// FIXME: correctly free buffer
|
// FIXME: correctly free buffer
|
||||||
buffer = ft_strjoin(buffer, read_buffer);
|
buffer = ft_strjoin_free_s1(buffer, read_buffer);
|
||||||
}
|
}
|
||||||
free(read_buffer);
|
free(read_buffer);
|
||||||
|
if (ft_strlen(buffer) == 0)
|
||||||
|
{
|
||||||
|
free(buffer);
|
||||||
|
buffer = NULL;
|
||||||
|
}
|
||||||
return (buffer);
|
return (buffer);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
5
test.c
5
test.c
|
|
@ -17,18 +17,21 @@
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
int i;
|
||||||
char *line;
|
char *line;
|
||||||
|
|
||||||
if (argc == 2)
|
if (argc == 2)
|
||||||
{
|
{
|
||||||
fd = open(argv[1], O_RDONLY);
|
fd = open(argv[1], O_RDONLY);
|
||||||
line = "";
|
line = "";
|
||||||
|
i = 0;
|
||||||
while (line != NULL)
|
while (line != NULL)
|
||||||
{
|
{
|
||||||
line = get_next_line(fd);
|
line = get_next_line(fd);
|
||||||
if (line != NULL)
|
if (line != NULL)
|
||||||
{
|
{
|
||||||
printf("%s", line);
|
i++;
|
||||||
|
printf("%d\t%s", i, line);
|
||||||
free(line);
|
free(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue