Mercurial > hg > dmlib
annotate dmres.h @ 613:15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 12 Apr 2013 12:34:17 +0300 |
parents | c1a5652e473d |
children | e8fc083b7499 |
rev | line source |
---|---|
0 | 1 /* |
2 * DMLib | |
3 * -- Resource management | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
5 * (C) Copyright 2011-2012 Tecnic Software productions (TNSP) | |
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 <zlib.h> |
14 #include "dmpack.h" | |
15 #endif | |
16 | |
17 #ifdef __cplusplus | |
18 extern "C" { | |
19 #endif | |
20 | |
21 | |
22 /* Constants | |
23 */ | |
24 enum | |
25 { | |
26 DMPRUNE_ATIME = 0x0001, | |
27 DMPRUNE_MTIME = 0x0002, | |
28 }; | |
29 | |
30 enum | |
31 { | |
32 DRF_USE_PACK = 0x0001, | |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
33 DRF_USE_STDIO = 0x0002, |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
34 DRF_PRELOAD_ALL = 0x0004, |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
35 DRF_PRELOAD_RES = 0x0008, |
0 | 36 }; |
37 | |
38 enum | |
39 { | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
40 DMF_PRELOAD_RAW = 0x0001, // Preload raw data |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
41 DMF_PRELOAD_RES = 0x0002, // Perform resource preloading (not |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
42 DMF_PERSIST = 0x0004, // Persist loaded resource (only freed at shutdown/explicit prune) |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
43 DMF_STREAM = 0x0008, // This resource is streamed (UNSUPPORTED FOR NOW) |
4
e0fc7863d024
Mask out bits from resFlags that should not be there after initialization.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
44 DMF_MASK = 0x0fff, |
0 | 45 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
46 DMF_LOADED_RAW = 0x1000, // Raw data has been loaded |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
47 DMF_LOADED_RES = 0x2000, // Resource has been loaded |
0 | 48 }; |
49 | |
50 | |
51 /* Typedefs and structures | |
52 */ | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
53 struct DMResourceLib; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
54 struct DMResourceDataOps; |
0 | 55 struct DMResourceOps; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
56 struct DMResource; |
0 | 57 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
58 typedef struct DMResource |
0 | 59 { |
60 // Timestamps (in seconds from time()) | |
61 int mtime, // When resource was loaded | |
62 atime; // Last accessed (dmres_ref()/unref) | |
63 int refcount; // Reference count | |
64 | |
65 int flags; // Resource flags (DMF_*) | |
66 char *filename; | |
67 | |
68 size_t dataSize; // Size of data | |
69 off_t dataOffset; // Current offset in data | |
70 Uint8 *data; // Pointer to data | |
71 | |
72 int error; // Error code | |
73 | |
74 struct DMResourceOps *fops; // Pointer to file handling functions struct | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
75 struct DMResourceLib *lib; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
76 struct DMResource *next, *prev; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
77 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
78 void *rdata; |
103
d97accc86365
Add variable rdataSize to DMResource.
Matti Hamalainen <ccr@tnsp.org>
parents:
74
diff
changeset
|
79 size_t rdataSize; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
80 struct DMResourceDataOps *rops; |
0 | 81 |
82 FILE * fh; | |
83 } DMResource; | |
84 | |
85 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
86 typedef struct DMResourceDataOps |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
87 { |
105
d5d27f262227
Beging merging of dmsimple2-fork into the main "engine".
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
88 BOOL (*probe)(DMResource *res, const char *fext); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
89 int (*load)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
90 void (*free)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
91 } DMResourceDataOps; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
92 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
93 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
94 typedef struct DMResourceLib |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
95 { |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
96 int flags; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
97 char *resPath; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
98 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
99 DMResource *resources, *preload; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
100 DMMutex *mutex; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
101 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
102 #ifdef DM_USE_PACKFS |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
103 char *packFilename; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
104 DMPackFile *packFile; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
105 #endif |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
106 } DMResourceLib; |
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 |
0 | 109 typedef struct DMResourceOps |
110 { | |
111 int (*ferror)(DMResource *); | |
112 int (*fseek)(DMResource *, const off_t, const int); | |
113 off_t (*fsize)(DMResource *); | |
114 off_t (*ftell)(DMResource *); | |
115 BOOL (*feof)(DMResource *); | |
116 int (*fgetc)(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
117 int (*fputc)(int, DMResource *); |
0 | 118 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
|
119 size_t (*fwrite)(void *, const size_t, const size_t, DMResource *); |
0 | 120 |
121 int (*fopen)(DMResource *); | |
122 void (*fclose)(DMResource *); | |
123 int (*preload)(DMResource *); | |
124 } DMResourceOps; | |
125 | |
126 | |
127 /* Functions | |
128 */ | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
129 int dmres_init(DMResourceLib **lib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
130 int dmres_close(DMResourceLib *lib); |
0 | 131 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
132 void dmres_prune(DMResourceLib *lib, int agems, int flags); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
133 int dmres_preload(DMResourceLib *lib, BOOL start, int *loaded, int *total); |
0 | 134 |
114
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
135 void dmres_flags_to_symbolic(char *str, size_t size, int flags); |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
136 int dmres_symbolic_to_flags(const char *str); |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
137 int dmres_load_resfile(DMResourceLib *lib, const char *filename); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
138 int dmres_write_resfile(DMResourceLib *lib, const char *filename); |
0 | 139 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
140 DMResource * dmres_new(DMResourceLib *lib, const char *filename, int flags, size_t size); |
208
c6b4fa03744c
Make some functions static, some public in the header.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
141 void dmres_free(DMResource *node); |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
142 void dmres_insert(DMResourceLib *lib, DMResource * node); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
143 void dmres_delete(DMResourceLib *lib, DMResource * node); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
144 DMResource * dmres_find(DMResourceLib *lib, const char *filename); |
0 | 145 int dmres_ref(DMResource *); |
146 int dmres_unref(DMResource *); | |
147 | |
148 | |
149 // Opening and closing resources | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
150 DMResource * dmf_open(DMResourceLib *lib, const char *); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
151 DMResource * dmf_create_memio(DMResourceLib *lib, const char *, Uint8 *buf, size_t len); |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
152 #ifdef DM_USE_STDIO |
74
23ac82365a65
Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
153 DMResource * dmf_create_stdio(const char *filename, const char *mode); |
0 | 154 DMResource * dmf_create_stdio_stream(FILE *); |
155 #endif | |
156 void dmf_close(DMResource *); | |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
157 int dmf_preload(DMResource *); |
0 | 158 |
159 | |
160 // Basic resource access functions | |
161 int dmferror(DMResource *); | |
162 int dmfseek(DMResource *, const off_t, const int); | |
163 off_t dmfsize(DMResource *); | |
164 off_t dmftell(DMResource *); | |
165 BOOL dmfeof(DMResource *); | |
166 int dmfgetc(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
167 int dmfputc(int, DMResource *); |
0 | 168 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
|
169 size_t dmfwrite(void *, const size_t, const size_t, DMResource *); |
235 | 170 char * dmfgets(char *s, int size, DMResource * f); |
0 | 171 |
172 | |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
173 // 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
|
174 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
|
175 BOOL dmf_read_byte(DMResource *, Uint8 *); |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
176 |
0 | 177 BOOL dmf_read_be16(DMResource *, Uint16 *); |
178 BOOL dmf_read_be32(DMResource *, Uint32 *); | |
179 BOOL dmf_read_le16(DMResource *, Uint16 *); | |
180 BOOL dmf_read_le32(DMResource *, Uint32 *); | |
181 | |
182 #ifdef DM_HAVE_64BIT | |
183 BOOL dmf_read_be64(DMResource *, Uint64 *); | |
184 BOOL dmf_read_le64(DMResource *, Uint64 *); | |
185 #endif | |
186 | |
187 | |
188 #ifdef __cplusplus | |
189 } | |
190 #endif | |
191 | |
192 #endif // DMRES_H |