Browse Source

final

master
Elouan MARTINET 4 years ago
parent
commit
d4ef8babfb
  1. 57
      get_next_line.c
  2. 1
      get_next_line.h
  3. 26
      libft/Makefile
  4. 25
      libft/ft_memcpy.c
  5. 22
      libft/includes/libft.h
  6. 22
      libft/libft.h

57
get_next_line.c

@ -13,44 +13,31 @@
#include <errno.h>
#include "get_next_line.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
char *csrc;
char *cdst;
csrc = (char *)src;
cdst = (char *)dst;
while (n--)
*(cdst++) = *(csrc++);
return (dst);
}
static int ft_copier(struct s_fd *el, int copy, char *bf, char **line)
static int ft_copier(struct s_fd *el, int r, char *bf, char **line)
{
int j;
int r;
j = 0;
while (j < r && bf[j] != '\n')
while (j < (r < 0 ? -r : r) && bf[j] != '\n')
j++;
if (j >= r)
if (j >= (r < 0 ? -r : r))
return (42);
if (!(*line = malloc((copy ? el->n : 0) + j + 1)))
if (!(*line = malloc((r < 0 ? el->n : 0) + j + 1)))
return (-1);
ft_memcpy(*line + (copy ? el->n : 0), bf, j);
(*line)[(copy ? el->n : 0) + j] = '\0';
if (copy)
ft_memcpy(*line + (r < 0 ? el->n : 0), bf, j);
(*line)[(r < 0 ? el->n : 0) + j] = '\0';
if (r < 0)
{
ft_memcpy(*line, el->bf, el->n);
free(el->bf);
el->n = 0;
}
if (j < r - 1)
if (j < (r < 0 ? -r : r) - 1)
{
if (!(el->bf = malloc(r - j - 1 + 1)))
if (!(el->bf = malloc((r < 0 ? -r : r) - j - 1 + 1)))
return (-1);
ft_memcpy(el->bf, bf + j + 1, r - j - 1);
el->n = r - j - 1;
ft_memcpy(el->bf, bf + j + 1, (r < 0 ? -r : r) - j - 1);
el->n = (r < 0 ? -r : r) - j - 1;
}
return (1);
}
@ -70,7 +57,7 @@ static int ft_first_pass(struct s_fd *li, int i, char *bf, char **line)
}
if (r == 0)
return (0);
j = ft_copier(&(li[i]), 0, bf, line);
j = ft_copier(&(li[i]), r, bf, line);
if (j != 42)
return (j);
if (!(li[i].bf = malloc(r + 1)))
@ -80,7 +67,7 @@ static int ft_first_pass(struct s_fd *li, int i, char *bf, char **line)
return (42);
}
static int ft_else_pass(struct s_fd *li, int i, char *bf, char **line)
static int ft_else_pass(struct s_fd *li, int i, char **line)
{
int j;
char *tmp;
@ -103,10 +90,7 @@ static int ft_else_pass(struct s_fd *li, int i, char *bf, char **line)
((li[i].bf = tmp) && (li[i].n -= j - 1));
}
else
{
free(li[i].bf);
li[i].n = 0;
}
free(li[i].bf + (li[i].n = 0));
return (1);
}
@ -114,7 +98,7 @@ static int ft_end_pass(struct s_fd *li, int i, char *bf, char **line)
{
int r;
int j;
char *tmp;
char *tmp;
while (1)
{
@ -123,12 +107,11 @@ static int ft_end_pass(struct s_fd *li, int i, char *bf, char **line)
{
if (r == -1 && errno == EAGAIN)
return (-1);
((*line = li[i].bf) && (li[i].fd = -1) && (li[i].n = 0));
(*line)[li[i].n] = '\0';
free(li[i].bf);
return (1);
((*line = li[i].bf) && (li[i].fd += -1 - li[i].fd));
free(li[i].bf + ((*line)[li[i].n] = '\0'));
return ((li[i].n = 0) + 1);
}
j = ft_copier(&(li[i]), 1, bf, line);
j = ft_copier(&(li[i]), -r, bf, line);
if (j != 42)
return (j);
if (!(tmp = malloc(li[i].n + r + 1)))
@ -160,7 +143,7 @@ int get_next_line(const int fd, char **line)
if (li[i].n == 0)
r = ft_first_pass(li, i, bf, line);
else
r = ft_else_pass(li, i, bf, line);
r = ft_else_pass(li, i, line);
if (r != 42)
return (r);
return (ft_end_pass(li, i, bf, line));

1
get_next_line.h

@ -15,6 +15,7 @@
# include <unistd.h>
# include <stdlib.h>
# include "libft.h"
# ifndef BUFF_SIZE
# define BUFF_SIZE 1

26
libft/Makefile

@ -0,0 +1,26 @@
CFLAGS ?= -Wall -Wextra -Werror
NAME ?= libft.a
SRC := \
ft_memcpy.c
OBJ := $(SRC:.c=.o)
CFLAGS += -MMD -MP
all: $(NAME)
$(NAME): $(OBJ)
ar rc $@ $^
ranlib $@
-include $(OBJ:.o=.d)
clean:
$(RM) $(OBJ) $(OBJ:.o=.d)
fclean: clean
$(RM) $(NAME)
re: fclean all
.PHONY: all clean fclean re

25
libft/ft_memcpy.c

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/07 18:29:55 by emartine #+# #+# */
/* Updated: 2017/11/07 18:29:57 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
char *csrc;
char *cdst;
csrc = (char *)src;
cdst = (char *)dst;
while (n--)
*(cdst++) = *(csrc++);
return (dst);
}

22
libft/includes/libft.h

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/06 18:06:49 by emartine #+# #+# */
/* Updated: 2017/11/06 18:06:50 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
void *ft_memcpy(void *dst, const void *src, size_t n);
#endif

22
libft/libft.h

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/06 18:06:49 by emartine #+# #+# */
/* Updated: 2017/11/06 18:06:50 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
void *ft_memcpy(void *dst, const void *src, size_t n);
#endif
Loading…
Cancel
Save