Add string reader util

This commit is contained in:
bnnm 2017-08-12 11:14:16 +02:00
parent 561048e613
commit c5dbece5ac
2 changed files with 39 additions and 23 deletions

View File

@ -344,6 +344,28 @@ size_t get_streamfile_dos_line(int dst_length, char * dst, off_t offset,
}
/* reads a c-string, up to maxsize or NULL, returning size. buf is optional. */
int read_string(char * buf, size_t maxsize, off_t offset, STREAMFILE *streamFile) {
int i;
for (i=0; i < maxsize; i++) {
char c = read_8bit(offset + i, streamFile);
if (buf) buf[i] = c;
if (c == '\0')
return i;
if (i+1 == maxsize) { /* null at maxsize and don't validate (expected to be garbage) */
if (buf) buf[i] = '\0';
return maxsize;
}
if (c < 0x20 || c > 0xA5)
goto fail;
}
fail:
if (buf) buf[0] = '\0';
return 0;
}
/**
* Opens an stream using the base streamFile name plus a new extension (ex. for headers in a separate file)
*/

View File

@ -42,34 +42,39 @@
#endif
#endif
/* struct representing a file with callbacks. Code should use STREAMFILEs and not std C functions
* to do file operations, as plugins may need to provide their own callbacks. */
typedef struct _STREAMFILE {
size_t (*read)(struct _STREAMFILE *,uint8_t * dest, off_t offset, size_t length);
size_t (*get_size)(struct _STREAMFILE *);
off_t (*get_offset)(struct _STREAMFILE *);
// for dual-file support
/* for dual-file support */
void (*get_name)(struct _STREAMFILE *,char *name,size_t length);
// for when the "name" is encoded specially, this is the actual user
// visible name
/* for when the "name" is encoded specially, this is the actual user visible name */
void (*get_realname)(struct _STREAMFILE *,char *name,size_t length);
struct _STREAMFILE * (*open)(struct _STREAMFILE *,const char * const filename,size_t buffersize);
void (*close)(struct _STREAMFILE *);
#ifdef PROFILE_STREAMFILE
size_t (*get_bytes_read)(struct _STREAMFILE *);
int (*get_error_count)(struct _STREAMFILE *);
#endif
} 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);
/* 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) {
streamfile->close(streamfile);
}
/* read from a file
*
* returns number of bytes read
*/
/* read from a file, returns number of bytes read */
static inline size_t read_streamfile(uint8_t * dest, off_t offset, size_t length, STREAMFILE * streamfile) {
return streamfile->read(streamfile,dest,offset,length);
}
@ -132,26 +137,15 @@ static inline int8_t read_8bit(off_t offset, STREAMFILE * streamfile) {
return buf[0];
}
/* open file with a set buffer size, create a STREAMFILE object
*
* Returns pointer to new STREAMFILE or NULL if open failed
*/
STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t buffersize);
/* open file with a default buffer size, create a STREAMFILE object
*
* Returns pointer to new STREAMFILE or NULL if open failed
*/
static inline STREAMFILE * open_stdio_streamfile(const char * const filename) {
return open_stdio_streamfile_buffer(filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
}
/* various STREAMFILE helpers functions */
size_t get_streamfile_dos_line(int dst_length, char * dst, off_t offset, STREAMFILE * infile, int *line_done_ptr);
STREAMFILE * open_stream_ext(STREAMFILE *streamFile, const char * ext);
int read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile);
int read_string(char * buf, size_t bufsize, off_t offset, STREAMFILE *streamFile);
int read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile);
int read_pos_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile);
int check_extensions(STREAMFILE *streamFile, const char * cmp_exts);