fdf add lib
This commit is contained in:
BIN
get_next_line/a.out
Executable file
BIN
get_next_line/a.out
Executable file
Binary file not shown.
85
get_next_line/get_next_line.c
Normal file
85
get_next_line/get_next_line.c
Normal file
@ -0,0 +1,85 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
|
||||
/* Updated: 2023/12/08 18:17:17 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.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;
|
||||
char *res;
|
||||
ssize_t bytes_read;
|
||||
|
||||
res = ft_calloc(1, 1);
|
||||
if (!buf)
|
||||
buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
|
||||
while (buf && res)
|
||||
{
|
||||
res = ft_strjoin(res, buf);
|
||||
if (check_line(res, buf))
|
||||
return (res);
|
||||
bytes_read = read(fd, buf, BUFFER_SIZE);
|
||||
if (bytes_read < 1)
|
||||
{
|
||||
free(buf);
|
||||
buf = NULL;
|
||||
if (res[0] != 0)
|
||||
return (res);
|
||||
free(res);
|
||||
return (NULL);
|
||||
}
|
||||
buf[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/get_next_line.h
Normal file
29
get_next_line/get_next_line.h
Normal file
@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
|
||||
/* Updated: 2023/12/08 18:01:49 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_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
|
85
get_next_line/get_next_line_bonus.c
Normal file
85
get_next_line/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/get_next_line_bonus.h
Normal file
29
get_next_line/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
|
91
get_next_line/get_next_line_utils.c
Normal file
91
get_next_line/get_next_line_utils.c
Normal file
@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
|
||||
/* Updated: 2023/12/08 17:52:41 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.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);
|
||||
}
|
91
get_next_line/get_next_line_utils_bonus.c
Normal file
91
get_next_line/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);
|
||||
}
|
8
get_next_line/test.txt
Normal file
8
get_next_line/test.txt
Normal file
@ -0,0 +1,8 @@
|
||||
Si tu lis cette ligne GGWP
|
||||
asdfasdf
|
||||
sad
|
||||
f
|
||||
|
||||
sadf
|
||||
|
||||
asdfasdff
|
Reference in New Issue
Block a user