view dmres.h @ 0:32250b436bca

Initial re-import.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Sep 2012 01:54:23 +0300
parents
children e0fc7863d024
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,
};

enum
{
    DMF_PRELOAD     = 0x0001, // Preload this resource
    DMF_PERSIST     = 0x0002, // Persist loaded resource (only freed at shutdown/explicit prune)
    //DMF_STREAM      = 0x0004, // This resource is streamed

    DMF_LOADED      = 0x1000, // Resource has been loaded
};


/* Typedefs and structures
 */
struct DMResourceOps;

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;

    FILE * fh;
} DMResource;


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 *);
    size_t  (*fread)(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);
void         dmres_close(void);

void         dmres_prune(int agems, int flags);
int          dmres_preload();

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 *);
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 *);
size_t       dmfread(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