Skip to content

Commit

Permalink
Create shareable filestream_getline
Browse files Browse the repository at this point in the history
  • Loading branch information
inactive123 committed Dec 10, 2017
1 parent 2e979ec commit 2fd8210
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 74 deletions.
38 changes: 1 addition & 37 deletions libretro-common/file/config_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
{
Expand Down
39 changes: 37 additions & 2 deletions libretro-common/include/streams/file_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand All @@ -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
35 changes: 0 additions & 35 deletions libretro-common/streams/file_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,41 +290,6 @@ RFILE *filestream_open(const char *path, unsigned mode, unsigned hints)
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)
Expand Down

0 comments on commit 2fd8210

Please sign in to comment.