Created
May 16, 2016 16:06
-
-
Save mouuff/000c2bbb7c29bc814ee91758ed74e47b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** get_next_line.c for get_next_line in /home/alies/rendu/CPE_2015_getnextline | |
** | |
** Made by Arnaud Alies | |
** Login <alies_a@epitech.net> | |
** | |
** Started on Thu Dec 17 13:44:58 2015 Arnaud Alies | |
** Last update Sat Apr 2 12:48:17 2016 alies_a | |
*/ | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include "my.h" | |
int append_next(char **next, int *next_size, | |
const char *buff, const int buff_size) | |
{ | |
char *res; | |
int x; | |
if ((res = malloc(sizeof(char) * (buff_size + *next_size + 1))) == NULL) | |
return (1); | |
x = 0; | |
while (x < *next_size) | |
{ | |
res[x] = (*next)[x]; | |
x += 1; | |
} | |
x = 0; | |
while (x < buff_size) | |
{ | |
res[*next_size] = buff[x]; | |
*next_size += 1; | |
x += 1; | |
} | |
res[*next_size] = '\0'; | |
if (*next != NULL) | |
free(*next); | |
*next = res; | |
return (0); | |
} | |
char *dump_next(char **next, int *next_size, int x) | |
{ | |
char *res; | |
char *new_next; | |
int size; | |
size = *next_size - x; | |
if ((new_next = malloc(sizeof(char) * (size))) == NULL) | |
return (NULL); | |
if ((res = malloc(sizeof(char) * (x + 1))) == NULL) | |
return (NULL); | |
*next_size = 0; | |
while (*next_size < x) | |
{ | |
res[*next_size] = (*next)[*next_size]; | |
*next_size += 1; | |
} | |
res[*next_size - 1] = '\0'; | |
*next_size = 0; | |
while (*next_size < size) | |
{ | |
new_next[*next_size] = (*next)[x + *next_size]; | |
*next_size += 1; | |
} | |
free(*next); | |
*next = new_next; | |
return (res); | |
} | |
int is_line(int set) | |
{ | |
static int is_line = 1; | |
if (set == -1) | |
is_line = 0; | |
if (set == 1) | |
is_line = 1; | |
return (is_line); | |
} | |
char *get_line(char **next, int *next_size) | |
{ | |
int x; | |
x = 0; | |
while (x < *next_size) | |
{ | |
if ((*next)[x] == '\n' || (*next)[x] == ';') | |
{ | |
is_line((*next)[x] == '\n' ? 1 : -1); | |
return (dump_next(next, next_size, x + 1)); | |
} | |
x += 1; | |
} | |
return (NULL); | |
} | |
char *get_next_line(const int fd, char **next, int *size) | |
{ | |
char buff[READ_SIZE]; | |
int r; | |
char *res; | |
if (fd < 0 || fd > 99) | |
return (NULL); | |
while ((res = get_line(next, size)) == NULL) | |
{ | |
if ((r = read(fd, buff, READ_SIZE)) == -1) | |
return (NULL); | |
if (append_next(next, size, buff, r)) | |
return (NULL); | |
if (r <= 0) | |
{ | |
if (*size > 0) | |
{ | |
*size += 1; | |
return (dump_next(next, size, *size)); | |
} | |
return (NULL); | |
} | |
} | |
return (res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment