<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   get_next_line.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: pstrait <<EMAIL>>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/12/03 17:58:18 by pstrait           #+#    #+#             */
/*   Updated: 2020/12/24 19:05:01 by pstrait          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "get_next_line.h"

char	*ft_strchr(const char *s, int c)
{
	char	*str;

	str = (char *)s;
	while (*str != c)
	{
		if (*str == '\0')
		{
			return (NULL);
		}
		str++;
	}
	return (str);
}

int		check_pref_buff(char **prev_buff, char **line)
{
	char	*pos;
	char	*p_prev_buff;

	if (*prev_buff)
	{
		free(*line);
		if ((pos = ft_strchr(*prev_buff, '\n')))
		{
			*pos = '\0';
			*line = ft_strdup(*prev_buff);
			p_prev_buff = *prev_buff;
			pos++;
			if (ft_strlen(pos))
			{
				*prev_buff = ft_strdup(pos);
				free(p_prev_buff);
			}
			else
				*prev_buff = NULL;
			return (1);
		}
		*line = ft_strdup(*prev_buff);
		free(*prev_buff);
		*prev_buff = NULL;
	}
	return (0);
}

int		read_file(int fd, char *buff, char **line, char **prev_buff)
{
	char		*pos;
	int			ret;
	char		*tmp_buff;

	while ((ret = read(fd, buff, BUFFER_SIZE)) > 0)
	{
		buff[ret] = '\0';
		if ((pos = ft_strchr(buff, '\n')))
		{
			*pos = '\0';
			tmp_buff = *line;
			*line = ft_strjoin(*line, buff);
			if (++pos < &buff[ret])
				*prev_buff = ft_strdup(pos);
			free(tmp_buff);
			free(buff);
			return (1);
		}
		tmp_buff = *line;
		*line = ft_strjoin(*line, buff);
		free(tmp_buff);
	}
	free(buff);
	return (ret < 0 ? -1 : 0);
}

int		get_next_line(int fd, char **line)
{
	static char *prev_buff = NULL;
	char		*buff;
	int			ret_code;

	if (fd < 0 || !line || BUFFER_SIZE < 1)
		return (-1);
	*line = malloc(1);
	**line = '\0';
	if (check_pref_buff(&prev_buff, line) == 1)
		return (1);
	buff = malloc(sizeof(char*) * (BUFFER_SIZE) + 1);
	if (!buff)
	{
		free(*line);
		*line = NULL;
		return (-1);
	}
	ret_code = read_file(fd, buff, line, &prev_buff);
	return (ret_code);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   get_next_line_utils.c                              :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: pstrait <<EMAIL>>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/12/09 18:37:22 by pstrait           #+#    #+#             */
/*   Updated: 2020/12/24 19:05:07 by pstrait          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "get_next_line.h"

size_t	ft_strlen(const char *s)
{
	size_t count;

	count = 0;
	while (*s != '\0')
	{
		s++;
		count++;
	}
	return (count);
}

size_t	ft_strlcat(char *dst, const char *src, size_t dstsize)
{
	size_t dst_size;
	size_t src_size;
	size_t total_string_size;
	size_t size;

	dst_size = ft_strlen(dst);
	src_size = ft_strlen(src);
	total_string_size = dstsize;
	if (!dst || !src)
		return ((size_t)NULL);
	if (total_string_size <= dst_size)
		return (total_string_size + src_size);
	dst += dst_size;
	size = total_string_size - dst_size - 1;
	while (size-- && *src)
	{
		*dst = *src;
		src++;
		dst++;
	}
	*dst = '\0';
	return (dst_size + src_size);
}

size_t	ft_strlcpy(char *restrict dst, const char *restrict src,
		size_t dstsize)
{
	size_t			i;
	unsigned char	*psrc;
	unsigned char	*pdst;

	i = 0;
	psrc = (unsigned char *)src;
	pdst = (unsigned char *)dst;
	if (dstsize < 1)
		return (ft_strlen(src));
	if (!dst || !src)
		return ((size_t)NULL);
	while (dstsize - 1 && psrc[i])
	{
		pdst[i] = psrc[i];
		dstsize--;
		i++;
	}
	pdst[i] = '\0';
	return (ft_strlen(src));
}

char	*ft_strjoin(char const *s1, char const *s2)
{
	size_t	size_st;
	char	*str;

	if (!s1 || !s2)
		return (NULL);
	size_st = ft_strlen(s1) + ft_strlen(s2) + 1;
	str = malloc(size_st);
	if (str == NULL)
		return (NULL);
	ft_strlcpy(str, s1, size_st);
	ft_strlcat(str, s2, size_st);
	return (str);
}

char	*ft_strdup(const char *s1)
{
	char *str;

	str = malloc(ft_strlen(s1) + 1);
	if (str == NULL)
		return (0);
	ft_strlcpy(str, s1, ft_strlen(s1) + 1);
	return (str);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   get_next_line.h                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: pstrait <<EMAIL>>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/12/03 17:53:13 by pstrait           #+#    #+#             */
/*   Updated: 2020/12/24 19:04:14 by pstrait          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H

# ifndef BUFFER_SIZE
#  define BUFFER_SIZE 5
# endif

# include <unistd.h>
# include <fcntl.h>
# include <stdlib.h>
# include <stdio.h> //delete 

int		get_next_line(int fd, char **line);
size_t	ft_strlen(const char *s);
size_t	ft_strlcat(char *dst, const char *src, size_t dstsize);
size_t	ft_strlcpy(char *restrict dst, const char *restrict src,
		size_t dstsize);
char	*ft_strjoin(char const *s1, char const *s2);
char	*ft_strdup(const char *s1);

#endif
