sa marche un peux

This commit is contained in:
Camille Chauvet 2022-10-26 17:25:43 +02:00
parent 07565c2c6c
commit df151381bc
7 changed files with 56 additions and 27 deletions

Binary file not shown.

BIN
a.out

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */ /* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/10/26 01:53:25 by cchauvet ### ########.fr */ /* Updated: 2022/10/26 16:39:09 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,7 +38,7 @@ ssize_t ft_strchr(char *str, char c)
i = 0; i = 0;
while (str[i] != c && str[i] != '\0') while (str[i] != c && str[i] != '\0')
i++; i++;
if (str[i] == '\0' && c != '\0') if (str[i] == '\0')
return (-1); return (-1);
return (i); return (i);
} }
@ -51,20 +51,20 @@ char *ft_getline(int fd)
stash = NULL; stash = NULL;
i = 0; i = 0;
while (ft_strchr(stash + i * BUFFER_SIZE, '\n')) while (ft_strchr(stash + i, '\n') == -1)
{ {
stash = ft_realloc(stash, (i + 1) * BUFFER_SIZE); i = i + ft_strlen(buf);
if (stash == NULL)
return (NULL);
buf = ft_getstash(fd); buf = ft_getstash(fd);
if (buf == NULL) if (buf == NULL)
{
free(stash);
return (NULL); return (NULL);
} if (buf[0] == '\0')
ft_strfcat(stash, buf); {
if (stash[(i + 1) * BUFFER_SIZE -1] == '\0') free(buf);
break ; break ;
}
stash = ft_strfjoin(stash, buf);
if (stash == NULL)
return (NULL);
} }
return (stash); return (stash);
} }
@ -80,13 +80,12 @@ char *get_next_line(int fd)
buf1 = ft_getline(fd); buf1 = ft_getline(fd);
if (buf1 == NULL) if (buf1 == NULL)
return (NULL); return (NULL);
stash = ft_realloc(stash, ft_strchr(buf1, 0) + ft_strchr(stash, 0) + 2); stash = ft_strfjoin(stash, buf1);
if (stash == NULL) if (stash == NULL)
return (NULL); return (NULL);
ft_strfcat(stash, buf1);
} }
buf1 = ft_strndup(stash, ft_strchr(stash, '\n') + 1); buf1 = ft_strndup(stash, ft_strchr(stash, '\n') + 1);
buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strchr(stash, 0)); buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strlen(stash));
free(stash); free(stash);
stash = buf2; stash = buf2;
if (stash == NULL || buf1 == NULL) if (stash == NULL || buf1 == NULL)

View File

@ -3,9 +3,12 @@
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# define BUFFER_SIZE 10
void *ft_calloc(size_t nmemb, size_t size); void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size); void *ft_realloc(void *ptr, size_t size);
char *ft_strfcat(char *dst, char *src); char *ft_strfjoin(char *dst, char *src);
char *ft_strndup(char *src, size_t n); char *ft_strndup(char *src, size_t n);
size_t ft_strlen(char *str);
char *get_next_line(int fd); char *get_next_line(int fd);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */ /* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/10/26 01:24:43 by cchauvet ### ########.fr */ /* Updated: 2022/10/26 16:33:23 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,7 +42,10 @@ void *ft_realloc(void *ptr, size_t size)
if (tab == NULL) if (tab == NULL)
return (NULL); return (NULL);
if (ptr == NULL) if (ptr == NULL)
{
free(ptr);
return (tab); return (tab);
}
i = 0; i = 0;
while (in[i] != 0 && i < size) while (in[i] != 0 && i < size)
{ {
@ -53,26 +56,45 @@ void *ft_realloc(void *ptr, size_t size)
return (tab); return (tab);
} }
char *ft_strfcat(char *dst, char *src) size_t ft_strlen(char *str)
{
size_t i;
if (str == NULL)
return (0);
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *ft_strfjoin(char *s1, char *s2)
{ {
size_t i; size_t i;
size_t j; size_t j;
char *out;
j = 0; out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
while (dst[j] != '\0') if (out == NULL)
j++; return (NULL);
i = 0; i = 0;
if (src != NULL) if (s1 != NULL)
{ {
while (src[i] != '\0') while (s1[i] != '\0')
{ {
dst[i + j] = src[i]; out[i] = s1[i];
i++; i++;
} }
} }
dst[i] = '\0'; free(s1);
free(src); j = 0;
return (dst); while (s2[j] != '\0')
{
out[i + j] = s2[j];
j++;
}
free(s2);
return (out);
} }
char *ft_strndup(char *src, size_t n) char *ft_strndup(char *src, size_t n)

4
main.c
View File

@ -7,7 +7,9 @@
int main(void) int main(void)
{ {
int fd = open("tester", O_RDONLY); int fd = open("tester", O_RDONLY);
printf("%s", get_next_line(fd)); char *str = get_next_line(fd);
printf("%s", str);
free(str);
// printf("%s", get_next_line(fd)); // printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd)); // printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd)); // printf("%s", get_next_line(fd));

3
tester
View File

@ -0,0 +1,3 @@
test1
test2
test3