view src/dmres.h @ 2383:43e39d9ec42f

Add __attribute__(__format__ ..) specifiers for functions that use printf() style format specifiers.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 Jan 2020 14:55:41 +0200
parents e69de47d2419
children 5ffc48a0bebe
line wrap: on
line source

/*
 * DMLib
 * -- Resource management
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2011-2015 Tecnic Software productions (TNSP)
 */
#ifndef DMRES_H
#define DMRES_H

#include "dmlib.h"

#ifdef DM_USE_PACKFS
#include "dmpack.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif


/* Constants
 */
enum
{
    DMPRUNE_ATIME   = 0x0001,
    DMPRUNE_MTIME   = 0x0002,
};

enum
{
    DRF_USE_PACK    = 0x0001,
    DRF_USE_STDIO   = 0x0002,
    DRF_PRELOAD_RAW = 0x0004,
    DRF_PRELOAD_RES = 0x0008,
};

enum
{
    DMF_PERSIST     = 0x0001, // Persist loaded RAW resource
    DMF_COMPRESSED  = 0x0002, // Resource is compressed in PACK file, otherwise raw
    DMF_TEMPORARY   = 0x0004,
    DMF_PACK_MASK   = 0x00ff, // Mask for flags that may be specified in PACK

    DMF_UNALLOCATED = 0x1000, // The raw data is not allocated, so do not free it
    DMF_LOADED_RAW  = 0x2000, // Raw data has been loaded
    DMF_LOADED_RES  = 0x4000, // Resource has been loaded
};


/* Typedefs and structures
 */
struct DMResourceLib;
struct DMResourceDataOps;
struct DMResourceOps;
struct DMResource;

typedef struct DMResource
{
    // Timestamps (in seconds from time())
    int    mtime,              // When resource was loaded
           atime;              // Last accessed (dmResourceRef()/unref)
    int    refcount;           // Reference count

    int    flags;              // Resource flags (DMF_*)
    char   *filename;

    // Raw data (or mem data)
    size_t rawSize;            // Size of data
    off_t  rawOffset;          // Current offset in data
    Uint8 *rawData;            // Pointer to data

    // Decoded resource data
    void   *resData;
    size_t resSize;
    struct DMResourceDataOps *rops;

    int    error;              // Error code

#ifdef DM_USE_STDIO
    FILE * fh;                 // File handle for stdio
#endif

    struct DMResourceOps *fops;    // Pointer to file handling functions struct
    struct DMResourceLib *lib;     // Pointer to the resource library
    struct DMResource *next, *prev;
} DMResource;


typedef struct DMResourceDataOps
{
    BOOL   (*probe)(DMResource *res, const char *fext);
    int    (*load)(DMResource *res);
    void   (*free)(DMResource *res);
} DMResourceDataOps;


typedef struct DMResourceLib
{
    int  flags;
    char *resPath;

    DMResource *resources, *preload;
    DMMutex *mutex;

#ifdef DM_USE_PACKFS
    char *packFilename;
    DMPackFile *packFile;
#endif
} DMResourceLib;


typedef struct DMResourceOps
{
    char    *name;

    int     (*freset)(DMResource *);
    int     (*ferror)(DMResource *);
    int     (*fseek)(DMResource *, const off_t, const int);
    off_t   (*fsize)(DMResource *);
    off_t   (*ftell)(DMResource *);
    BOOL    (*feof)(DMResource *);
    int     (*fgetc)(DMResource *);
    int     (*fputc)(int, DMResource *);
    size_t  (*fread)(void *, const size_t, const size_t, DMResource *);
    size_t  (*fwrite)(const void *, const size_t, const size_t, DMResource *);

    int     (*fopen)(DMResource *);
    void    (*fclose)(DMResource *);
    int     (*preload)(DMResource *);
} DMResourceOps;


/* Functions
 */
int          dmResourcesInit(DMResourceLib **lib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *));
int          dmResourcesClose(DMResourceLib *lib);

void         dmResourcesPrune(DMResourceLib *lib, const int agems, int const flags);
int          dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total);

DMResource * dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags);
void         dmResourceFree(DMResource *node);
void         dmResourceInsert(DMResourceLib *lib, DMResource * node);
void         dmResourceDelete(DMResourceLib *lib, DMResource * node);
DMResource * dmResourceFind(DMResourceLib *lib, const char *filename);
int          dmResourceRef(DMResource *);
int          dmResourceUnref(DMResource *);


// Opening and closing resources
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 *fh, DMResource **phandle);
#endif


// Basic resource access functions
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);
int          dmfputs(const char *str, DMResource *fh);
int          dmvfprintf(DMResource *fh, const char *fmt, va_list ap);
int          dmfprintf(DMResource *fh, const char *fmt, ...)
             __attribute__ ((__format__ (__printf__, 2, 3)));

// Helper functions for endianess based reading etc
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);

#include "dmfiletmpl.h"

#undef DM_DEFINE_FFUNC

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);

#include "dmfiletmpl.h"

#undef DM_DEFINE_FFUNC


#ifdef __cplusplus
}
#endif
#endif // DMRES_H