Unify open_stdio usage and add opening by FILE

This commit is contained in:
bnnm 2017-08-13 19:58:01 +02:00
parent 88c8bd4916
commit f4f920482a
2 changed files with 25 additions and 12 deletions

View File

@ -12,7 +12,7 @@
#define STREAMFILE_IGNORE_EOF 0
/* buffered file reader */
/* a STREAMFILE that operates via standard IO using a buffer */
typedef struct {
STREAMFILE sf; /* callbacks */
FILE * infile; /* actual FILE */
@ -31,7 +31,8 @@ typedef struct {
#endif
} STDIOSTREAMFILE;
static STREAMFILE * open_stdio_streamfile_buffer_by_FILE(FILE *infile,const char * const filename, size_t buffersize);
static STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t buffersize);
static STREAMFILE * open_stdio_streamfile_buffer_by_file(FILE *infile,const char * const filename, size_t buffersize);
static size_t read_the_rest(uint8_t * dest, off_t offset, size_t length, STDIOSTREAMFILE * streamfile) {
size_t length_read_total=0;
@ -211,12 +212,13 @@ static STREAMFILE *open_stdio(STDIOSTREAMFILE *streamFile,const char * const fil
if (!filename)
return NULL;
// if same name, duplicate the file pointer we already have open
if (!strcmp(streamFile->name,filename)) {
if (((newfd = dup(fileno(streamFile->infile))) >= 0) &&
(newfile = fdopen( newfd, "rb" )))
{
newstreamFile = open_stdio_streamfile_buffer_by_FILE(newfile,filename,buffersize);
newstreamFile = open_stdio_streamfile_buffer_by_file(newfile,filename,buffersize);
if (newstreamFile) {
return newstreamFile;
}
@ -228,7 +230,7 @@ static STREAMFILE *open_stdio(STDIOSTREAMFILE *streamFile,const char * const fil
return open_stdio_streamfile_buffer(filename,buffersize);
}
static STREAMFILE * open_stdio_streamfile_buffer_by_FILE(FILE *infile,const char * const filename, size_t buffersize) {
static STREAMFILE * open_stdio_streamfile_buffer_by_file(FILE *infile,const char * const filename, size_t buffersize) {
uint8_t * buffer;
STDIOSTREAMFILE * streamfile;
@ -269,14 +271,14 @@ static STREAMFILE * open_stdio_streamfile_buffer_by_FILE(FILE *infile,const char
return &streamfile->sf;
}
STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t buffersize) {
static STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t buffersize) {
FILE * infile;
STREAMFILE *streamFile;
infile = fopen(filename,"rb");
if (!infile) return NULL;
streamFile = open_stdio_streamfile_buffer_by_FILE(infile,filename,buffersize);
streamFile = open_stdio_streamfile_buffer_by_file(infile,filename,buffersize);
if (!streamFile) {
fclose(infile);
}
@ -284,6 +286,18 @@ STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t bu
return streamFile;
}
STREAMFILE * open_stdio_streamfile(const char * filename) {
return open_stdio_streamfile_buffer(filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
}
STREAMFILE * open_stdio_streamfile_by_file(FILE * file, const char * filename) {
return open_stdio_streamfile_buffer_by_file(file,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
}
/* **************************************************** */
/* Read a line into dst. The source files are MS-DOS style,
* separated (not terminated) by CRLF. Return 1 if the full line was
* retrieved (if it could fit in dst), 0 otherwise. In any case the result

View File

@ -67,13 +67,12 @@ typedef struct _STREAMFILE {
} STREAMFILE;
/* open file with a set buffer size, create a STREAMFILE object or return NULL if open failed */
STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t buffersize);
/* create a STREAMFILE from path */
STREAMFILE * open_stdio_streamfile(const char * filename);
/* create a STREAMFILE from pre-opened file path */
STREAMFILE * open_stdio_streamfile_by_file(FILE * file, const char * filename);
/* open file with a default buffer size, create a STREAMFILE object or return NULL if open failed */
static inline STREAMFILE * open_stdio_streamfile(const char * const filename) {
return open_stdio_streamfile_buffer(filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
}
/* close a file, destroy the STREAMFILE object */
static inline void close_streamfile(STREAMFILE * streamfile) {