28#if defined (HAVE_CONFIG_H)
47#define xMalloc(n,Type) (Type *)eMalloc((size_t)(n) * sizeof (Type))
48#define xRealloc(p,n,Type) (Type *)eRealloc((p), (n) * sizeof (Type))
50static void *
eMalloc (
const size_t size)
52 void *buffer = malloc (size);
56 fprintf(stderr,
"out of memory");
63static void *
eRealloc (
void *
const ptr,
const size_t size)
70 buffer = realloc (
ptr, size);
73 fprintf(stderr,
"out of memory");
84#define eFreeNoNullCheck eFree
86# define Assert(c) do {} while(0)
87# define AssertNotReached() do {} while(0)
91#define MIO_CHUNK_SIZE 4096
93#define MAX(a, b) (((a) > (b)) ? (a) : (b))
199 FILE *fp = open_func (
filename, mode);
377 if (
mio_seek (base, 0, SEEK_END) != 0)
384 if (
mio_seek (base, start, SEEK_SET) != 0)
387 data =
xMalloc (size,
unsigned char);
389 mio_seek (base, original_pos, SEEK_SET);
542 return fread (ptr_, size, nmemb, mio->
impl.
file.
fp);
547 if (size != 0 && nmemb != 0)
550 size_t copy_bytes = size * nmemb;
551 unsigned char *
ptr = ptr_;
553 if (size_avail < copy_bytes)
554 copy_bytes = size_avail;
558 n_read = copy_bytes / size;
601 if (new_size == ULONG_MAX)
611 if (new_size <= mio->
impl.mem.allocated_size)
619 unsigned char *newbuf;
635 unsigned char *newbuf;
638 if (newbuf || new_size == 0)
698 size_t n_written = 0;
700 if (size != 0 && nmemb != 0)
741 rv = (int)((
unsigned char)c);
828 if (rv >= 0 && (
size_t)rv == (n - 1))
970 return fgets (s, (
int)size, mio->
impl.
file.
fp);
978 bool newline =
false;
992 for (;
pos < buf_size && i < (size - 1); i++)
994 s[i] = (char)buf[
pos];
1008 if (!newline &&
pos >= buf_size)
1104 return fseek (mio->
impl.
file.
fp, offset, whence);
1113 if (offset < 0 || (
size_t)offset > mio->
impl.
mem.
size)
1123 if ((offset < 0 && (
size_t)-offset > mio->
impl.
mem.
pos) ||
1136 if (offset > 0 || (
size_t)-offset > mio->
impl.
mem.
size)
1292 if (
pos->tag != mio)
1294 g_critical (
"mio_setpos((MIO*)%p, (MIOPos*)%p): "
1295 "Given MIOPos was not set by a previous call to mio_getpos() "
1296 "on the same MIO object, which means there is a bug in "
1298 (
void *)mio, (
void *)
pos);
1361 mio->
udata.
d = user_data;
1362 mio->
udata.
f = user_data_free_func;
#define AssertNotReached()
unsigned char * mio_memory_get_data(MIO *mio, size_t *size)
mio_memory_get_data: @mio: A MIO object @size: (allow-none) (out): Return location for the length of ...
MIO * mio_new_fp(FILE *fp, MIOFCloseFunc close_func)
mio_new_fp: @fp: An opened #FILE object @close_func: (allow-none): Function used to close @fp when th...
size_t mio_write(MIO *mio, const void *ptr, size_t size, size_t nmemb)
mio_write: @mio: A MIO object @ptr: Pointer to the memory to write on the stream @size: Size of each ...
int mio_getc(MIO *mio)
mio_getc: @mio: A MIO object
static int mem_try_ensure_space(MIO *mio, size_t n)
long mio_tell(MIO *mio)
mio_tell: @mio: A MIO object
MIO * mio_new_file(const char *filename, const char *mode)
mio_new_file: @filename: Filename to open, same as the fopen()'s first argument @mode: Mode in which ...
int mio_vprintf(MIO *mio, const char *format, va_list ap)
mio_vprintf: @mio: A MIO object @format: A printf format string @ap: The variadic argument list for t...
size_t mio_read(MIO *mio, void *ptr_, size_t size, size_t nmemb)
mio_read: @mio: A MIO object @ptr: Pointer to the memory to fill with the read data @size: Size of ea...
void mio_clearerr(MIO *mio)
mio_clearerr: @mio: A MIO object
int mio_seek(MIO *mio, long offset, int whence)
mio_seek: @mio: A MIO object @offset: Offset of the new place, from @whence @whence: Move origin.
static int mem_try_resize(MIO *mio, size_t new_size)
int mio_eof(MIO *mio)
mio_eof: @mio: A MIO object
void * mio_get_user_data(MIO *mio)
mio_get_user_data: @mio: A MIO object
int mio_try_resize(MIO *mio, size_t new_size)
int mio_getpos(MIO *mio, MIOPos *pos)
mio_getpos: @mio: A MIO stream @pos: (out): A MIOPos object to fill-in
int mio_flush(MIO *mio)
mio_flush: @mio: A MIO object
MIO * mio_new_file_full(const char *filename, const char *mode, MIOFOpenFunc open_func, MIOFCloseFunc close_func)
mio_new_file_full: @filename: Filename to open, passed as-is to @open_func as the first argument @mod...
int mio_error(MIO *mio)
mio_error: @mio: A MIO object
void mio_rewind(MIO *mio)
mio_rewind: @mio: A MIO object
FILE * mio_file_get_fp(MIO *mio)
mio_file_get_fp: @mio: A MIO object
char * mio_gets(MIO *mio, char *s, size_t size)
mio_gets: @mio: A MIO object @s: A string to fill with the read data @size: The maximum number of byt...
MIO * mio_new_memory(unsigned char *data, size_t size, MIOReallocFunc realloc_func, MIODestroyNotify free_func)
mio_new_memory: @data: Initial data (may be NULL) @size: Length of @data in bytes @realloc_func: A fu...
MIO * mio_new_mio(MIO *base, long start, long size)
mio_new_mio: @base: The original mio @start: stream offset of the @base where new mio starts @size: t...
int mio_printf(MIO *mio, const char *format,...)
mio_printf: @mio: A MIO object @format: A print format string ...: Arguments of the format
int mio_puts(MIO *mio, const char *s)
mio_puts: @mio: A MIO object @s: The string to write
MIO * mio_ref(MIO *mio)
mio_ref: @mio: A MIO object
int mio_ungetc(MIO *mio, int ch)
mio_ungetc: @mio: A MIO object @ch: Character to put back in the stream
int mio_putc(MIO *mio, int c)
mio_putc: @mio: A MIO object : The character to write
int mio_unref(MIO *mio)
mio_unref: @mio: A MIO object
void mio_attach_user_data(MIO *mio, void *user_data, MIODestroyNotify user_data_free_func)
mio_attach_user_data: @mio: A MIO object @user_data: a pointer to any data object @user_data_free_fun...
int mio_setpos(MIO *mio, MIOPos *pos)
mio_setpos: @mio: A MIO object @pos: (in): A MIOPos object filled-in by a previous call of mio_getpos...
void(* MIODestroyNotify)(void *data)
MIODestroyNotify: @data: Data element being destroyed.
void *(* MIOReallocFunc)(void *ptr, size_t size)
MIOReallocFunc: @ptr: Pointer to the memory to resize @size: New size of the memory pointed by @ptr.
FILE *(* MIOFOpenFunc)(const char *filename, const char *mode)
MIOFOpenFunc: @filename: The filename to open @mode: fopen() modes for opening @filename.
int(* MIOFCloseFunc)(FILE *fp)
MIOFCloseFunc: @fp: An opened #FILE object.
#define va_copy(dest, src)
void * eMalloc(const size_t size)
void eFreeNoNullCheck(void *const ptr)
void * eRealloc(void *const ptr, const size_t size)
void eFree(void *const ptr)
struct _MIO::@13::@15 mem
struct _MIO::@13::@14 file
MIODestroyNotify free_func
MIOReallocFunc realloc_func