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 ) && \
16 !defined( RETROFLAT_API_PSN )
34#ifndef MAUG_PATH_SZ_MAX
36# define MAUG_PATH_SZ_MAX 256
51#define MFILE_CADDY_TYPE_FILE 0x01
56#define MFILE_CADDY_TYPE_MEM_BUFFER 0x80
64#define MFILE_FLAG_READ_ONLY 0x01
72#define MFILE_FLAG_HANDLE_LOCKED 0x02
93#define MFILE_READ_FLAG_LSBF 0x01
100#define MFILE_READ_FLAG_MSBF 0x01
102#define MFILE_ASSIGN_FLAG_TRIM_EXT 0x01
104#ifndef MFILE_READ_TRACE_LVL
105# define MFILE_READ_TRACE_LVL 0
108#ifndef MFILE_WRITE_TRACE_LVL
109# define MFILE_WRITE_TRACE_LVL 0
112#ifndef MFILE_SEEK_TRACE_LVL
113# define MFILE_SEEK_TRACE_LVL 0
116#ifndef MFILE_CONTENTS_TRACE_LVL
117# define MFILE_CONTENTS_TRACE_LVL 0
135#define mfile_cmp_path( a, b ) strncmp( a, b, MAUG_PATH_SZ_MAX )
142typedef off_t (*mfile_cursor_t)(
struct MFILE_CADDY* p_file );
143typedef off_t (*mfile_has_bytes_t)(
struct MFILE_CADDY* p_file );
147 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
150 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz, uint8_t flags );
152 struct MFILE_CADDY* p_file,
char* buf, off_t buf_sz, uint8_t flags );
154 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, ... );
156 struct MFILE_CADDY* p_f,
const uint8_t* buf,
size_t buf_sz );
158off_t mfile_mem_cursor(
struct MFILE_CADDY* p_file );
159off_t mfile_mem_has_bytes(
struct MFILE_CADDY* p_file );
162 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
165 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags );
167 mfile_t* p_file, uint8_t flags,
const char* fmt, va_list args );
177 struct MFILE_CADDY* p_f,
const uint8_t* buf,
size_t buf_sz );
189 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, va_list args );
199 union MFILE_HANDLE
h;
209 mfile_has_bytes_t has_bytes;
210 mfile_cursor_t cursor;
211 mfile_read_byte_t read_byte;
212 mfile_read_block_t read_block;
214 mfile_read_int_t read_int;
215 mfile_read_line_t read_line;
216 mfile_printf_t printf;
218 mfile_write_block_t write_block;
233off_t mfile_file_has_bytes(
struct MFILE_CADDY* p_file );
238 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz );
241 struct MFILE_CADDY* p_f, uint8_t* buf,
size_t buf_sz, uint8_t flags );
246 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags );
249 struct MFILE_CADDY* p_f, uint8_t flags,
const char* fmt, ... );
252 struct MFILE_CADDY* p_f,
const uint8_t* buf,
size_t buf_sz );
255 struct MFILE_CADDY* p_f, uint8_t flags,
const char* fmt, va_list args );
257#define mfile_get_sz( p_file ) ((p_file)->sz)
263 MAUG_MHANDLE,
void* ptr, off_t, mfile_t* p_file );
271MERROR_RETVAL mfile_open_write(
const char* filename, mfile_t* p_file );
283off_t mfile_file_has_bytes(
struct MFILE_CADDY* p_file ) {
285 cursor = p_file->cursor( p_file );
286 if( p_file->
sz > cursor ) {
287#if MFILE_READ_TRACE_LVL > 0
288 debug_printf( MFILE_READ_TRACE_LVL,
289 "file has " OFF_T_FMT
" bytes left...",
290 p_file->
sz - cursor );
292 return p_file->
sz - cursor;
294#if MFILE_READ_TRACE_LVL > 0
296 debug_printf( MFILE_READ_TRACE_LVL,
"file has error bytes left!" );
308 char* ext_ptr = NULL;
312 if( MFILE_ASSIGN_FLAG_TRIM_EXT == (MFILE_ASSIGN_FLAG_TRIM_EXT & flags) ) {
313 ext_ptr = maug_strrchr( tgt,
'.' );
314 if( NULL != ext_ptr ) {
325 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz, uint8_t flags
332#elif defined( MAUG_MSBF )
336 debug_printf( MFILE_READ_TRACE_LVL,
"reading integer forward" );
338 retval = p_file->read_block( p_file, buf, buf_sz );
341 debug_printf( MFILE_READ_TRACE_LVL,
"reading integer reversed" );
343 while( 0 < buf_sz ) {
344 retval = p_file->read_byte( p_file, (buf + (buf_sz - 1)) );
345 maug_cleanup_if_not_ok();
358 struct MFILE_CADDY* p_file, uint8_t flags,
const char* fmt, ...
363 va_start( vargs, fmt );
364 retval = p_file->vprintf( p_file, flags, fmt, vargs );
372off_t mfile_mem_cursor(
struct MFILE_CADDY* p_file ) {
378off_t mfile_mem_has_bytes(
struct MFILE_CADDY* p_file ) {
393 if( (MAUG_MHANDLE)NULL != p_f->
h.mem ) {
404static void mfile_mem_release(
struct MFILE_CADDY* p_f ) {
410 p_f->flags &= ~MFILE_FLAG_HANDLE_LOCKED;
417 return p_file->read_block( p_file, buf, 1 );
423 struct MFILE_CADDY* p_file, uint8_t* buf,
size_t buf_sz
431 retval = mfile_mem_lock( p_file );
432 maug_cleanup_if_not_ok();
434 while( 0 < buf_sz-- ) {
440 mfile_mem_release( p_file );
454 debug_printf( MFILE_SEEK_TRACE_LVL,
455 "seeking memory buffer to position " OFF_T_FMT
" (" OFF_T_FMT
")",
464 struct MFILE_CADDY* p_f,
char* buffer, off_t buffer_sz, uint8_t flags
471 while( i < buffer_sz - 1 && p_f->has_bytes( p_f ) ) {
473 if( i + 1 >= buffer_sz ) {
474 error_printf(
"overflow reading string from file!" );
475 retval = MERROR_FILE;
479 p_f->read_int( p_f, (uint8_t*)&(buffer[i]), 1, 0 );
480 if(
'\n' == buffer[i] ) {
487 assert( i < buffer_sz );
498 mfile_t* p_file, uint8_t flags,
const char* fmt, va_list args
507 error_printf(
"writing to memory buffer not currently supported!" );
519 struct MFILE_CADDY* p_f,
const uint8_t* buf,
size_t buf_sz
533 retval = mfile_mem_lock( p_f );
534 maug_cleanup_if_not_ok();
536#if MFILE_WRITE_TRACE_LVL > 0
537 debug_printf( MFILE_WRITE_TRACE_LVL,
"p: %p, sz: %u, cur: %u, buf_sz: %u\n",
558 mfile_mem_release( p_f );
566 MAUG_MHANDLE handle,
void* ptr, off_t handle_sz, mfile_t* p_file
570 debug_printf( MFILE_SEEK_TRACE_LVL,
571 "locking handle %p as file %p (" OFF_T_FMT
" bytes)...",
572 handle, p_file, handle_sz );
574 maug_mzero( p_file,
sizeof(
struct MFILE_CADDY ) );
577 if( (MAUG_MHANDLE)NULL == handle && NULL != ptr ) {
579 }
else if( (MAUG_MHANDLE)NULL != handle && NULL == ptr ) {
580 p_file->
h.mem = handle;
583 error_printf(
"must specify handle or pointer!" );
584 retval = MERROR_FILE;
590 p_file->has_bytes = mfile_mem_has_bytes;
591 p_file->cursor = mfile_mem_cursor;
592 p_file->read_byte = mfile_mem_read_byte;
593 p_file->read_block = mfile_mem_read_block;
594 p_file->read_int = mfile_file_read_int;
595 p_file->seek = mfile_mem_seek;
596 p_file->read_line = mfile_mem_read_line;
599 p_file->
sz = handle_sz;
612 retval = mfile_plt_open_read( filename, p_file );
614 if( MERROR_OK == retval ) {
625MERROR_RETVAL mfile_open_write(
const char* filename, mfile_t* p_file ) {
628 retval = mfile_plt_open_write( filename, p_file );
630 if( MERROR_OK == retval ) {
642# if MFILE_SEEK_TRACE_LVL > 0
643 debug_printf( MFILE_SEEK_TRACE_LVL,
"closing file..." );
646 munmap( bytes_ptr_h, bytes_sz );
649 switch( p_file->
type ) {
655 mfile_plt_close( p_file );
662 debug_printf( MFILE_SEEK_TRACE_LVL,
663 "unlocked handle %p from file %p...",
664 p_file->
h.mem, p_file );
670 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:28
#define MFILE_CADDY_TYPE_FILE
A standard UNIX file.
Definition mfile.h:51
#define MFILE_CADDY_TYPE_MEM_BUFFER
An array of bytes in memory abstracted through this library.
Definition mfile.h:56
#define MFILE_FLAG_HANDLE_LOCKED
Flag for MFILE_CADDY::flags indicating subsequent internal unlocks should unlock the handle back to i...
Definition mfile.h:72
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:36
#define MFILE_READ_FLAG_MSBF
Flag for mfile_read_int_t() indicating integer should always be read most significant byte first.
Definition mfile.h:100
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:188
#define MFILE_READ_FLAG_LSBF
Flag for mfile_read_int_t() indicating integer should always be read least significant byte first.
Definition mfile.h:93
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:64
char retroflat_asset_path[MAUG_PATH_SZ_MAX+1]
Path/name used to load an asset from disk.
Definition mfile.h:130
off_t mem_cursor
Current position if its type is MFILE_CADDY_TYPE_MEM_BUFFER.
Definition mfile.h:202
uint8_t type
The RetroFile Types flag describing this file.
Definition mfile.h:197
union MFILE_HANDLE h
The physical handle or pointer to access the file by.
Definition mfile.h:199
uint8_t * mem_buffer
Locked pointer for MFILE_HANDLE::mem.
Definition mfile.h:204
off_t sz
Size of the current file/buffer in bytes.
Definition mfile.h:207