# HG changeset patch # User Matti Hamalainen # Date 1526287533 -10800 # Node ID 0cac3360a0aaab1da078f61f2dd93507e54e744f # Parent 93d1050eac99066ee9eec137b983d2713f46c36b Cleanups. diff -r 93d1050eac99 -r 0cac3360a0aa src/dmres.c --- a/src/dmres.c Mon May 14 11:20:05 2018 +0300 +++ b/src/dmres.c Mon May 14 11:45:33 2018 +0300 @@ -168,105 +168,105 @@ } -static void dm_stdio_fclose(DMResource * f) +static void dm_stdio_fclose(DMResource *fh) { - if (f->fh != NULL) + if (fh->fh != NULL) { - fclose(f->fh); - f->fh = NULL; + fclose(fh->fh); + fh->fh = NULL; } } -static int dm_stdio_ferror(DMResource * f) +static int dm_stdio_ferror(DMResource *fh) { - return f->error; + return fh->error; } -static off_t dm_stdio_ftell(DMResource * f) +static off_t dm_stdio_ftell(DMResource *fh) { - return ftello(f->fh); + return ftello(fh->fh); } -static int dm_stdio_fseek(DMResource *f, const off_t pos, const int whence) +static int dm_stdio_fseek(DMResource *fh, const off_t pos, const int whence) { - int ret = fseeko(f->fh, pos, whence); - f->error = dmGetErrno(); + int ret = fseeko(fh->fh, pos, whence); + fh->error = dmGetErrno(); return ret; } -static int dm_stdio_freset(DMResource * f) +static int dm_stdio_freset(DMResource *fh) { - if (f->fh != NULL) - return dm_stdio_fseek(f, 0, SEEK_SET); + if (fh->fh != NULL) + return dm_stdio_fseek(fh, 0, SEEK_SET); else return DMERR_OK; } -static off_t dm_stdio_fsize(DMResource *f) +static off_t dm_stdio_fsize(DMResource *fh) { off_t savePos, fileSize; // Check if the size is cached - if (f->rawSize != 0) - return f->rawSize; + if (fh->rawSize != 0) + return fh->rawSize; // Get file size - if ((savePos = dm_stdio_ftell(f)) < 0) + if ((savePos = dm_stdio_ftell(fh)) < 0) return -1; - if (dm_stdio_fseek(f, 0, SEEK_END) != 0) + if (dm_stdio_fseek(fh, 0, SEEK_END) != 0) return -1; - if ((fileSize = dm_stdio_ftell(f)) < 0) + if ((fileSize = dm_stdio_ftell(fh)) < 0) return -1; - if (dm_stdio_fseek(f, savePos, SEEK_SET) != 0) + if (dm_stdio_fseek(fh, savePos, SEEK_SET) != 0) return -1; - f->rawSize = fileSize; + fh->rawSize = fileSize; return fileSize; } -static BOOL dm_stdio_feof(DMResource * f) +static BOOL dm_stdio_feof(DMResource *fh) { - return feof(f->fh); + return feof(fh->fh); } -static int dm_stdio_fgetc(DMResource * f) +static int dm_stdio_fgetc(DMResource *fh) { - int ret = fgetc(f->fh); - f->error = dmGetErrno(); + int ret = fgetc(fh->fh); + fh->error = dmGetErrno(); return ret; } -static int dm_stdio_fputc(int v, DMResource * f) +static int dm_stdio_fputc(int v, DMResource *fh) { - int ret = fputc(v, f->fh); - f->error = dmGetErrno(); + int ret = fputc(v, fh->fh); + fh->error = dmGetErrno(); return ret; } -static size_t dm_stdio_fread(void *ptr, size_t size, size_t nmemb, DMResource * f) +static size_t dm_stdio_fread(void *ptr, size_t size, size_t nmemb, DMResource *fh) { - size_t ret = fread(ptr, size, nmemb, f->fh); - f->error = dmGetErrno(); + size_t ret = fread(ptr, size, nmemb, fh->fh); + fh->error = dmGetErrno(); return ret; } -static size_t dm_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, DMResource * f) +static size_t dm_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, DMResource *fh) { - size_t ret = fwrite(ptr, size, nmemb, f->fh); - f->error = dmGetErrno(); + size_t ret = fwrite(ptr, size, nmemb, fh->fh); + fh->error = dmGetErrno(); return ret; } @@ -520,13 +520,13 @@ } -static int dm_pack_fopen(DMResource * f) +static int dm_pack_fopen(DMResource *fh) { - if ((f->flags & DMF_LOADED_RAW) == 0) + if ((fh->flags & DMF_LOADED_RAW) == 0) { - int ret = dm_pack_preload(f); + int ret = dm_pack_preload(fh); if (ret == DMERR_OK) - f->flags |= DMF_LOADED_RAW; + fh->flags |= DMF_LOADED_RAW; return ret; } @@ -535,28 +535,28 @@ } -static void dm_pack_fclose(DMResource * f) +static void dm_pack_fclose(DMResource *fh) { - if ((f->flags & DMF_PERSIST) == 0) - dmResourceFreeRawData(f); + if ((fh->flags & DMF_PERSIST) == 0) + dmResourceFreeRawData(fh); } #endif -static int dm_mem_freset(DMResource * f) +static int dm_mem_freset(DMResource *fh) { - f->rawOffset = 0; + fh->rawOffset = 0; return DMERR_OK; } -static int dm_mem_ferror(DMResource * f) +static int dm_mem_ferror(DMResource *fh) { - return f->error; + return fh->error; } -static int dm_mem_fseek(DMResource * f, const off_t offset, const int whence) +static int dm_mem_fseek(DMResource *fh, const off_t offset, const int whence) { off_t newPos; @@ -568,11 +568,11 @@ break; case SEEK_CUR: - newPos = f->rawOffset + offset; + newPos = fh->rawOffset + offset; break; case SEEK_END: - newPos = f->rawSize + offset; + newPos = fh->rawSize + offset; break; default: @@ -580,71 +580,71 @@ } // Set the new position - f->rawOffset = newPos; + fh->rawOffset = newPos; // Check the new position - if (newPos < 0 && (size_t) newPos >= f->rawSize) + if (newPos < 0 && (size_t) newPos >= fh->rawSize) return -1; return 0; } -static off_t dm_mem_fsize(DMResource * f) +static off_t dm_mem_fsize(DMResource *fh) { - return f->rawSize; + return fh->rawSize; } -static off_t dm_mem_ftell(DMResource * f) +static off_t dm_mem_ftell(DMResource *fh) { - return f->rawOffset; + return fh->rawOffset; } -static BOOL dm_mem_feof(DMResource * f) +static BOOL dm_mem_feof(DMResource *fh) { // Check for EOF - if ((size_t) f->rawOffset <= f->rawSize) + if ((size_t) fh->rawOffset <= fh->rawSize) return FALSE; else return TRUE; } -static int dm_mem_fgetc(DMResource * f) +static int dm_mem_fgetc(DMResource *fh) { // Check for EOF - if ((size_t) f->rawOffset < f->rawSize) - return f->rawData[f->rawOffset++]; + if ((size_t) fh->rawOffset < fh->rawSize) + return fh->rawData[fh->rawOffset++]; else return EOF; } -static size_t dm_mem_fread(void *buf, size_t size, size_t nmemb, DMResource * f) +static size_t dm_mem_fread(void *buf, size_t size, size_t nmemb, DMResource *fh) { size_t length = (size * nmemb); // Check if we can read the whole chunk - if (((size_t) f->rawOffset + length) >= f->rawSize) + if (((size_t) fh->rawOffset + length) >= fh->rawSize) { - nmemb = (f->rawSize - f->rawOffset) / size; + nmemb = (fh->rawSize - fh->rawOffset) / size; length = size * nmemb; } - memcpy(buf, f->rawData + f->rawOffset, length); - f->rawOffset += length; + memcpy(buf, fh->rawData + fh->rawOffset, length); + fh->rawOffset += length; return nmemb; } -static int dm_mem_fputc(int ch, DMResource * f) +static int dm_mem_fputc(int ch, DMResource *fh) { // Check for EOF - if ((size_t) f->rawOffset < f->rawSize) + if ((size_t) fh->rawOffset < fh->rawSize) { - f->rawData[f->rawOffset++] = ch; + fh->rawData[fh->rawOffset++] = ch; return ch; } else @@ -652,21 +652,21 @@ } -static size_t dm_mem_fwrite(const void *buf, size_t size, size_t nmemb, DMResource * f) +static size_t dm_mem_fwrite(const void *buf, size_t size, size_t nmemb, DMResource *fh) { size_t length = (size * nmemb); // Check if we can write the whole chunk - if (((size_t) f->rawOffset + length) >= f->rawSize) + if (((size_t) fh->rawOffset + length) >= fh->rawSize) { - nmemb = (f->rawSize - f->rawOffset) / size; + nmemb = (fh->rawSize - fh->rawOffset) / size; length = size * nmemb; } if (length > 0) { - memcpy(f->rawData + f->rawOffset, buf, length); - f->rawOffset += length; + memcpy(fh->rawData + fh->rawOffset, buf, length); + fh->rawOffset += length; } return nmemb; } @@ -876,84 +876,84 @@ #endif -void dmf_close(DMResource * f) +void dmf_close(DMResource *handle) { - if (f == NULL) + if (handle == NULL) return; - dmResourceUnref(f); + dmResourceUnref(handle); - if (f->fops->fclose != NULL) - f->fops->fclose(f); + if (handle->fops->fclose != NULL) + handle->fops->fclose(handle); } -int dmfreset(DMResource *f) +int dmfreset(DMResource *fh) { - if (f == NULL) + if (fh == NULL) return DMERR_NULLPTR; - if (f->fops == NULL || f->fops->freset == NULL) + if (fh->fops == NULL || fh->fops->freset == NULL) return DMERR_OK; - return f->fops->freset(f); + return fh->fops->freset(fh); } -int dmferror(DMResource * f) +int dmferror(DMResource *fh) { - f->atime = time(NULL); - return f->fops->ferror(f); + fh->atime = time(NULL); + return fh->fops->ferror(fh); } -int dmfseek(DMResource * f, const off_t offset, int whence) +int dmfseek(DMResource *fh, const off_t offset, const int whence) { - f->atime = time(NULL); - return f->fops->fseek(f, offset, whence); + fh->atime = time(NULL); + return fh->fops->fseek(fh, offset, whence); } -off_t dmfsize(DMResource * f) +off_t dmfsize(DMResource *fh) { - f->atime = time(NULL); - return f->fops->fsize(f); + fh->atime = time(NULL); + return fh->fops->fsize(fh); } -off_t dmftell(DMResource * f) +off_t dmftell(DMResource *fh) { - f->atime = time(NULL); - return f->fops->ftell(f); + fh->atime = time(NULL); + return fh->fops->ftell(fh); } -BOOL dmfeof(DMResource * f) +BOOL dmfeof(DMResource *fh) { - f->atime = time(NULL); - return f->fops->feof(f); + fh->atime = time(NULL); + return fh->fops->feof(fh); } -int dmfgetc(DMResource * f) +int dmfgetc(DMResource *fh) { - f->atime = time(NULL); - return f->fops->fgetc(f); + fh->atime = time(NULL); + return fh->fops->fgetc(fh); } -int dmfputc(int v, DMResource * f) +int dmfputc(const int val, DMResource *fh) { - f->atime = time(NULL); - return f->fops->fputc(v, f); + fh->atime = time(NULL); + return fh->fops->fputc(val, fh); } -size_t dmfread(void *ptr, size_t size, size_t nmemb, DMResource * f) +size_t dmfread(void *ptr, size_t size, size_t nmemb, DMResource *fh) { - f->atime = time(NULL); - return f->fops->fread(ptr, size, nmemb, f); + fh->atime = time(NULL); + return fh->fops->fread(ptr, size, nmemb, fh); } -size_t dmfwrite(const void *ptr, size_t size, size_t nmemb, DMResource * f) +size_t dmfwrite(const void *ptr, size_t size, size_t nmemb, DMResource *fh) { - f->atime = time(NULL); - return f->fops->fwrite(ptr, size, nmemb, f); + fh->atime = time(NULL); + return fh->fops->fwrite(ptr, size, nmemb, fh); } -char *dmfgets(char *str, int size, DMResource * f) +char *dmfgets(char *str, const int size, DMResource *fh) { char *ptr = str, *end = str + size - 1; int c; @@ -961,7 +961,7 @@ if (size <= 0) return NULL; - while (ptr < end && (c = f->fops->fgetc(f)) != EOF) + while (ptr < end && (c = fh->fops->fgetc(fh)) != EOF) { *ptr++ = c; if (c == '\n') @@ -1259,27 +1259,27 @@ /* Helper resource access routines */ -int dmf_read_str(DMResource *f, void *s, size_t l) +BOOL dmf_read_str(DMResource *fh, void *ptr, const size_t len) { - return dmfread(s, 1, l, f) == l; + return dmfread(ptr, len, 1, fh) == 1; } -BOOL dmf_read_byte(DMResource *f, Uint8 *val) +BOOL dmf_read_byte(DMResource *fh, Uint8 *val) { - int tmp = dmfgetc(f); + int tmp = dmfgetc(fh); *val = tmp; return tmp != EOF; } -#define DM_DEFINE_FFUNC(xname, xtype, xmacro) \ -BOOL dmf_read_ ## xname (DMResource *f, xtype *v) { \ - xtype result; \ - if (dmfread(&result, sizeof( xtype ), 1, f) != 1) \ - return FALSE; \ - *v = DM_ ## xmacro ## _TO_NATIVE (result); \ - return TRUE; \ +#define DM_DEFINE_FFUNC(xname, xtype, xmacro) \ +BOOL dmf_read_ ## xname (DMResource *fh, xtype *val) { \ + xtype result; \ + if (dmfread(&result, sizeof( xtype ), 1, fh) != 1) \ + return FALSE; \ + *val = DM_ ## xmacro ## _TO_NATIVE (result); \ + return TRUE; \ } #include "dmfiletmpl.h" diff -r 93d1050eac99 -r 0cac3360a0aa src/dmres.h --- a/src/dmres.h Mon May 14 11:20:05 2018 +0300 +++ b/src/dmres.h Mon May 14 11:45:33 2018 +0300 @@ -148,33 +148,33 @@ // Opening and closing resources -int dmf_open(DMResourceLib *lib, const char *, DMResource **handle); -void dmf_close(DMResource *); +int dmf_open(DMResourceLib *lib, const char *, DMResource **phandle); +void dmf_close(DMResource *fh); int dmf_open_memio(DMResourceLib *lib, const char *, Uint8 *buf, size_t len, DMResource **phandle); #ifdef DM_USE_STDIO int dmf_open_stdio(const char *filename, const char *mode, DMResource **phandle); -int dmf_open_stdio_stream(FILE *, DMResource **phandle); +int dmf_open_stdio_stream(FILE *fh, DMResource **phandle); #endif // Basic resource access functions -int dmfreset(DMResource *); -int dmferror(DMResource *); -int dmfseek(DMResource *, const off_t, const int); -off_t dmfsize(DMResource *); -off_t dmftell(DMResource *); -BOOL dmfeof(DMResource *); -int dmfgetc(DMResource *); -int dmfputc(int, DMResource *); -size_t dmfread(void *, const size_t, const size_t, DMResource *); -size_t dmfwrite(const void *, const size_t, const size_t, DMResource *); -char * dmfgets(char *s, int size, DMResource * f); +int dmfreset(DMResource *fh); +int dmferror(DMResource *fh); +int dmfseek(DMResource *fh, const off_t offset, const int whence); +off_t dmfsize(DMResource *fh); +off_t dmftell(DMResource *fh); +BOOL dmfeof(DMResource *fh); +int dmfgetc(DMResource *fh); +int dmfputc(int val, DMResource *fh); +size_t dmfread(void *ptr, const size_t size, const size_t nmemb, DMResource *fh); +size_t dmfwrite(const void *ptr, const size_t size, const size_t nmemb, DMResource *fh); +char * dmfgets(char *str, const int size, DMResource *fh); // Helper functions for endianess based reading etc -int dmf_read_str(DMResource *, void *, const size_t); -BOOL dmf_read_byte(DMResource *, Uint8 *); +BOOL dmf_read_str(DMResource *fh, void *ptr, const size_t len); +BOOL dmf_read_byte(DMResource *fh, Uint8 *val); #define DM_DEFINE_FFUNC(xname, xtype, z) \ BOOL dmf_read_ ## xname (DMResource *fh, xtype *v); diff -r 93d1050eac99 -r 0cac3360a0aa src/dmresw.c --- a/src/dmresw.c Mon May 14 11:20:05 2018 +0300 +++ b/src/dmresw.c Mon May 14 11:45:33 2018 +0300 @@ -7,15 +7,15 @@ #include "dmresw.h" -int dmf_write_str(DMResource *f, const void *s, const size_t l) +BOOL dmf_write_str(DMResource *fh, const void *data, const size_t len) { - return dmfwrite(s, 1, l, f) == l; + return dmfwrite(data, len, 1, fh) == 1; } -BOOL dmf_write_byte(DMResource *f, const Uint8 val) +BOOL dmf_write_byte(DMResource *fh, const Uint8 val) { - return dmfputc(val, f) == val; + return dmfputc(val, fh) == val; } diff -r 93d1050eac99 -r 0cac3360a0aa src/dmresw.h --- a/src/dmresw.h Mon May 14 11:20:05 2018 +0300 +++ b/src/dmresw.h Mon May 14 11:45:33 2018 +0300 @@ -13,8 +13,8 @@ extern "C" { #endif -int dmf_write_str(DMResource *, const void *, const size_t); -BOOL dmf_write_byte(DMResource *, const Uint8); +BOOL dmf_write_str(DMResource *fh, const void *data, const size_t len); +BOOL dmf_write_byte(DMResource *fh, const Uint8 val); #define DM_DEFINE_FFUNC(xname, xtype, z) \ BOOL dmf_write_ ## xname (DMResource *fh, const xtype v);