view dmres.h @ 495:30145d17aebd

Move certain editor related targets to the actual TARGETS variable, and remove them from being separately specified in the "make clean" target, thusly cleaning them only when editor is enabled in build configuration.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 16 Nov 2012 19:18:03 +0200
parents 3d9c044ec08d
children b60220fd1669
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 DM_USE_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 DMResourceLib;
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 DMResourceLib *lib;
    struct DMResource *next, *prev;

    void   *rdata;
    size_t rdataSize;
    struct DMResourceDataOps *rops;

    FILE * fh;
} 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
{
    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(DMResourceLib **lib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *));
int          dmres_close(DMResourceLib *lib);

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

void         dmres_flags_to_symbolic(char *str, size_t size, int flags);
int          dmres_symbolic_to_flags(const char *str);
int          dmres_load_resfile(DMResourceLib *lib, const char *filename);
int          dmres_write_resfile(DMResourceLib *lib, const char *filename);

DMResource * dmres_new(DMResourceLib *lib, const char *filename, int flags, size_t size);
void         dmres_free(DMResource *node);
void         dmres_insert(DMResourceLib *lib, DMResource * node);
void         dmres_delete(DMResourceLib *lib, DMResource * node);
DMResource * dmres_find(DMResourceLib *lib, const char *filename);
int          dmres_ref(DMResource *);
int          dmres_unref(DMResource *);


// Opening and closing resources
DMResource * dmf_open(DMResourceLib *lib, const char *);
DMResource * dmf_create_memio(DMResourceLib *lib, const char *, Uint8 *buf, size_t len);
#ifdef DM_USE_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 *);
char *       dmfgets(char *s, int size, DMResource * f);


// Helper functions for endianess based reading etc
int          dmf_read_str(DMResource *, Uint8 *, size_t);
BOOL         dmf_read_byte(DMResource *, Uint8 *);

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