5#if !defined( DEBUG_THRESHOLD )
6# define DEBUG_THRESHOLD 1
9#ifndef UPRINTF_BUFFER_SZ_MAX
10# define UPRINTF_BUFFER_SZ_MAX 1024
13#if !defined( MFILE_MMAP ) && \
14 !defined( RETROFLAT_API_WINCE ) && \
15 !defined( RETROFLAT_API_PALM )
33#ifndef MAUG_PATH_SZ_MAX
35# define MAUG_PATH_SZ_MAX 256
50#define MFILE_CADDY_TYPE_FILE 0x01
55#define MFILE_CADDY_TYPE_MEM_BUFFER 0x80
59#define MFILE_FLAG_READ_ONLY 0x01
74#define MFILE_READ_FLAG_LSBF 0x01
81#define MFILE_READ_FLAG_MSBF 0x01
83#define MFILE_ASSIGN_FLAG_TRIM_EXT 0x01
85#ifndef MFILE_TRACE_LVL
86# define MFILE_TRACE_LVL 0
89#ifndef MFILE_TRACE_CONTENTS_LVL
90# define MFILE_TRACE_CONTENTS_LVL 0
108#define mfile_cmp_path( a, b ) strncmp( a, b, MAUG_PATH_SZ_MAX )
115typedef off_t (*mfile_has_bytes_t)(
struct MFILE_CADDY* p_file );
119 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
122 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz, uint8_t flags );
124 struct MFILE_CADDY* p_file,
char* buf, off_t buf_sz, uint8_t flags );
126 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, ... );
128off_t mfile_mem_has_bytes(
struct MFILE_CADDY* p_file );
131 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
134 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags );
136 mfile_t* p_file, uint8_t flags,
const char* fmt, va_list args );
146 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, va_list args );
156 union MFILE_HANDLE
h;
164 mfile_has_bytes_t has_bytes;
165 mfile_read_byte_t read_byte;
166 mfile_read_block_t read_block;
168 mfile_read_int_t read_int;
169 mfile_read_line_t read_line;
170 mfile_printf_t printf;
186off_t mfile_file_has_bytes(
struct MFILE_CADDY* p_file );
191 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
194 struct MFILE_CADDY* p_f, uint8_t* buf,
size_t buf_sz, uint8_t flags );
199 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags );
202 struct MFILE_CADDY* p_f, uint8_t flags,
const char* fmt, ... );
205 struct MFILE_CADDY* p_f, uint8_t flags,
const char* fmt, va_list args );
207#define mfile_check_lock( p_file ) (NULL != (p_file)->mem_buffer)
209#define mfile_get_sz( p_file ) ((p_file)->sz)
222MERROR_RETVAL mfile_open_write(
const char* filename, mfile_t* p_file );
238 char* ext_ptr = NULL;
242 if( MFILE_ASSIGN_FLAG_TRIM_EXT == (MFILE_ASSIGN_FLAG_TRIM_EXT & flags) ) {
243 ext_ptr = maug_strrchr( tgt,
'.' );
244 if( NULL != ext_ptr ) {
255 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz, uint8_t flags
262#elif defined( MAUG_MSBF )
266 debug_printf( MFILE_TRACE_LVL,
"reading integer forward" );
268 retval = p_file->read_block( p_file, buf, buf_sz );
271 debug_printf( MFILE_TRACE_LVL,
"reading integer reversed" );
273 while( 0 < buf_sz ) {
274 retval = p_file->read_byte( p_file, (buf + (buf_sz - 1)) );
275 maug_cleanup_if_not_ok();
288 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, ...
293 va_start( vargs, fmt );
294 retval = p_file->vprintf( p_file, flags, fmt, vargs );
302off_t mfile_mem_has_bytes(
struct MFILE_CADDY* p_file ) {
309 return p_file->read_block( p_file, buf, 1 );
315 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz
322 while( 0 < buf_sz-- ) {
338 debug_printf( MFILE_TRACE_LVL,
339 "seeking memory buffer to position " OFF_T_FMT
" (" OFF_T_FMT
")",
348 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags
355 while( i < buffer_sz - 1 && p_f->has_bytes( p_f ) ) {
357 if( i + 1 >= buffer_sz ) {
358 error_printf(
"overflow reading string from file!" );
359 retval = MERROR_FILE;
363 p_f->read_int( p_f, (uint8_t*)&(buffer[i]), 1, 0 );
364 if(
'\n' == buffer[i] ) {
371 assert( i < buffer_sz );
382 mfile_t* p_file, uint8_t flags,
const char* fmt, va_list args
386 if( MFILE_FLAG_READ_ONLY == (MFILE_FLAG_READ_ONLY & p_file->flags) ) {
391 error_printf(
"writing to memory buffer not currently supported!" );
399 MAUG_MHANDLE handle, off_t handle_sz, mfile_t* p_file
404 "locking handle %p as file %p (" OFF_T_FMT
" bytes)...",
405 handle, p_file, handle_sz );
407 maug_mzero( p_file,
sizeof(
struct MFILE_CADDY ) );
411 p_file->has_bytes = mfile_mem_has_bytes;
412 p_file->read_byte = mfile_mem_read_byte;
413 p_file->read_block = mfile_mem_read_block;
414 p_file->read_int = mfile_file_read_int;
415 p_file->seek = mfile_mem_seek;
416 p_file->read_line = mfile_mem_read_line;
418 p_file->sz = handle_sz;
429 retval = mfile_plt_open_read( filename, p_file );
436MERROR_RETVAL mfile_open_write(
const char* filename, mfile_t* p_file ) {
439 retval = mfile_plt_open_write( filename, p_file );
448 munmap( bytes_ptr_h, bytes_sz );
451 switch( p_file->
type ) {
457 mfile_plt_close( p_file );
464 debug_printf( 1,
"unlocked handle %p from file %p...",
465 p_file->
h.mem, p_file );
471 error_printf(
"unknown file type: %d", (p_file)->
type );
int MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19
#define MFILE_CADDY_TYPE_FILE
A standard UNIX file.
Definition mfile.h:50
#define MFILE_CADDY_TYPE_MEM_BUFFER
An array of bytes in memory abstracted through this library.
Definition mfile.h:55
#define MAUG_PATH_SZ_MAX
Maximum size allocated for asset paths.
Definition mfile.h:35
#define MFILE_READ_FLAG_MSBF
Flag for mfile_read_int_t() indicating integer should always be read most significant byte first.
Definition mfile.h:81
MERROR_RETVAL(* mfile_vprintf_t)(struct MFILE_CADDY *p_file, uint8_t flags, const char *fmt, va_list args)
Callback to printf the given format string, replacing tokens from the providied pre-initialized list ...
Definition mfile.h:145
#define MFILE_READ_FLAG_LSBF
Flag for mfile_read_int_t() indicating integer should always be read least significant byte first.
Definition mfile.h:74
MERROR_RETVAL mfile_open_read(const char *filename, mfile_t *p_file)
Open a file and read it into memory or memory-map it.
void mfile_close(mfile_t *p_file)
Close a file opened with mfile_open_read().
MERROR_RETVAL mfile_lock_buffer(MAUG_MHANDLE, off_t, mfile_t *p_file)
Lock a buffer and assign it to an mfile_t to read/write.
char retroflat_asset_path[MAUG_PATH_SZ_MAX+1]
Path/name used to load an asset from disk.
Definition mfile.h:103
off_t mem_cursor
Current position if its type is MFILE_CADDY_TYPE_MEM_BUFFER.
Definition mfile.h:160
uint8_t type
The RetroFile Types flag describing this file.
Definition mfile.h:154
union MFILE_HANDLE h
The physical handle or pointer to access the file by.
Definition mfile.h:156
uint8_t * mem_buffer
Locked pointer for MFILE_HANDLE::mem.
Definition mfile.h:162