Mercurial > hg > dmlib
annotate src/dmres.h @ 1036:1a0a327d8fed
Use 64bit file offsets.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 01 Mar 2015 16:57:50 +0200 |
parents | 6436d80ae0fc |
children | ebabf5aefb76 |
rev | line source |
---|---|
0 | 1 /* |
2 * DMLib | |
3 * -- Resource management | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
863 | 5 * (C) Copyright 2011-2015 Tecnic Software productions (TNSP) |
0 | 6 */ |
7 #ifndef DMRES_H | |
8 #define DMRES_H | |
9 | |
10 #include "dmlib.h" | |
11 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
12 #ifdef DM_USE_PACKFS |
0 | 13 #include "dmpack.h" |
14 #endif | |
15 | |
16 #ifdef __cplusplus | |
17 extern "C" { | |
18 #endif | |
19 | |
1036 | 20 #ifdef HAVE_FSEEK64 |
21 # define DM_FSEEK64 fseek64 | |
22 # define DM_FTELL64 ftell64 | |
23 #else | |
24 # define DM_FSEEK64 fseeko | |
25 # define DM_FTELL64 ftello | |
26 #endif | |
27 | |
0 | 28 |
29 /* Constants | |
30 */ | |
31 enum | |
32 { | |
33 DMPRUNE_ATIME = 0x0001, | |
34 DMPRUNE_MTIME = 0x0002, | |
35 }; | |
36 | |
37 enum | |
38 { | |
39 DRF_USE_PACK = 0x0001, | |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
40 DRF_USE_STDIO = 0x0002, |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
41 DRF_PRELOAD_RAW = 0x0004, |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
42 DRF_PRELOAD_RES = 0x0008, |
0 | 43 }; |
44 | |
45 enum | |
46 { | |
1009 | 47 DMF_PERSIST = 0x00001, // Persist loaded RAW resource |
48 DMF_TEMPORARY = 0x00002, | |
49 DMF_UNALLOCATED = 0x00004, // The raw data is not allocated, so do not free it | |
50 DMF_LOADED_RAW = 0x01000, // Raw data has been loaded | |
51 DMF_LOADED_RES = 0x02000, // Resource has been loaded | |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1009
diff
changeset
|
52 DMF_COMPRESSED = 0x10000, // Resource is compressed in PACK file, otherwise raw |
0 | 53 }; |
54 | |
55 | |
56 /* Typedefs and structures | |
57 */ | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
58 struct DMResourceLib; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
59 struct DMResourceDataOps; |
0 | 60 struct DMResourceOps; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
61 struct DMResource; |
0 | 62 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
63 typedef struct DMResource |
0 | 64 { |
65 // Timestamps (in seconds from time()) | |
66 int mtime, // When resource was loaded | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
67 atime; // Last accessed (dmResourceRef()/unref) |
0 | 68 int refcount; // Reference count |
69 | |
70 int flags; // Resource flags (DMF_*) | |
71 char *filename; | |
72 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
73 // Raw data (or mem data) |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
74 size_t rawSize; // Size of data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
75 off_t rawOffset; // Current offset in data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
76 Uint8 *rawData; // Pointer to data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
77 |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
78 // Decoded resource data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
79 void *resData; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
80 size_t resSize; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
81 struct DMResourceDataOps *rops; |
0 | 82 |
83 int error; // Error code | |
84 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
85 #ifdef DM_USE_STDIO |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
86 FILE * fh; // File handle for stdio |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
87 #endif |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
88 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
89 struct DMResourceOps *fops; // Pointer to file handling functions struct |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
90 struct DMResourceLib *lib; // Pointer to the resource library |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
91 struct DMResource *next, *prev; |
0 | 92 } DMResource; |
93 | |
94 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
95 typedef struct DMResourceDataOps |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
96 { |
105
d5d27f262227
Beging merging of dmsimple2-fork into the main "engine".
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
97 BOOL (*probe)(DMResource *res, const char *fext); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
98 int (*load)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
99 void (*free)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
100 } DMResourceDataOps; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
101 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
102 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
103 typedef struct DMResourceLib |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
104 { |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
105 int flags; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
106 char *resPath; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
107 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
108 DMResource *resources, *preload; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
109 DMMutex *mutex; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
110 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
111 #ifdef DM_USE_PACKFS |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
112 char *packFilename; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
113 DMPackFile *packFile; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
114 #endif |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
115 } DMResourceLib; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
116 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
117 |
0 | 118 typedef struct DMResourceOps |
119 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
120 char *name; |
732 | 121 |
122 int (*freset)(DMResource *); | |
0 | 123 int (*ferror)(DMResource *); |
1036 | 124 int (*fseek)(DMResource *, const Sint64, const int); |
0 | 125 off_t (*fsize)(DMResource *); |
1036 | 126 Sint64 (*ftell)(DMResource *); |
0 | 127 BOOL (*feof)(DMResource *); |
128 int (*fgetc)(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
129 int (*fputc)(int, DMResource *); |
0 | 130 size_t (*fread)(void *, const size_t, const size_t, DMResource *); |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
131 size_t (*fwrite)(void *, const size_t, const size_t, DMResource *); |
0 | 132 |
133 int (*fopen)(DMResource *); | |
134 void (*fclose)(DMResource *); | |
135 int (*preload)(DMResource *); | |
136 } DMResourceOps; | |
137 | |
138 | |
139 /* Functions | |
140 */ | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
141 int dmResourcesInit(DMResourceLib **lib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
142 int dmResourcesClose(DMResourceLib *lib); |
0 | 143 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
144 void dmResourcesPrune(DMResourceLib *lib, const int agems, int const flags); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
145 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total); |
0 | 146 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
147 DMResource * dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
148 void dmResourceFree(DMResource *node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
149 void dmResourceInsert(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
150 void dmResourceDelete(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
151 DMResource * dmResourceFind(DMResourceLib *lib, const char *filename); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
152 int dmResourceRef(DMResource *); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
153 int dmResourceUnref(DMResource *); |
0 | 154 |
155 | |
156 // Opening and closing resources | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
157 int dmf_open(DMResourceLib *lib, const char *, DMResource **handle); |
914 | 158 void dmf_close(DMResource *); |
159 | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
160 int dmf_create_memio(DMResourceLib *lib, const char *, Uint8 *buf, size_t len, DMResource **phandle); |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
161 #ifdef DM_USE_STDIO |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
162 int dmf_create_stdio(const char *filename, const char *mode, DMResource **phandle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
163 int dmf_create_stdio_stream(FILE *, DMResource **phandle); |
0 | 164 #endif |
165 | |
166 | |
167 // Basic resource access functions | |
732 | 168 int dmfreset(DMResource *); |
0 | 169 int dmferror(DMResource *); |
1036 | 170 int dmfseek(DMResource *, Sint64, const int); |
0 | 171 off_t dmfsize(DMResource *); |
1036 | 172 Sint64 dmftell(DMResource *); |
0 | 173 BOOL dmfeof(DMResource *); |
174 int dmfgetc(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
175 int dmfputc(int, DMResource *); |
0 | 176 size_t dmfread(void *, const size_t, const size_t, DMResource *); |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
177 size_t dmfwrite(void *, const size_t, const size_t, DMResource *); |
235 | 178 char * dmfgets(char *s, int size, DMResource * f); |
0 | 179 |
180 | |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
181 // Helper functions for endianess based reading etc |
553
b60220fd1669
s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
182 int dmf_read_str(DMResource *, void *, size_t); |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
183 BOOL dmf_read_byte(DMResource *, Uint8 *); |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
184 |
0 | 185 BOOL dmf_read_be16(DMResource *, Uint16 *); |
186 BOOL dmf_read_be32(DMResource *, Uint32 *); | |
872
b01d04e44b6f
Assume we always have 64bit type.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
187 BOOL dmf_read_be64(DMResource *, Uint64 *); |
b01d04e44b6f
Assume we always have 64bit type.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
188 |
0 | 189 BOOL dmf_read_le16(DMResource *, Uint16 *); |
190 BOOL dmf_read_le32(DMResource *, Uint32 *); | |
191 BOOL dmf_read_le64(DMResource *, Uint64 *); | |
192 | |
193 | |
194 #ifdef __cplusplus | |
195 } | |
196 #endif | |
197 | |
198 #endif // DMRES_H |