/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* get_next_line.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/14 02:40:53 by apommier #+# #+# */ /* Updated: 2020/12/16 07:56:59 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include #include #include #include #include #include int ft_strlen(char *s1) { int i; i = 0; if (!s1) return (0); while (s1[i]) i++; return (i); } char *ft_strjoin(char *save, char *s2) { char *dest; int i; int j; i = 0; j = 0; if (!save && !s2) return(0); dest = (char*)malloc(sizeof(char) * (ft_strlen(save) + ft_strlen(s2) + 1)); if (!dest) return (0); while (save && save[i]) { dest[j] = save[i]; j++; i++; } i = 0; while (s2 && s2[i]) { dest[j] = s2[i]; j++; i++; } dest[j] = 0; return (dest); } static char *up_save(char **save, int fd, int *end) { char *dest; char *delete; int i; dest = 0; delete = *save; if (!(dest = (char*)malloc(1 * (1 + BUFFER_SIZE)))) { if (*save) free(*save); return (0); } i = read(fd, dest, BUFFER_SIZE); *end = i; dest[i] = 0; if (!(*save = ft_strjoin(*save, dest))) { free(dest); free(delete); return (0); } return (*save); } int is_line(char *save, int *end) { int i; i = 0; if (save == 0) return (0); if (*end < BUFFER_SIZE) return (1); while (save[i]) { if (save[i] == '\n') { save[i] = 0; return (1); } i++; } return (0); } char *is_next_line(char **save, int *end, int fd) { char *delete; int i; i = 0; delete = *save; if (*save) { while ((*save)[i]) i++; i++; if (!(*save = ft_strjoin(*save + i, 0))) { free(delete); return (0); } } while (!is_line(*save, end)) { up_save(save, fd, end); if (!(*save)) { free(delete); return (0); } } free(delete); return (*save); } int get_next_line(int fd, char **line) { int j; int *end; static char **save; if (!save) { if(!(save = malloc(sizeof(char*)))) return (-1); *save = 0; } end = &j; j = BUFFER_SIZE; if (fd < 0 || !line || BUFFER_SIZE < 1) return (-1); is_next_line(save, end, fd); if (!(*save)) return (-1); *line = ft_strjoin(*save, 0); if (*end < BUFFER_SIZE) return (0); return (1); } int main(int argc, char **argv) { int fd; int ret; char *buff; int line; line = 0; if (argc == 2) { fd = open(argv[1], O_RDONLY); while ((ret = get_next_line(fd, &buff)) > 0) { printf("[Return: %d] Line ->%d : '%s'\n", ret, ++line, buff); free(buff); } printf("[Return: %d] Line ->%d : %s\n", ret, ++line, buff); close(fd); } if (argc == 1) { printf("No input file, read here\n"); while ((ret = get_next_line(0, &buff)) > 0) { printf("\033[38;5;2m" "[Return: %d] Line ->%d: %s\n", ret, ++line, buff); free(buff); } close(fd); return (0); } }