/ push_swap / push_swap / get_next_line_bonus.c
get_next_line_bonus.c
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   get_next_line.c                                    :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2022/12/04 18:11:51 by gychoi            #+#    #+#             */
  9  /*   Updated: 2022/12/07 20:05:35 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "get_next_line_bonus.h"
 14  
 15  size_t	gnl_strlen(char *str)
 16  {
 17  	size_t	i;
 18  
 19  	i = 0;
 20  	while (str[i] != '\0')
 21  		i++;
 22  	return (i);
 23  }
 24  
 25  size_t	search_newline(char *line)
 26  {
 27  	size_t	i;
 28  
 29  	i = 0;
 30  	while (line[i] != '\0')
 31  	{
 32  		if (line[i] == '\n')
 33  			return (i + 1);
 34  		i++;
 35  	}
 36  	return (0);
 37  }
 38  
 39  char	*make_one_line(t_gnl *node)
 40  {
 41  	char	*string;
 42  	char	*temp;
 43  	size_t	len;
 44  	size_t	i;
 45  
 46  	len = search_newline(node->backup);
 47  	if (len < 1)
 48  		len = gnl_strlen(node->backup);
 49  	string = malloc(sizeof(char) * len + 1);
 50  	if (string == NULL)
 51  		return (NULL);
 52  	temp = node->backup;
 53  	i = 0;
 54  	while (i < len)
 55  		string[i++] = *(node->backup)++;
 56  	string[i] = '\0';
 57  	node->backup = gnl_strdup(node->backup);
 58  	free(temp);
 59  	if (node->backup == NULL)
 60  	{
 61  		free(string);
 62  		return (NULL);
 63  	}
 64  	return (string);
 65  }
 66  
 67  char	*get_readline(t_gnl *node)
 68  {
 69  	char	buffer[BUFFER_SIZE + 1];
 70  	char	*temp;
 71  	char	*line;
 72  	ssize_t	readout;
 73  
 74  	readout = read(node->fd, buffer, BUFFER_SIZE);
 75  	if (readout < 0)
 76  		return (NULL);
 77  	temp = gnl_strdup(node->backup);
 78  	if (temp == NULL)
 79  		return (NULL);
 80  	while (readout)
 81  	{
 82  		buffer[readout] = '\0';
 83  		line = gnl_strjoin(temp, buffer);
 84  		if (line == NULL)
 85  			return (NULL);
 86  		free(temp);
 87  		if (search_newline(line))
 88  			return (line);
 89  		temp = line;
 90  		readout = read(node->fd, buffer, BUFFER_SIZE);
 91  	}
 92  	return (temp);
 93  }
 94  
 95  char	*get_next_line(int fd)
 96  {
 97  	static t_gnl		*head;
 98  	t_gnl				*node;
 99  	char				*line;
100  	char				*temp;
101  
102  	if (fd < 0 || BUFFER_SIZE < 1)
103  		return (NULL);
104  	node = gnl_lstset(&head, fd);
105  	if (node == NULL)
106  		return ((char *)gnl_lstclear(&head, fd));
107  	temp = node->backup;
108  	node->backup = get_readline(node);
109  	free(temp);
110  	if (node->backup == NULL)
111  		return ((char *)gnl_lstclear(&head, fd));
112  	if (gnl_strlen(node->backup) < 1)
113  		return ((char *)gnl_lstclear(&head, fd));
114  	line = make_one_line(node);
115  	if (line == NULL)
116  		return ((char *)gnl_lstclear(&head, fd));
117  	return (line);
118  }