Mercurial > hg > dmlib
annotate src/dmres.h @ 2437:f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 25 Feb 2020 07:26:40 +0200 |
parents | 5ffc48a0bebe |
children | fa089a430121 |
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 | |
20 | |
21 /* Constants | |
22 */ | |
23 enum | |
24 { | |
25 DMPRUNE_ATIME = 0x0001, | |
26 DMPRUNE_MTIME = 0x0002, | |
27 }; | |
28 | |
29 enum | |
30 { | |
31 DRF_USE_PACK = 0x0001, | |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
32 DRF_USE_STDIO = 0x0002, |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
33 DRF_PRELOAD_RAW = 0x0004, |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
34 DRF_PRELOAD_RES = 0x0008, |
0 | 35 }; |
36 | |
37 enum | |
38 { | |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
39 DMF_PERSIST = 0x0001, // Persist loaded RAW resource |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
40 DMF_COMPRESSED = 0x0002, // Resource is compressed in PACK file, otherwise raw |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
41 DMF_TEMPORARY = 0x0004, |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
42 DMF_PACK_MASK = 0x00ff, // Mask for flags that may be specified in PACK |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
43 |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
44 DMF_UNALLOCATED = 0x1000, // The raw data is not allocated, so do not free it |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
45 DMF_LOADED_RAW = 0x2000, // Raw data has been loaded |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
46 DMF_LOADED_RES = 0x4000, // Resource has been loaded |
0 | 47 }; |
48 | |
49 | |
50 /* Typedefs and structures | |
51 */ | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
52 struct DMResourceLib; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
53 struct DMResourceDataOps; |
0 | 54 struct DMResourceOps; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
55 struct DMResource; |
0 | 56 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
57 typedef struct DMResource |
0 | 58 { |
59 // Timestamps (in seconds from time()) | |
60 int mtime, // When resource was loaded | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
61 atime; // Last accessed (dmResourceRef()/unref) |
0 | 62 int refcount; // Reference count |
63 | |
64 int flags; // Resource flags (DMF_*) | |
65 char *filename; | |
66 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
67 // Raw data (or mem data) |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2385
diff
changeset
|
68 size_t maxSize; // Maximum size (0 = no limit) |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2385
diff
changeset
|
69 size_t memSize; // Size of data |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2385
diff
changeset
|
70 size_t memAlloc; // Amount allocated |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2385
diff
changeset
|
71 size_t minAlloc; // Minimum allocation increase (0 = default) |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2385
diff
changeset
|
72 off_t memOffset; // Current offset in data |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2385
diff
changeset
|
73 Uint8 *memData; // Pointer to data |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
74 |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
75 // Decoded resource data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
76 void *resData; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
77 size_t resSize; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
78 struct DMResourceDataOps *rops; |
0 | 79 |
80 int error; // Error code | |
81 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
82 #ifdef DM_USE_STDIO |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
83 FILE * fh; // File handle for stdio |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
84 #endif |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
85 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
86 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
|
87 struct DMResourceLib *lib; // Pointer to the resource library |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
88 struct DMResource *next, *prev; |
0 | 89 } DMResource; |
90 | |
91 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
92 typedef struct DMResourceDataOps |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
93 { |
105
d5d27f262227
Beging merging of dmsimple2-fork into the main "engine".
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
94 BOOL (*probe)(DMResource *res, const char *fext); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
95 int (*load)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
96 void (*free)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
97 } DMResourceDataOps; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
98 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
99 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
100 typedef struct DMResourceLib |
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 int flags; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
103 char *resPath; |
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 DMResource *resources, *preload; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
106 DMMutex *mutex; |
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 #ifdef DM_USE_PACKFS |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
109 char *packFilename; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
110 DMPackFile *packFile; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
111 #endif |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
112 } DMResourceLib; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
113 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
114 |
0 | 115 typedef struct DMResourceOps |
116 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
117 char *name; |
732 | 118 |
119 int (*freset)(DMResource *); | |
0 | 120 int (*ferror)(DMResource *); |
1106
f8e9f6b2a41a
Hmm .. back out the Sint64 changes for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
121 int (*fseek)(DMResource *, const off_t, const int); |
0 | 122 off_t (*fsize)(DMResource *); |
1106
f8e9f6b2a41a
Hmm .. back out the Sint64 changes for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
123 off_t (*ftell)(DMResource *); |
0 | 124 BOOL (*feof)(DMResource *); |
125 int (*fgetc)(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
126 int (*fputc)(int, DMResource *); |
0 | 127 size_t (*fread)(void *, const size_t, const size_t, DMResource *); |
1086 | 128 size_t (*fwrite)(const void *, const size_t, const size_t, DMResource *); |
0 | 129 |
130 int (*fopen)(DMResource *); | |
131 void (*fclose)(DMResource *); | |
132 int (*preload)(DMResource *); | |
133 } DMResourceOps; | |
134 | |
135 | |
136 /* Functions | |
137 */ | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
138 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
|
139 int dmResourcesClose(DMResourceLib *lib); |
0 | 140 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
141 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
|
142 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total); |
0 | 143 |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
144 DMResource * dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags); |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
145 void dmResourceFree(DMResource *node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
146 void dmResourceInsert(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
147 void dmResourceDelete(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
148 DMResource * dmResourceFind(DMResourceLib *lib, const char *filename); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
149 int dmResourceRef(DMResource *); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
150 int dmResourceUnref(DMResource *); |
0 | 151 |
152 | |
153 // Opening and closing resources | |
1607 | 154 int dmf_open(DMResourceLib *lib, const char *, DMResource **phandle); |
155 void dmf_close(DMResource *fh); | |
914 | 156 |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1106
diff
changeset
|
157 int dmf_open_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
|
158 #ifdef DM_USE_STDIO |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1106
diff
changeset
|
159 int dmf_open_stdio(const char *filename, const char *mode, DMResource **phandle); |
1607 | 160 int dmf_open_stdio_stream(FILE *fh, DMResource **phandle); |
0 | 161 #endif |
162 | |
163 | |
164 // Basic resource access functions | |
1607 | 165 int dmfreset(DMResource *fh); |
166 int dmferror(DMResource *fh); | |
167 int dmfseek(DMResource *fh, const off_t offset, const int whence); | |
168 off_t dmfsize(DMResource *fh); | |
169 off_t dmftell(DMResource *fh); | |
170 BOOL dmfeof(DMResource *fh); | |
171 int dmfgetc(DMResource *fh); | |
172 int dmfputc(int val, DMResource *fh); | |
173 size_t dmfread(void *ptr, const size_t size, const size_t nmemb, DMResource *fh); | |
174 size_t dmfwrite(const void *ptr, const size_t size, const size_t nmemb, DMResource *fh); | |
175 char * dmfgets(char *str, const int size, DMResource *fh); | |
1608
7f9fe2a9a87e
Implement dmfputs() in DMResource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
1607
diff
changeset
|
176 int dmfputs(const char *str, DMResource *fh); |
1885
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
177 int dmvfprintf(DMResource *fh, const char *fmt, va_list ap); |
2383
43e39d9ec42f
Add __attribute__(__format__ ..) specifiers for functions that use printf() style format specifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1885
diff
changeset
|
178 int dmfprintf(DMResource *fh, const char *fmt, ...) |
2385
5ffc48a0bebe
Conditionally define DM_ATTR_PRINTF_FMT(xstart, xend) instead of using
Matti Hamalainen <ccr@tnsp.org>
parents:
2383
diff
changeset
|
179 DM_ATTR_PRINTF_FMT(2, 3); |
0 | 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 |
1607 | 182 BOOL dmf_read_str(DMResource *fh, void *ptr, const size_t len); |
183 BOOL dmf_read_byte(DMResource *fh, Uint8 *val); | |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
184 |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
185 #define DM_DEFINE_FFUNC(xname, xtype, z) \ |
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
186 BOOL dmf_read_ ## xname (DMResource *fh, xtype *v); |
872
b01d04e44b6f
Assume we always have 64bit type.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
187 |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
188 #include "dmfiletmpl.h" |
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
189 |
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
190 #undef DM_DEFINE_FFUNC |
0 | 191 |
1881
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
192 BOOL dmf_write_str(DMResource *fh, const void *data, const size_t len); |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
193 BOOL dmf_write_byte(DMResource *fh, const Uint8 val); |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
194 |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
195 #define DM_DEFINE_FFUNC(xname, xtype, z) \ |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
196 BOOL dmf_write_ ## xname (DMResource *fh, const xtype v); |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
197 |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
198 #include "dmfiletmpl.h" |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
199 |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
200 #undef DM_DEFINE_FFUNC |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
201 |
0 | 202 |
203 #ifdef __cplusplus | |
204 } | |
205 #endif | |
206 #endif // DMRES_H |