finished
This commit is contained in:
73
fff/get_next_line_utils_madumerg.c
Normal file
73
fff/get_next_line_utils_madumerg.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/24 11:51:43 by madumerg #+# #+# */
|
||||||
|
/* Updated: 2023/12/08 17:21:26 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
|
||||||
|
size_t ft_strlen(char *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_strjoin(char *s1, char *s2)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
size_t i;
|
||||||
|
size_t j;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
if (!s2)
|
||||||
|
return (NULL);
|
||||||
|
len = ft_strlen(s1) + ft_strlen(s2);
|
||||||
|
str = malloc(sizeof(char) * (len + 1));
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (s1[i] != '\0')
|
||||||
|
{
|
||||||
|
str[i] = s1[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
j = 0;
|
||||||
|
while (s2[j] != '\0')
|
||||||
|
str[i++] = s2[j++];
|
||||||
|
free(s1);
|
||||||
|
str[i] = '\0';
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *ft_calloc(size_t ct, size_t size)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (size == 0 || ct == 0)
|
||||||
|
return (malloc(1));
|
||||||
|
if ((int)size < 0 && (int)ct < 0)
|
||||||
|
return (NULL);
|
||||||
|
if ((unsigned long long)(size * ct) > 4294967295)
|
||||||
|
return (NULL);
|
||||||
|
str = malloc(size * ct);
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
while (i < (ct * size))
|
||||||
|
{
|
||||||
|
*(unsigned char *)(str + i) = '\0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (str);
|
||||||
|
}
|
@ -6,21 +6,34 @@
|
|||||||
/* 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/08 11:31:04 by adjoly ### ########.fr */
|
/* Updated: 2023/12/08 18:17:17 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "get_next_line.h"
|
#include "get_next_line.h"
|
||||||
|
|
||||||
int check_line(char *buf)
|
char check_line(char *res, char *buf)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (buf[i] && buf[i] != '\n')
|
j = 0;
|
||||||
|
while (res[i] && res[i] != '\n')
|
||||||
i++;
|
i++;
|
||||||
if (buf[i] == '\n' || buf[i] == '\0')
|
if (res[i] == '\n')
|
||||||
return (i + (buf[i] == '\n'));
|
{
|
||||||
|
i++;
|
||||||
|
while (res[i])
|
||||||
|
{
|
||||||
|
buf[j] = res[i];
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
buf[j] = 0;
|
||||||
|
res[i - j] = 0;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,39 +41,32 @@ char *get_next_line(int fd)
|
|||||||
{
|
{
|
||||||
static char *buf;
|
static char *buf;
|
||||||
char *res;
|
char *res;
|
||||||
size_t size;
|
ssize_t bytes_read;
|
||||||
int bytes_read;
|
|
||||||
|
|
||||||
if (BUFFER_SIZE <= 0 || fd < 0)
|
|
||||||
return (NULL);
|
|
||||||
res = ft_calloc(1, 1);
|
res = ft_calloc(1, 1);
|
||||||
size = BUFFER_SIZE;
|
|
||||||
if (!buf)
|
if (!buf)
|
||||||
buf = malloc(sizeof(char) * (BUFFER_SIZE + 1));
|
buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
|
||||||
while (buf && res)
|
while (buf && res)
|
||||||
{
|
{
|
||||||
|
res = ft_strjoin(res, buf);
|
||||||
|
if (check_line(res, buf))
|
||||||
|
return (res);
|
||||||
bytes_read = read(fd, buf, BUFFER_SIZE);
|
bytes_read = read(fd, buf, BUFFER_SIZE);
|
||||||
size = check_line(buf);
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
free(buf);
|
|
||||||
return (res);
|
|
||||||
}
|
|
||||||
free(buf);
|
free(buf);
|
||||||
buf = NULL;
|
buf = NULL;
|
||||||
|
if (res[0] != 0)
|
||||||
|
return (res);
|
||||||
|
free(res);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
else if (bytes_read >= 1)
|
buf[bytes_read] = 0;
|
||||||
return (res);
|
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <unistd.h>
|
/*#include <unistd.h>
|
||||||
void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}}
|
void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}}
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -69,11 +75,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 (ln != NULL)
|
while (ln)
|
||||||
// {
|
{
|
||||||
ln = get_next_line(fd);
|
ln = get_next_line(fd);
|
||||||
ft_putstr(ln);
|
ft_putstr(ln);
|
||||||
free(ln);
|
free(ln);
|
||||||
// }
|
|
||||||
close(fd);
|
|
||||||
}
|
}
|
||||||
|
close(fd);
|
||||||
|
}*/
|
||||||
|
@ -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/08 11:12:12 by adjoly ### ########.fr */
|
/* Updated: 2023/12/08 18:01:49 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -21,9 +21,9 @@
|
|||||||
# endif
|
# endif
|
||||||
|
|
||||||
char *get_next_line(int fd);
|
char *get_next_line(int fd);
|
||||||
char *ft_strljoin(char *s1, char *s2, size_t len);
|
char *ft_strjoin(char *s1, char *s2);
|
||||||
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_strlen_nl(char *s);
|
||||||
|
|
||||||
#endif
|
#endif
|
85
get_next_line_bonus.c
Normal file
85
get_next_line_bonus.c
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2023/12/08 18:02:13 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line_bonus.h"
|
||||||
|
|
||||||
|
char check_line(char *res, char *buf)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
while (res[i] && res[i] != '\n')
|
||||||
|
i++;
|
||||||
|
if (res[i] == '\n')
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
while (res[i])
|
||||||
|
{
|
||||||
|
buf[j] = res[i];
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
buf[j] = 0;
|
||||||
|
res[i - j] = 0;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_next_line(int fd)
|
||||||
|
{
|
||||||
|
static char *buf[1024];
|
||||||
|
char *res;
|
||||||
|
size_t bytes_read;
|
||||||
|
|
||||||
|
res = ft_calloc(1, 1);
|
||||||
|
if (!buf[fd])
|
||||||
|
buf[fd] = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
|
||||||
|
while (buf[fd] && res)
|
||||||
|
{
|
||||||
|
res = ft_strjoin(res, buf);
|
||||||
|
if (check_line(res, buf[fd]))
|
||||||
|
return (res);
|
||||||
|
bytes_read = read(fd, buf[fd], BUFFER_SIZE);
|
||||||
|
if (bytes_read < 1)
|
||||||
|
{
|
||||||
|
free(&buf[fd]);
|
||||||
|
buf[fd] = NULL;
|
||||||
|
if (res[0] != 0)
|
||||||
|
return (res);
|
||||||
|
free(res);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
buf[fd][bytes_read] = 0;
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*#include <unistd.h>
|
||||||
|
void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}}
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *ln;
|
||||||
|
int fd;
|
||||||
|
fd = open("test.txt", O_RDONLY);
|
||||||
|
while (ln)
|
||||||
|
{
|
||||||
|
ln = get_next_line(fd);
|
||||||
|
ft_putstr(ln);
|
||||||
|
free(ln);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
}*/
|
29
get_next_line_bonus.h
Normal file
29
get_next_line_bonus.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_bonus.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2023/12/08 18:01:40 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef GET_NEXT_LINE_BONUS_H
|
||||||
|
# define GET_NEXT_LINE_BONUS_H
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
|
||||||
|
# ifndef BUFFER_SIZE
|
||||||
|
# define BUFFER_SIZE 5
|
||||||
|
# endif
|
||||||
|
|
||||||
|
char *get_next_line(int fd);
|
||||||
|
char *ft_strjoin(char *s1, char *s2);
|
||||||
|
size_t ft_strlen(char *s);
|
||||||
|
void *ft_calloc(size_t nmemb, size_t size);
|
||||||
|
size_t ft_strlen_nl(char *s);
|
||||||
|
|
||||||
|
#endif
|
@ -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/08 11:17:51 by adjoly ### ########.fr */
|
/* Updated: 2023/12/08 17:52:41 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -22,17 +22,7 @@ size_t ft_strlen(char *s)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*size_t ft_strlen_til_nl(char *s)
|
char *ft_strjoin(char *s1, char *s2)
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (s[i] && s[i] != '\n')
|
|
||||||
i++;
|
|
||||||
return (i);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
char *ft_strljoin(char *s1, char *s2, size_t len)
|
|
||||||
{
|
{
|
||||||
char *result;
|
char *result;
|
||||||
size_t i;
|
size_t i;
|
||||||
@ -40,9 +30,9 @@ char *ft_strljoin(char *s1, char *s2, size_t len)
|
|||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
j = 0;
|
j = 0;
|
||||||
if (!s1 || !s2)
|
if (!s2)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
result = ft_calloc((ft_strlen(s1) + len + 1), sizeof(char));
|
result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
while (s1[i])
|
while (s1[i])
|
||||||
@ -50,7 +40,7 @@ char *ft_strljoin(char *s1, char *s2, size_t len)
|
|||||||
result[i] = s1[i];
|
result[i] = s1[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
while (s2[j] || j < len)
|
while (s2[j])
|
||||||
{
|
{
|
||||||
result[i] = s2[j];
|
result[i] = s2[j];
|
||||||
i++;
|
i++;
|
||||||
@ -68,32 +58,34 @@ size_t ft_strlcpy(char *dst, char *src, size_t size)
|
|||||||
i = 0;
|
i = 0;
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return (ft_strlen(src));
|
return (ft_strlen(src));
|
||||||
while (i < size - 1 && src[i])
|
while (i < size && src[i])
|
||||||
{
|
{
|
||||||
dst[i] = src[i];
|
dst[i] = src[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (i < size)
|
dst[size] = '\0';
|
||||||
dst[i] = '\0';
|
|
||||||
return (ft_strlen(src));
|
return (ft_strlen(src));
|
||||||
}
|
}
|
||||||
|
|
||||||
void *ft_calloc(size_t nmemb, size_t size)
|
void *ft_calloc(size_t nmemb, size_t size)
|
||||||
{
|
{
|
||||||
|
char *str;
|
||||||
size_t i;
|
size_t i;
|
||||||
void *result;
|
|
||||||
|
|
||||||
if (size != 0 && nmemb != 0 && (nmemb * size) / nmemb != size)
|
i = 0;
|
||||||
return (NULL);
|
|
||||||
if (nmemb == 0 || size == 0)
|
if (nmemb == 0 || size == 0)
|
||||||
return (malloc(1));
|
return (malloc(1));
|
||||||
result = malloc(size * nmemb);
|
if ((int)size < 0 && (int)nmemb < 0)
|
||||||
if (result == NULL)
|
|
||||||
return (NULL);
|
return (NULL);
|
||||||
while (i < size * nmemb)
|
if ((unsigned long long)(size * nmemb) > 4294967295)
|
||||||
|
return (NULL);
|
||||||
|
str = malloc(nmemb * size);
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
while (i < (nmemb * size))
|
||||||
{
|
{
|
||||||
*(unsigned char *)(result + i) = '\0';
|
*(unsigned char *)(str + i) = '\0';
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (result);
|
return (str);
|
||||||
}
|
}
|
||||||
|
91
get_next_line_utils_bonus.c
Normal file
91
get_next_line_utils_bonus.c
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_utils_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2023/12/08 17:54:32 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line_bonus.h"
|
||||||
|
|
||||||
|
size_t ft_strlen(char *s)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_strjoin(char *s1, char *s2)
|
||||||
|
{
|
||||||
|
char *result;
|
||||||
|
size_t i;
|
||||||
|
size_t j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
if (!s2)
|
||||||
|
return (NULL);
|
||||||
|
result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
|
||||||
|
if (result == NULL)
|
||||||
|
return (NULL);
|
||||||
|
while (s1[i])
|
||||||
|
{
|
||||||
|
result[i] = s1[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (s2[j])
|
||||||
|
{
|
||||||
|
result[i] = s2[j];
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
free(s1);
|
||||||
|
result[i] = '\0';
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ft_strlcpy(char *dst, char *src, size_t size)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (size == 0)
|
||||||
|
return (ft_strlen(src));
|
||||||
|
while (i < size && src[i])
|
||||||
|
{
|
||||||
|
dst[i] = src[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dst[size] = '\0';
|
||||||
|
return (ft_strlen(src));
|
||||||
|
}
|
||||||
|
|
||||||
|
void *ft_calloc(size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (nmemb == 0 || size == 0)
|
||||||
|
return (malloc(1));
|
||||||
|
if ((int)size < 0 && (int)nmemb < 0)
|
||||||
|
return (NULL);
|
||||||
|
if ((unsigned long long)(size * nmemb) > 4294967295)
|
||||||
|
return (NULL);
|
||||||
|
str = malloc(nmemb * size);
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
while (i < (nmemb * size))
|
||||||
|
{
|
||||||
|
*(unsigned char *)(str + i) = '\0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (str);
|
||||||
|
}
|
Reference in New Issue
Block a user