view dmres.h @ 96:6bf5220fa47e

Urgh .. use memset to silence some bogus GCC warnings about using potentially uninitialized values, while that will not actually be possible. In any case, it is annoying.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Oct 2012 18:52:28 +0300
parents 23ac82365a65
children d97accc86365
line wrap: on
line source

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

#include "dmlib.h"

#ifdef DMRES_PACKFS
#include <zlib.h>
#include "dmpack.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif


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

enum
{
    DRF_USE_PACK    = 0x0001,
    DRF_PRELOAD_ALL = 0x0002,
    DRF_PRELOAD_RES = 0x0004,
};

enum
{
    DMF_PRELOAD_RAW = 0x0001, // Preload raw data
    DMF_PRELOAD_RES = 0x0002, // Perform resource preloading (not 
    DMF_PERSIST     = 0x0004, // Persist loaded resource (only freed at shutdown/explicit prune)
    DMF_STREAM      = 0x0008, // This resource is streamed (UNSUPPORTED FOR NOW)
    DMF_MASK        = 0x0fff,

    DMF_LOADED_RAW  = 0x1000, // Raw data has been loaded
    DMF_LOADED_RES  = 0x2000, // Resource has been loaded
};


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

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

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

    size_t dataSize;           // Size of data
    off_t  dataOffset;         // Current offset in data
    Uint8 *data;               // Pointer to data

    int    error;              // Error code

    struct DMResourceOps *fops;    // Pointer to file handling functions struct
    struct DMResource *next, *prev;

    void   *rdata;
    struct DMResourceDataOps *rops;

    FILE * fh;
} DMResource;


typedef struct DMResourceDataOps
{
    int    (*load)(DMResource *res);
    void   (*free)(DMResource *res);
} DMResourceDataOps;


typedef struct DMResourceOps
{
    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)(void *, const size_t, const size_t, DMResource *);

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


/* Functions
 */
int          dmres_init(const char *filename, const char *path, int flags, int (*classifier)(DMResource *));
void         dmres_close(void);

void         dmres_prune(int agems, int flags);
int          dmres_preload(BOOL start, int *loaded, int *total);

int          dmres_load_resfile(const char *filename);
int          dmres_write_resfile(const char *filename);

DMResource * dmres_find(const char *filename);
int          dmres_ref(DMResource *);
int          dmres_unref(DMResource *);


// Opening and closing resources
DMResource * dmf_open(const char *);
DMResource * dmf_open_memio(const char *, Uint8 *buf, size_t len);
#ifdef DMRES_STDIO
DMResource * dmf_create_stdio(const char *filename, const char *mode);
DMResource * dmf_create_stdio_stream(FILE *);
#endif
void         dmf_close(DMResource *);


// Basic resource access functions
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(void *, const size_t, const size_t, DMResource *);


// Specialized functions for endianess based reading etc
int          dmf_read_str(DMResource *, Uint8 *, size_t);
BOOL         dmf_read_be16(DMResource *, Uint16 *);
BOOL         dmf_read_be32(DMResource *, Uint32 *);
BOOL         dmf_read_le16(DMResource *, Uint16 *);
BOOL         dmf_read_le32(DMResource *, Uint32 *);

#ifdef DM_HAVE_64BIT
BOOL         dmf_read_be64(DMResource *, Uint64 *);
BOOL         dmf_read_le64(DMResource *, Uint64 *);
#endif


#ifdef __cplusplus
}
#endif

#endif // DMRES_H