From 2fd8210e597c512375ddc6c95ad46fe36855aa75 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 10 Dec 2017 22:35:08 +0100 Subject: [PATCH] Create shareable filestream_getline --- libretro-common/file/config_file.c | 38 +----------------- libretro-common/include/streams/file_stream.h | 39 ++++++++++++++++++- libretro-common/streams/file_stream.c | 35 ----------------- 3 files changed, 38 insertions(+), 74 deletions(-) diff --git a/libretro-common/file/config_file.c b/libretro-common/file/config_file.c index b4d7251d1a..c7b6a92f24 100644 --- a/libretro-common/file/config_file.c +++ b/libretro-common/file/config_file.c @@ -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) { diff --git a/libretro-common/include/streams/file_stream.h b/libretro-common/include/streams/file_stream.h index fde5b36a4c..c1b6fe951a 100644 --- a/libretro-common/include/streams/file_stream.h +++ b/libretro-common/include/streams/file_stream.h @@ -25,11 +25,13 @@ #include #include +#include #include #include #include +#include #include #include @@ -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 diff --git a/libretro-common/streams/file_stream.c b/libretro-common/streams/file_stream.c index 4e601e31f8..845e12c348 100644 --- a/libretro-common/streams/file_stream.c +++ b/libretro-common/streams/file_stream.c @@ -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)