Create shareable filestream_getline

This commit is contained in:
twinaphex 2017-12-10 22:35:08 +01:00
parent 2e979ec537
commit 2fd8210e59
3 changed files with 38 additions and 74 deletions

View File

@ -80,42 +80,6 @@ struct config_file
static config_file_t *config_file_new_internal(
const char *path, unsigned depth);
static char *getaline(RFILE *file)
{
char* newline = (char*)malloc(9);
char* newline_tmp = NULL;
size_t cur_size = 8;
size_t idx = 0;
int in = filestream_getc(file);
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;
}
/* ignore MS line endings */
if (in != '\r')
newline[idx++] = in;
in = filestream_getc(file);
}
newline[idx] = '\0';
return newline;
}
static char *strip_comment(char *str)
{
/* Remove everything after comment.
@ -419,7 +383,7 @@ static config_file_t *config_file_new_internal(
list->value = NULL;
list->next = NULL;
line = getaline(file);
line = filestream_getline(file);
if (!line)
{

View File

@ -25,11 +25,13 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <sys/types.h>
#include <retro_common_api.h>
#include <retro_inline.h>
#include <boolean.h>
#include <stdarg.h>
@ -84,8 +86,6 @@ 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);
int filestream_eof(RFILE *stream);
@ -107,6 +107,41 @@ FILE* filestream_get_fp(RFILE *stream); */
int filestream_flush(RFILE *stream);
static INLINE 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;
}
RETRO_END_DECLS
#endif

View File

@ -290,41 +290,6 @@ 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)