This commit is contained in:
Khaïs COLIN 2024-11-08 15:09:12 +01:00
parent 607a121ced
commit 80f0b560ce
3 changed files with 22 additions and 22 deletions

View file

@ -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/11/06 17:33:37 by kcolin ### ########.fr */ /* Updated: 2024/11/08 15:05:50 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,11 +33,11 @@ char *read_at_least_one_line(char *buffer, int fd)
char *read_buffer; char *read_buffer;
int bytes_read; int bytes_read;
read_buffer = malloc((BUFFER_SIZE + 1 ) * sizeof(char)); read_buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (read_buffer == NULL) if (read_buffer == NULL)
return (NULL); return (NULL);
bytes_read = 1; bytes_read = 1;
while (!ft_strchr(buffer, '\n') && bytes_read > 0) while (ft_strchr(buffer, '\n') == NULL && bytes_read > 0)
{ {
bytes_read = read(fd, read_buffer, BUFFER_SIZE); bytes_read = read(fd, read_buffer, BUFFER_SIZE);
if (bytes_read < 0) if (bytes_read < 0)
@ -48,18 +48,19 @@ char *read_at_least_one_line(char *buffer, int fd)
} }
read_buffer[bytes_read] = '\0'; read_buffer[bytes_read] = '\0';
buffer = ft_strjoin(buffer, read_buffer); buffer = ft_strjoin(buffer, read_buffer);
if (buffer == NULL)
break ;
} }
free(read_buffer); free(read_buffer);
return (buffer); return (buffer);
} }
#include <stdio.h>
char *get_next_line(int fd) char *get_next_line(int fd)
{ {
static char *buffer = NULL; static char *buffer = NULL;
char *out; char *out;
size_t line_length; size_t line_length;
char *buf;
if (fd < 0) if (fd < 0)
return (NULL); return (NULL);
@ -67,9 +68,11 @@ char *get_next_line(int fd)
if (buffer == NULL) if (buffer == NULL)
return (NULL); return (NULL);
line_length = 0; line_length = 0;
while (buffer[line_length] != '\0' || buffer[line_length] != '\n') while (buffer[line_length] != '\0' && buffer[line_length] != '\n')
line_length++; line_length++;
out = ft_substr(buffer, 0, line_length + 1); out = ft_substr(buffer, 0, line_length + 1);
buffer = ft_substr(buffer, line_length, ft_strlen(buffer) - line_length); buf = buffer;
buffer = ft_substr(buf, line_length + 1, ft_strlen(buffer) - line_length);
free(buf);
return (out); return (out);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:23:21 by kcolin #+# #+# */ /* Created: 2024/10/23 20:23:21 by kcolin #+# #+# */
/* Updated: 2024/11/06 16:51:35 by kcolin ### ########.fr */ /* Updated: 2024/11/08 14:58:43 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,7 @@
# include <unistd.h> # include <unistd.h>
# ifndef BUFFER_SIZE # ifndef BUFFER_SIZE
# define BUFFER_SIZE 1024 # define BUFFER_SIZE 1023
# endif # endif
# if BUFFER_SIZE <= 0 # if BUFFER_SIZE <= 0
# error BUFFER_SIZE must be at least 1 # error BUFFER_SIZE must be at least 1

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/01 12:31:58 by kcolin #+# #+# */ /* Created: 2024/11/01 12:31:58 by kcolin #+# #+# */
/* Updated: 2024/11/06 17:37:39 by kcolin ### ########.fr */ /* Updated: 2024/11/08 15:04:19 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,24 +34,21 @@ char *ft_strjoin(char *s1, char *s2)
if (s1 == NULL) if (s1 == NULL)
{ {
s1 = malloc(1); s1 = malloc(1);
if (s1 == NULL)
return (NULL);
s1[0] = '\0'; s1[0] = '\0';
} }
out = malloc(ft_strlen(s1) + ft_strlen(s2) + 1); out = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (out == NULL) if (out == NULL)
return (NULL); return (NULL);
i = 0; i = -1;
j = 0; j = -1;
while (s1[i] != '\0') while (s1[++i] != '\0')
{
out[i] = s1[i]; out[i] = s1[i];
i++; while (s2[++j] != '\0')
} out[i++] = s2[j];
while (s2[j] != '\0') out[i] = '\0';
{ free(s1);
out[i] = s2[j];
i++;
j++;
}
return (out); return (out);
} }