1
0
This commit is contained in:
Adam Joly
2023-12-08 11:31:17 +01:00
parent aa18417fe2
commit afcbcabcc0
6 changed files with 25 additions and 26 deletions

BIN
a.out

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2023/12/06 12:34:24 by adjoly ### ########.fr */ /* Updated: 2023/12/08 11:31:04 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@ int check_line(char *buf)
i = 0; i = 0;
while (buf[i] && buf[i] != '\n') while (buf[i] && buf[i] != '\n')
i++; i++;
if (buf[i] == '\n') if (buf[i] == '\n' || buf[i] == '\0')
return (i + (buf[i] == '\n')); return (i + (buf[i] == '\n'));
return (0); return (0);
} }
@ -28,24 +28,28 @@ char *get_next_line(int fd)
{ {
static char *buf; static char *buf;
char *res; char *res;
int size; size_t size;
int bytes_read; int bytes_read;
if (BUFFER_SIZE <= 0 || fd < 0) if (BUFFER_SIZE <= 0 || fd < 0)
return (NULL); return (NULL);
res = ft_calloc(1, 1); res = ft_calloc(1, 1);
size = BUFFER_SIZE;
if (!buf) if (!buf)
buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1); buf = malloc(sizeof(char) * (BUFFER_SIZE + 1));
while (buf && res) while (buf && res)
{ {
// size = check_line(res);
bytes_read = read(fd, buf, BUFFER_SIZE); bytes_read = read(fd, buf, BUFFER_SIZE);
res = ft_strjoin(res, buf); size = check_line(buf);
buf[bytes_read] = '\0'; res = ft_strljoin(res, buf, size);
// ft_strlcpy(buf, &res[size], ft_strlen(&res[size]));
if (bytes_read < 1) if (bytes_read < 1)
{ {
if (ft_strlen(res) > 0) if (ft_strlen(res) > 0)
{
free(buf);
return (res); return (res);
}
free(buf); free(buf);
buf = NULL; buf = NULL;
return (NULL); return (NULL);
@ -65,12 +69,11 @@ int main(void)
char *ln; char *ln;
int fd; int fd;
fd = open("test.txt", O_RDONLY); fd = open("test.txt", O_RDONLY);
while (1) // while (ln != NULL)
{ // {
ln = get_next_line(fd); ln = get_next_line(fd);
if (ln == NULL)
return (0);
ft_putstr(ln); ft_putstr(ln);
free(ln); free(ln);
} // }
close(fd);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */ /* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
/* Updated: 2023/12/06 11:16:29 by adjoly ### ########.fr */ /* Updated: 2023/12/08 11:12:12 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,10 +21,9 @@
# endif # endif
char *get_next_line(int fd); char *get_next_line(int fd);
char *ft_strjoin(char *s1, char *s2); char *ft_strljoin(char *s1, char *s2, size_t len);
size_t ft_strlen_til_nl(char *s);
size_t ft_strlen(char *s); size_t ft_strlen(char *s);
void *ft_calloc(size_t nmemb, size_t size); void *ft_calloc(size_t nmemb, size_t size);
size_t ft_strlcpy(char *dst, char *src, size_t size); size_t ft_strlcpy(char *dst, char *src, size_t size);
#endif #endif

BIN
get_next_line.h.gch Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */ /* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2023/12/06 12:05:08 by adjoly ### ########.fr */ /* Updated: 2023/12/08 11:17:51 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,14 +17,12 @@ size_t ft_strlen(char *s)
size_t i; size_t i;
i = 0; i = 0;
if (s[i] == 0)
return (0);
while (s[i]) while (s[i])
i++; i++;
return (i); return (i);
} }
size_t ft_strlen_til_nl(char *s) /*size_t ft_strlen_til_nl(char *s)
{ {
size_t i; size_t i;
@ -32,9 +30,9 @@ size_t ft_strlen_til_nl(char *s)
while (s[i] && s[i] != '\n') while (s[i] && s[i] != '\n')
i++; i++;
return (i); return (i);
} }*/
char *ft_strjoin(char *s1, char *s2) char *ft_strljoin(char *s1, char *s2, size_t len)
{ {
char *result; char *result;
size_t i; size_t i;
@ -42,9 +40,9 @@ char *ft_strjoin(char *s1, char *s2)
i = 0; i = 0;
j = 0; j = 0;
if (s1 == NULL && s2 == NULL) if (!s1 || !s2)
return (NULL); return (NULL);
result = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char)); result = ft_calloc((ft_strlen(s1) + len + 1), sizeof(char));
if (result == NULL) if (result == NULL)
return (NULL); return (NULL);
while (s1[i]) while (s1[i])
@ -52,7 +50,7 @@ char *ft_strjoin(char *s1, char *s2)
result[i] = s1[i]; result[i] = s1[i];
i++; i++;
} }
while (s2[j]) while (s2[j] || j < len)
{ {
result[i] = s2[j]; result[i] = s2[j];
i++; i++;

View File

@ -1,4 +1,3 @@
Si tu lit cette ligne GGWP Si tu lit cette ligne GGWP
Main non tu peux voir celle ci aussi Main non tu peux voir celle ci aussi
What tu lis celle la aussi What tu lis celle la aussi
LLL