Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   get_next_line.c                                    :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: asaboure <marvin@42.fr>                    +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2019/12/10 16:26:02 by asaboure          #+#    #+#             */
  9. /*   Updated: 2019/12/12 16:50:00 by asaboure         ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include "get_next_line.h"
  14. #include <sys/types.h>
  15. #include <sys/uio.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18.  
  19. #include <stdio.h>
  20.  
  21. int     is_nl(char *str)
  22. {
  23.     size_t  i;
  24.  
  25.     i = 0;
  26.     while (str[i++])
  27.         if (str[i] == '\n')
  28.             return (1);
  29.     return (0);
  30. }
  31.  
  32. char    *ft_next(char *result)
  33. {
  34.     size_t  i;
  35.  
  36.     i = 0;
  37.     while (result[i] && result[i] != '\n')
  38.         i++;
  39.     return (result + i + 1);
  40. }
  41.  
  42. int     get_next_line(int fd, char **line)
  43. {
  44.     static char *result = NULL;
  45.     char        buf[BUFFER_SIZE + 1];
  46.     int         i;
  47.  
  48.     i = 1;
  49.     if (result == NULL)
  50.     {
  51.         if (!(result = (char *)malloc(1)))
  52.             return (-1);
  53.         result[0] = '\0';
  54.     }
  55.     while (i > 0 && is_nl(result) == 0)
  56.     {
  57.         i = read(fd, buf, BUFFER_SIZE);
  58.         buf[i] = '\0';
  59.         result = ft_strjoin(result, buf);
  60.     }
  61.     if ((result[0] == '\0' && i == 0) || i == -1)
  62.     {
  63.         free(result);
  64.         return (i);
  65.     }
  66.     *line = ft_strdup_nl(result);
  67.     result = ft_next(result);
  68.     return (1);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement