oui
This commit is contained in:
Executable
+25
@@ -0,0 +1,25 @@
|
||||
NAME = get_next_line.a
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
SRC = get_next_line.c get_next_line_utils.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
RM = rm -f
|
||||
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
@ar rcs $(NAME) $(OBJ)
|
||||
|
||||
$(OBJ): $(SRC)
|
||||
@$(CC) $(CFLAGS) -c $(SRC)
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
clean:
|
||||
@$(RM) $(OBJ)
|
||||
|
||||
fclean: clean
|
||||
@$(RM) $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
Executable
BIN
Binary file not shown.
Executable
+105
@@ -0,0 +1,105 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/20 14:35:58 by hexplor #+# #+# */
|
||||
/* Updated: 2024/02/20 21:58:41 by yantoine ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "get_next_line.h"
|
||||
|
||||
static char *ft_getline(char *static_str)
|
||||
{
|
||||
int i;
|
||||
char *line;
|
||||
|
||||
i = 0;
|
||||
if (!static_str[i])
|
||||
return (NULL);
|
||||
while (static_str[i] && static_str[i] != '\n')
|
||||
i++;
|
||||
line = (char *)malloc(sizeof(char) * (i + 2));
|
||||
if (!line)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (static_str[i] && static_str[i] != '\n')
|
||||
{
|
||||
line[i] = static_str[i];
|
||||
i++;
|
||||
}
|
||||
if (static_str[i] == '\n')
|
||||
{
|
||||
line[i] = static_str[i];
|
||||
i++;
|
||||
}
|
||||
line[i] = '\0';
|
||||
return (line);
|
||||
}
|
||||
|
||||
static char *read_fd(int fd, char *str)
|
||||
{
|
||||
char *tmp;
|
||||
int bytes;
|
||||
|
||||
tmp = malloc((BUFFER_SIZE + 1) * sizeof(char));
|
||||
if (!tmp)
|
||||
return (NULL);
|
||||
bytes = 1;
|
||||
while (!gnl_strchr(str, '\n') && (bytes != 0))
|
||||
{
|
||||
bytes = read(fd, tmp, BUFFER_SIZE);
|
||||
if (bytes == -1)
|
||||
{
|
||||
free(tmp);
|
||||
return (NULL);
|
||||
}
|
||||
tmp[bytes] = '\0';
|
||||
str = gnl_strjoin(str, tmp);
|
||||
}
|
||||
free(tmp);
|
||||
return (str);
|
||||
}
|
||||
|
||||
static char *ft_remove_bn(char *static_str)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
char *restof;
|
||||
|
||||
i = 0;
|
||||
while (static_str[i] && static_str[i] != '\n')
|
||||
i++;
|
||||
if (!static_str[i])
|
||||
{
|
||||
free(static_str);
|
||||
return (NULL);
|
||||
}
|
||||
restof = (char *)malloc(sizeof(char) * (gnl_strlen(static_str) - i + 1));
|
||||
if (!restof)
|
||||
return (NULL);
|
||||
i++;
|
||||
j = 0;
|
||||
while (static_str[i])
|
||||
restof[j++] = static_str[i++];
|
||||
restof[j] = '\0';
|
||||
free(static_str);
|
||||
return (restof);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char *static_str;
|
||||
char *line;
|
||||
|
||||
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||
return (0);
|
||||
static_str = read_fd(fd, static_str);
|
||||
if (!static_str)
|
||||
return (NULL);
|
||||
line = ft_getline(static_str);
|
||||
static_str = ft_remove_bn(static_str);
|
||||
return (line);
|
||||
}
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/20 14:34:57 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:07:02 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
|
||||
# include <fcntl.h>
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <stdio.h>
|
||||
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 42
|
||||
# endif
|
||||
|
||||
char *get_next_line(int fd);
|
||||
char *gnl_strjoin(char *s1, char *s2);
|
||||
char *gnl_strchr(char *s, int c);
|
||||
size_t gnl_strlen(char *s);
|
||||
|
||||
#endif
|
||||
|
||||
//mettre la librairie en static pour ne pas avoir de probleme de compilation
|
||||
Executable
BIN
Binary file not shown.
Executable
+70
@@ -0,0 +1,70 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/20 14:34:32 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:10:24 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "get_next_line.h"
|
||||
|
||||
size_t gnl_strlen(char *s)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (0);
|
||||
while (s[i] != '\0')
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *gnl_strchr(char *s, int c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (0);
|
||||
if (c == '\0')
|
||||
return ((char *)&s[gnl_strlen(s)]);
|
||||
while (s[i] != '\0')
|
||||
{
|
||||
if (s[i] == (char) c)
|
||||
return ((char *)&s[i]);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *gnl_strjoin(char *s1, char *s2)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char *str;
|
||||
|
||||
if (!s1)
|
||||
{
|
||||
s1 = (char *)malloc(1 * sizeof(char));
|
||||
s1[0] = '\0';
|
||||
}
|
||||
if (!s1 || !s2)
|
||||
return (NULL);
|
||||
str = malloc(sizeof(char) * ((gnl_strlen(s1) + gnl_strlen(s2)) + 1));
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
j = 0;
|
||||
if (s1)
|
||||
while (s1[++i] != '\0')
|
||||
str[i] = s1[i];
|
||||
while (s2[j] != '\0')
|
||||
str[i++] = s2[j++];
|
||||
str[gnl_strlen(s1) + gnl_strlen(s2)] = '\0';
|
||||
free(s1);
|
||||
return (str);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user