Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include "get_next_line.h"
  2.  
  3. int next_line(char **str, char **line, int fd)
  4. {
  5. char *tmp;
  6.  
  7. if (ft_strchr(str[fd], '\n'))
  8. {
  9. tmp = ft_strdup(ft_strchr(str[fd], '\n') + 1);
  10. *line = ft_strsub(str[fd], 0, ft_strchr(str[fd], '\n') - str[fd]);
  11. ft_strdel(&str[fd]);
  12. str[fd] = tmp;
  13. return (1);
  14. }
  15. else if (str[fd][0])
  16. {
  17. *line = ft_strdup(str[fd]);
  18. ft_strdel(&str[fd]);
  19. return (1);
  20. }
  21. else
  22. return (0);
  23. }
  24.  
  25. int get_next_line(const int fd, char **line)
  26. {
  27. static char *str[10000];
  28. char buff[BUFF_SIZE + 1];
  29. char *tmp;
  30. int ret;
  31.  
  32. if (fd < 0 && !line)
  33. return (-1);
  34. while ((ret = read(fd, buff, BUFF_SIZE)) > 0)
  35. {
  36. buff[ret] = '\0';
  37. if (!str[fd])
  38. str[fd] = ft_strnew(1);
  39. tmp = ft_strjoin(str[fd], buff);
  40. free(str[fd]);
  41. str[fd] = tmp;
  42. if (ft_strchr(str[fd], '\n'))
  43. break ;
  44. }
  45. if (ret < 0)
  46. return (-1);
  47. if (!ret && !str[fd])
  48. return (0);
  49. return (next_line(str, line, fd));
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement