Create filestream_getline

This commit is contained in:
twinaphex 2016-06-03 07:09:50 +02:00
parent e9cf351c67
commit d40eade52c
3 changed files with 38 additions and 1 deletions

View File

@ -81,7 +81,7 @@ static config_file_t *config_file_new_internal(
static char *getaline(FILE *file)
{
char* newline = (char*)malloc(9);
char* newline = (char*)malloc(9);
char* newline_tmp = NULL;
size_t cur_size = 8;
size_t idx = 0;

View File

@ -65,6 +65,8 @@ int filestream_read_file(const char *path, void **buf, ssize_t *len);
char *filestream_gets(RFILE *stream, char *s, size_t len);
char *filestream_getline(RFILE *stream);
int filestream_getc(RFILE *stream);
bool filestream_write_file(const char *path, const void *data, ssize_t size);

View File

@ -243,6 +243,41 @@ error:
return NULL;
}
char *filestream_getline(RFILE *stream)
{
char* newline = (char*)malloc(9);
char* newline_tmp = NULL;
size_t cur_size = 8;
size_t idx = 0;
int in = filestream_getc(stream);
if (!newline)
return NULL;
while (in != EOF && in != '\n')
{
if (idx == cur_size)
{
cur_size *= 2;
newline_tmp = (char*)realloc(newline, cur_size + 1);
if (!newline_tmp)
{
free(newline);
return NULL;
}
newline = newline_tmp;
}
newline[idx++] = in;
in = filestream_getc(stream);
}
newline[idx] = '\0';
return newline;
}
char *filestream_gets(RFILE *stream, char *s, size_t len)
{
if (!stream)