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
63#define MFILE_FLAG_READ_ONLY 0x01
71#define MFILE_FLAG_HANDLE_LOCKED 0x02
92#define MFILE_READ_FLAG_LSBF 0x01
99#define MFILE_READ_FLAG_MSBF 0x01
101#define MFILE_ASSIGN_FLAG_TRIM_EXT 0x01
103#ifndef MFILE_READ_TRACE_LVL
104# define MFILE_READ_TRACE_LVL 0
107#ifndef MFILE_WRITE_TRACE_LVL
108# define MFILE_WRITE_TRACE_LVL 0
111#ifndef MFILE_SEEK_TRACE_LVL
112# define MFILE_SEEK_TRACE_LVL 0
115#ifndef MFILE_CONTENTS_TRACE_LVL
116# define MFILE_CONTENTS_TRACE_LVL 0
134#define mfile_cmp_path( a, b ) strncmp( a, b, MAUG_PATH_SZ_MAX )
141typedef off_t (*mfile_cursor_t)(
struct MFILE_CADDY* p_file );
142typedef off_t (*mfile_has_bytes_t)(
struct MFILE_CADDY* p_file );
146 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
149 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz, uint8_t flags );
151 struct MFILE_CADDY* p_file,
char* buf, off_t buf_sz, uint8_t flags );
153 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, ... );
155 struct MFILE_CADDY* p_f,
const uint8_t* buf,
size_t buf_sz );
157off_t mfile_mem_cursor(
struct MFILE_CADDY* p_file );
158off_t mfile_mem_has_bytes(
struct MFILE_CADDY* p_file );
161 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
164 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags );
166 mfile_t* p_file, uint8_t flags,
const char* fmt, va_list args );
176 struct MFILE_CADDY* p_f,
const uint8_t* buf,
size_t buf_sz );
188 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, va_list args );
198 union MFILE_HANDLE
h;
208 mfile_has_bytes_t has_bytes;
209 mfile_cursor_t cursor;
210 mfile_read_byte_t read_byte;
211 mfile_read_block_t read_block;
213 mfile_read_int_t read_int;
214 mfile_read_line_t read_line;
215 mfile_printf_t printf;
217 mfile_write_block_t write_block;
232off_t mfile_file_has_bytes(
struct MFILE_CADDY* p_file );
237 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
240 struct MFILE_CADDY* p_f, uint8_t* buf,
size_t buf_sz, uint8_t flags );
245 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags );
248 struct MFILE_CADDY* p_f, uint8_t flags,
const char* fmt, ... );
251 struct MFILE_CADDY* p_f,
const uint8_t* buf,
size_t buf_sz );
254 struct MFILE_CADDY* p_f, uint8_t flags,
const char* fmt, va_list args );
256#define mfile_get_sz( p_file ) ((p_file)->sz)
262 MAUG_MHANDLE,
void* ptr, off_t, mfile_t* p_file );
270MERROR_RETVAL mfile_open_write(
const char* filename, mfile_t* p_file );
282off_t mfile_file_has_bytes(
struct MFILE_CADDY* p_file ) {
284 cursor = p_file->cursor( p_file );
285 if( p_file->
sz > cursor ) {
286#if MFILE_READ_TRACE_LVL > 0
287 debug_printf( MFILE_READ_TRACE_LVL,
288 "file has " OFF_T_FMT
" bytes left...",
289 p_file->
sz - cursor );
291 return p_file->
sz - cursor;
293#if MFILE_READ_TRACE_LVL > 0
295 debug_printf( MFILE_READ_TRACE_LVL,
"file has error bytes left!" );
307 char* ext_ptr = NULL;
311 if( MFILE_ASSIGN_FLAG_TRIM_EXT == (MFILE_ASSIGN_FLAG_TRIM_EXT & flags) ) {
312 ext_ptr = maug_strrchr( tgt,
'.' );
313 if( NULL != ext_ptr ) {
324 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz, uint8_t flags
331#elif defined( MAUG_MSBF )
335 debug_printf( MFILE_READ_TRACE_LVL,
"reading integer forward" );
337 retval = p_file->read_block( p_file, buf, buf_sz );
340 debug_printf( MFILE_READ_TRACE_LVL,
"reading integer reversed" );
342 while( 0 < buf_sz ) {
343 retval = p_file->read_byte( p_file, (buf + (buf_sz - 1)) );
344 maug_cleanup_if_not_ok();
357 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, ...
362 va_start( vargs, fmt );
363 retval = p_file->vprintf( p_file, flags, fmt, vargs );
371off_t mfile_mem_cursor(
struct MFILE_CADDY* p_file ) {
377off_t mfile_mem_has_bytes(
struct MFILE_CADDY* p_file ) {
392 if( (MAUG_MHANDLE)NULL != p_f->
h.mem ) {
403static void mfile_mem_release(
struct MFILE_CADDY* p_f ) {
409 p_f->flags &= ~MFILE_FLAG_HANDLE_LOCKED;
416 return p_file->read_block( p_file, buf, 1 );
422 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz
430 retval = mfile_mem_lock( p_file );
431 maug_cleanup_if_not_ok();
433 while( 0 < buf_sz-- ) {
439 mfile_mem_release( p_file );
453 debug_printf( MFILE_SEEK_TRACE_LVL,
454 "seeking memory buffer to position " OFF_T_FMT
" (" OFF_T_FMT
")",
463 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags
470 while( i < buffer_sz - 1 && p_f->has_bytes( p_f ) ) {
472 if( i + 1 >= buffer_sz ) {
473 error_printf(
"overflow reading string from file!" );
474 retval = MERROR_FILE;
478 p_f->read_int( p_f, (uint8_t*)&(buffer[i]), 1, 0 );
479 if(
'\n' == buffer[i] ) {
486 assert( i < buffer_sz );
497 mfile_t* p_file, uint8_t flags,
const char* fmt, va_list args
506 error_printf(
"writing to memory buffer not currently supported!" );
518 struct MFILE_CADDY* p_f,
const uint8_t* buf,
size_t buf_sz
532 retval = mfile_mem_lock( p_f );
533 maug_cleanup_if_not_ok();
535#if MFILE_WRITE_TRACE_LVL > 0
536 debug_printf( MFILE_WRITE_TRACE_LVL,
"p: %p, sz: %u, cur: %u, buf_sz: %u\n",
557 mfile_mem_release( p_f );
565 MAUG_MHANDLE handle,
void* ptr, off_t handle_sz, mfile_t* p_file
569 debug_printf( MFILE_SEEK_TRACE_LVL,
570 "locking handle %p as file %p (" OFF_T_FMT
" bytes)...",
571 handle, p_file, handle_sz );
573 maug_mzero( p_file,
sizeof(
struct MFILE_CADDY ) );
576 if( (MAUG_MHANDLE)NULL == handle && NULL != ptr ) {
578 }
else if( (MAUG_MHANDLE)NULL != handle && NULL == ptr ) {
579 p_file->
h.mem = handle;
582 error_printf(
"must specify handle or pointer!" );
583 retval = MERROR_FILE;
589 p_file->has_bytes = mfile_mem_has_bytes;
590 p_file->cursor = mfile_mem_cursor;
591 p_file->read_byte = mfile_mem_read_byte;
592 p_file->read_block = mfile_mem_read_block;
593 p_file->read_int = mfile_file_read_int;
594 p_file->seek = mfile_mem_seek;
595 p_file->read_line = mfile_mem_read_line;
598 p_file->
sz = handle_sz;
611 retval = mfile_plt_open_read( filename, p_file );
613 if( MERROR_OK == retval ) {
624MERROR_RETVAL mfile_open_write(
const char* filename, mfile_t* p_file ) {
627 retval = mfile_plt_open_write( filename, p_file );
629 if( MERROR_OK == retval ) {
641# if MFILE_SEEK_TRACE_LVL > 0
642 debug_printf( MFILE_SEEK_TRACE_LVL,
"closing file..." );
645 munmap( bytes_ptr_h, bytes_sz );
648 switch( p_file->
type ) {
654 mfile_plt_close( p_file );
661 debug_printf( MFILE_SEEK_TRACE_LVL,
662 "unlocked handle %p from file %p...",
663 p_file->
h.mem, p_file );
669 error_printf(
"unknown file type: %d", (p_file)->
type );
uint16_t 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 MFILE_FLAG_HANDLE_LOCKED
Flag for MFILE_CADDY::flags indicating subsequent internal unlocks should unlock the handle back to i...
Definition mfile.h:71
MERROR_RETVAL mfile_lock_buffer(MAUG_MHANDLE, void *ptr, off_t, mfile_t *p_file)
Lock a buffer and assign it to an mfile_t to read/write.
#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:99
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:187
#define MFILE_READ_FLAG_LSBF
Flag for mfile_read_int_t() indicating integer should always be read least significant byte first.
Definition mfile.h:92
MERROR_RETVAL mfile_open_read(const char *filename, mfile_t *p_file)
Open a file and read it into memory or memory-map it.
MERROR_RETVAL mfile_mem_write_block(struct MFILE_CADDY *p_f, const uint8_t *buf, size_t buf_sz)
Insert provided buffer into the given file.
void mfile_close(mfile_t *p_file)
Close a file opened with mfile_open_read().
#define MFILE_FLAG_READ_ONLY
Flag for MFILE_CADDY::flags indicating this file is read-only.
Definition mfile.h:63
char retroflat_asset_path[MAUG_PATH_SZ_MAX+1]
Path/name used to load an asset from disk.
Definition mfile.h:129
off_t mem_cursor
Current position if its type is MFILE_CADDY_TYPE_MEM_BUFFER.
Definition mfile.h:201
uint8_t type
The RetroFile Types flag describing this file.
Definition mfile.h:196
union MFILE_HANDLE h
The physical handle or pointer to access the file by.
Definition mfile.h:198
uint8_t * mem_buffer
Locked pointer for MFILE_HANDLE::mem.
Definition mfile.h:203
off_t sz
Size of the current file/buffer in bytes.
Definition mfile.h:206