annotate dmres.c @ 609:877dba1b8a1f

Fix some resource code checks.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 12 Apr 2013 11:46:17 +0300
parents 72c6120271a7
children 15cd5b8a29b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * dmlib
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * -- Resource management
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * Programmed and designed by Matti 'ccr' Hamalainen
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 * (C) Copyright 2003-2012 Tecnic Software productions (TNSP)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include "dmres.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include <time.h>
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
11 DMResource *dmres_new(DMResourceLib *lib, const char *filename, int flags, size_t size)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 {
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
13 DMResource *node = dmMalloc0(sizeof(DMResource));
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
14 if (node == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
17 node->lib = lib;
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
18 node->filename = dm_strdup(filename);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
19 node->flags = flags;
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
20 node->dataSize = size;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
22 return node;
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
23 }
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
24
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
25
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
26 void dmres_free_res_data(DMResource *node)
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
27 {
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
28 if (node->rdata != NULL &&
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
29 node->rops != NULL &&
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
30 node->rops->free != NULL)
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
31 {
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
32 node->rops->free(node);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
33 }
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
34
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
35 node->rdata = NULL;
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
36 node->flags &= !DMF_LOADED_RES;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
40 void dmres_free_raw_data(DMResource *node)
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
41 {
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
42 dmFree(node->data);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
43 node->data = NULL;
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
44 node->flags &= !DMF_LOADED_RAW;
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
45 }
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
46
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
47
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
48 void dmres_purge_raw_data(DMResource *node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 {
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
50 if ((node->flags & DMF_PRELOAD_RAW) == 0 &&
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
51 (node->flags & DMF_LOADED_RAW) &&
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
52 node->data != NULL)
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
53 dmres_free_raw_data(node);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
54 }
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
55
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
56
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
57 void dmres_free(DMResource *node)
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
58 {
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
59 if (node != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 {
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
61 dmres_free_res_data(node);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
62 dmres_free_raw_data(node);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
63 dmFree(node->filename);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
64 dmFree(node);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
69 void dmres_insert(DMResourceLib *lib, DMResource * node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
71 if (lib == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
72 return;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
73
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
74 node->lib = lib;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
75
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
76 if (lib->resources != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
78 node->prev = lib->resources->prev;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
79 lib->resources->prev->next = node;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
80 lib->resources->prev = node;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
84 lib->resources = node->prev = node;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 node->next = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
91 void dmres_delete(DMResourceLib *lib, DMResource * node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
93 if (lib == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
94 return;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
95
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 if (node->prev)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 node->prev->next = node->next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 if (node->next)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 node->next->prev = node->prev;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 else
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
102 lib->resources->prev = node->prev;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 node->prev = node->next = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
108 DMResource * dmres_find(DMResourceLib *lib, const char *filename)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 DMResource *node, *found = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
112 if (lib == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
113 return NULL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
115 dmMutexLock(lib->mutex);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
116
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
117 for (node = lib->resources; node != NULL; node = node->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 if (strcmp(node->filename, filename) == 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 found = node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
126 dmMutexUnlock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 return found;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
132 #ifdef DM_USE_STDIO
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 /* Basic stdio file routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 static int dm_stdio_fopen(DMResource *handle)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 char *rfilename = dm_strdup_printf("%s%s", DMRES_DATA_PATH, handle->filename);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 if (rfilename == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 return DMERR_MALLOC;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 handle->fh = fopen(rfilename, "rb");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 dmFree(rfilename);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 handle->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 return (handle->fh != NULL) ? DMERR_OK : DMERR_FOPEN;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 static void dm_stdio_fclose(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 if (f->fh != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 fclose(f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 f->fh = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 static int dm_stdio_ferror(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 return f->error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 static int dm_stdio_fseek(DMResource *f, const off_t pos, const int whence)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 int ret = fseek(f->fh, pos, whence);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 static off_t dm_stdio_fsize(DMResource *f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 off_t savePos, fileSize;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 // Check if the size is cached
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 if (f->dataSize != 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 return f->dataSize;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 // Get file size
406
a0160ffdf7e5 Use fseeko() and ftello() to ensure 64-bit off_t compatibility in stdio backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 359
diff changeset
182 savePos = ftello(f->fh);
a0160ffdf7e5 Use fseeko() and ftello() to ensure 64-bit off_t compatibility in stdio backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 359
diff changeset
183 if (fseeko(f->fh, 0L, SEEK_END) != 0)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188
406
a0160ffdf7e5 Use fseeko() and ftello() to ensure 64-bit off_t compatibility in stdio backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 359
diff changeset
189 fileSize = ftello(f->fh);
a0160ffdf7e5 Use fseeko() and ftello() to ensure 64-bit off_t compatibility in stdio backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 359
diff changeset
190 if (fseeko(f->fh, savePos, SEEK_SET) != 0)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 f->dataSize = fileSize;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 return fileSize;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 static off_t dm_stdio_ftell(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 return ftell(f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 static BOOL dm_stdio_feof(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 return feof(f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 static int dm_stdio_fgetc(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 int ret = fgetc(f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
221 static int dm_stdio_fputc(int v, DMResource * f)
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
222 {
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
223 int ret = fputc(v, f->fh);
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
224 f->error = dmGetErrno();
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
225 return ret;
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
226 }
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
227
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
228
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 static size_t dm_stdio_fread(void *ptr, size_t size, size_t nmemb, DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 size_t ret = fread(ptr, size, nmemb, f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
237 static size_t dm_stdio_fwrite(void *ptr, size_t size, size_t nmemb, DMResource * f)
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
238 {
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
239 size_t ret = fwrite(ptr, size, nmemb, f->fh);
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
240 f->error = dmGetErrno();
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
241 return ret;
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
242 }
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
243
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
244
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 static int dm_stdio_preload(DMResource *handle)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 int ret = dm_stdio_fopen(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 if (ret != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 dm_stdio_fsize(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 handle->data = dmMalloc(handle->dataSize);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 if (handle->data == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 return DMERR_MALLOC;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 if (dm_stdio_fread(handle->data, sizeof(Uint8), handle->dataSize, handle) != handle->dataSize)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 return DMERR_FREAD;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 DMResourceOps dfStdioFileOps =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 dm_stdio_ferror,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 dm_stdio_fseek,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 dm_stdio_fsize,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 dm_stdio_ftell,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 dm_stdio_feof,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 dm_stdio_fgetc,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
272 dm_stdio_fputc,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 dm_stdio_fread,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
274 dm_stdio_fwrite,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 dm_stdio_fopen,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 dm_stdio_fclose,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 dm_stdio_preload
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 DMResourceOps dfStdioFHOps =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 dm_stdio_ferror,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 dm_stdio_fseek,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 dm_stdio_fsize,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 dm_stdio_ftell,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 dm_stdio_feof,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 dm_stdio_fgetc,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
289 dm_stdio_fputc,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 dm_stdio_fread,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
291 dm_stdio_fwrite,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 NULL
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 // Some mingw/windows headers define these as macros, which is bad for us
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 #ifdef __WIN32
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 #undef ferror
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 #undef feof
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 /*
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 * PACK file routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 */
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
310 #ifdef DM_USE_PACKFS
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 static int dm_pack_preload(DMResource *handle)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 DMPackEntry *node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 int res = DMERR_OK, cres, cdataLeft;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 z_stream cstream;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 Uint8 * cbuffer = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
318 if (handle->lib == NULL || handle->lib->packFile == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
319 return DMERR_NULLPTR;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
320
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 // Search PACK nodelist for file
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
322 if ((node = dm_pack_find(handle->lib->packFile->entries, handle->filename)) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 dmError("Entry '%s' not found in PACK file.\n", handle->filename);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 res = DMERR_NOT_FOUND;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 // Seek to entry
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
330 if (fseek(handle->lib->packFile->file, node->offset, SEEK_SET) == -1)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 dmError("Could not seek node position in PACK file.\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 res = DMERR_FSEEK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 // Allocate a structures and buffers
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 cbuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 if (cbuffer == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 res = DMERR_MALLOC;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 // Initialize fields
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 handle->dataOffset = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 handle->dataSize = node->size;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 handle->data = (Uint8 *) dmMalloc(node->size);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 if (handle->data == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 res = DMERR_MALLOC;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 // Initialize decompression
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 cstream.zalloc = (alloc_func) Z_NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 cstream.zfree = (free_func) Z_NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 cstream.opaque = (voidpf) Z_NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 cstream.next_out = handle->data;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 cstream.avail_out = handle->dataSize;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 cdataLeft = node->length;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 cres = inflateInit(&(cstream));
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 if (cres != Z_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 dmError("Could not initialize zlib stream inflation.\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 res = DMERR_INIT_FAIL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 // Uncompress the data
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 while (cdataLeft > 0 &&
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 cstream.avail_out > 0 && cres == Z_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 cstream.avail_in = fread(
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 cbuffer, sizeof(Uint8),
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 (cdataLeft >= DPACK_TMPSIZE) ? DPACK_TMPSIZE : cdataLeft,
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
377 handle->lib->packFile->file);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 cdataLeft -= cstream.avail_in;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 cstream.next_in = cbuffer;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 cres = inflate(&cstream, Z_FULL_FLUSH);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 // Cleanup
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 inflateEnd(&(cstream));
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 error:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 dmFree(cbuffer);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 return res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
393 static void dm_pack_fclose(DMResource * f)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 f->dataSize = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 f->dataOffset = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 dmFree(f->data);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 f->data = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 }
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
400 #endif
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 static int dm_mem_ferror(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 return f->error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 static int dm_mem_fseek(DMResource * f, const off_t offset, const int whence)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411 off_t newPos;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 // Calculate the new position
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 switch (whence)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416 case SEEK_SET:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 newPos = offset;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 case SEEK_CUR:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 newPos = f->dataOffset + offset;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424 case SEEK_END:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 newPos = f->dataSize + offset;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 default:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 // Set the new position
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 f->dataOffset = newPos;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 // Check the new position
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 if (newPos < 0 && (size_t) newPos >= f->dataSize)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 return 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 static off_t dm_mem_fsize(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 return f->dataSize;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 static off_t dm_mem_ftell(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 return f->dataOffset;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 static BOOL dm_mem_feof(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 // Check for EOF
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 if ((size_t) f->dataOffset <= f->dataSize)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 return TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 static int dm_mem_fgetc(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467 // Check for EOF
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 if ((size_t) f->dataOffset < f->dataSize)
439
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
469 return f->data[f->dataOffset++];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 return EOF;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 static size_t dm_mem_fread(void *buf, size_t size, size_t nmemb, DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 size_t length = (size * nmemb);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 // Check if we can read the whole chunk
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 if (((size_t) f->dataOffset + length) >= f->dataSize)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 nmemb = (f->dataSize - f->dataOffset) / size;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 length = size * nmemb;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 memcpy(buf, f->data + f->dataOffset, length);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487 f->dataOffset += length;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 return nmemb;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491
439
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
492 static int dm_mem_fputc(int ch, DMResource * f)
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
493 {
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
494 // Check for EOF
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
495 if ((size_t) f->dataOffset < f->dataSize)
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
496 {
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
497 f->data[f->dataOffset++] = ch;
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
498 return ch;
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
499 }
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
500 else
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
501 return EOF;
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
502 }
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
503
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
504
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
505 static size_t dm_mem_fwrite(void *buf, size_t size, size_t nmemb, DMResource * f)
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
506 {
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
507 size_t length = (size * nmemb);
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
508
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
509 // Check if we can write the whole chunk
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
510 if (((size_t) f->dataOffset + length) >= f->dataSize)
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
511 {
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
512 nmemb = (f->dataSize - f->dataOffset) / size;
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
513 length = size * nmemb;
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
514 }
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
515
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
516 if (length > 0)
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
517 {
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
518 memcpy(f->data + f->dataOffset, buf, length);
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
519 f->dataOffset += length;
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
520 }
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
521 return nmemb;
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
522 }
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
523
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
524
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
525
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
526 #ifdef DM_USE_PACKFS
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 DMResourceOps dfPackFileOps =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 dm_mem_ferror,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 dm_mem_fseek,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 dm_mem_fsize,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 dm_mem_ftell,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 dm_mem_feof,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534 dm_mem_fgetc,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
535 NULL,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 dm_mem_fread,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
537 NULL,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 NULL,
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
540 dm_pack_fclose,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 dm_pack_preload
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 };
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
543 #endif
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 DMResourceOps dfMemIOFileOps =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 dm_mem_ferror,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 dm_mem_fseek,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 dm_mem_fsize,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 dm_mem_ftell,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 dm_mem_feof,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 dm_mem_fgetc,
439
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
554 dm_mem_fputc,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555 dm_mem_fread,
439
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
556 dm_mem_fwrite,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 NULL
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 /* FS file handling functions. These functions call the actual
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 * functions depending on where the file is located.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 */
603
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
567 static void dmf_reset(DMResource *handle)
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
568 {
609
877dba1b8a1f Fix some resource code checks.
Matti Hamalainen <ccr@tnsp.org>
parents: 604
diff changeset
569 if (handle != NULL && handle->fops != NULL)
603
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
570 dmfseek(handle, 0, SEEK_SET);
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
571 }
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
572
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
573
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
574 static void dmf_init_fops(DMResource *handle)
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
575 {
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
576 // Check fops
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
577 if (handle->fops == NULL)
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
578 {
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
579 #ifdef DM_USE_PACKFS
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
580 if (handle->lib->flags & DRF_USE_PACK)
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
581 handle->fops = &dfPackFileOps;
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
582 # ifdef DM_USE_STDIO
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
583 else
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
584 handle->fops = &dfStdioFileOps;
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
585 # else
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
586 handle->fops = &dfPackFileOps;
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
587 # endif
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
588
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
589 #else
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
590 handle->fops = NULL;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
591 #endif
609
877dba1b8a1f Fix some resource code checks.
Matti Hamalainen <ccr@tnsp.org>
parents: 604
diff changeset
592
877dba1b8a1f Fix some resource code checks.
Matti Hamalainen <ccr@tnsp.org>
parents: 604
diff changeset
593 dmf_reset(handle);
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
594 }
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
595 }
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
596
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
597
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
598 int dmf_preload(DMResource *handle)
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
599 {
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
600 int ret = DMERR_INIT_FAIL;
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
601
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
602 // Check if we want to preload raw data?
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
603 if ((handle->flags & DMF_PRELOAD_RAW) ||
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
604 (handle->lib->flags & DRF_PRELOAD_ALL))
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
605 {
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
606 if (handle->flags & DMF_LOADED_RAW)
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
607 ret = DMERR_OK;
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
608 else
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
609 if (handle->fops->preload != NULL)
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
610 {
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
611 ret = handle->fops->preload(handle);
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
612 if (ret == DMERR_OK)
604
72c6120271a7 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 603
diff changeset
613 {
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
614 handle->flags |= DMF_LOADED_RAW;
604
72c6120271a7 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 603
diff changeset
615 }
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
616 }
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
617 }
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
618 else
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
619 {
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
620 if (handle->fops->fopen != NULL)
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
621 ret = handle->fops->fopen(handle);
603
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
622 else
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
623 if (handle->fops->preload != NULL)
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
624 ret = handle->fops->preload(handle);
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
625 }
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
626
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
627 // Check if resource data is to be preloaded
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
628 if ((handle->flags & DMF_PRELOAD_RES) || (handle->lib->flags & DRF_PRELOAD_RES))
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
629 {
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
630 if (handle->flags & DMF_LOADED_RES)
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
631 ret = DMERR_OK;
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
632 else
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
633 if (handle->rops != NULL &&
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
634 handle->rops->load != NULL)
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
635 {
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
636 ret = handle->rops->load(handle);
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
637 if (ret == DMERR_OK)
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
638 {
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
639 // Okay, mark as loaded
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
640 handle->flags |= DMF_LOADED_RES;
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
641
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
642 // Check if we can purge the raw data now
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
643 if ((handle->flags & DMF_PERSIST) == 0)
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
644 dmres_purge_raw_data(handle);
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
645 }
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
646 }
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
647 }
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
648
603
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
649 dmf_reset(handle);
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
650 return ret;
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
651 }
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
652
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
653
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
654 DMResource *dmf_open(DMResourceLib *lib, const char *filename)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
656 int ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657 DMResource *handle;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
658
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659 // Check master directory for resource
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
660 if ((handle = dmres_find(lib, filename)) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661 {
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
662 #ifdef DM_USE_STDIO
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663 // Hmm.. does not exist? Fall back to a stdio file
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
664 handle = dmres_new(lib, filename, 0, 0);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 if (handle == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
666 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
668 handle->fops = &dfStdioFileOps;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
669 dmres_insert(lib, handle);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670 #else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
671 // Stdio not enabled, fail
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
672 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
676 // Initialize file ops
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
677 dmf_init_fops(handle);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679 // Check if the data is preloaded
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
680 if (handle->flags & DMF_LOADED_RAW)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682 dmres_ref(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 return handle;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
686 // Try preloading
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
687 ret = dmf_preload(handle);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689 if (ret == DMERR_OK)
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
690 {
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
691 dmres_ref(handle);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
692 return handle;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
693 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
695 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
696 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
697
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
699 DMResource * dmf_create_memio(DMResourceLib *lib, const char *filename, Uint8 *buf, size_t len)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
700 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701 DMResource *handle;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
703 // Check master directory for resource
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
704 if ((handle = dmres_find(lib, filename)) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 // Hmm.. does not exist? Fall back to a stdio file
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
707 handle = dmres_new(lib, filename, DMF_LOADED_RAW, len);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708 if (handle == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
709 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711 handle->fops = &dfMemIOFileOps;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 handle->data = buf;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
713 dmres_insert(lib, handle);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
714 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
716 // Increase refcount
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
717 dmres_ref(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
718
603
36b544eb6f4b Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
719 dmf_reset(handle);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
720 return handle;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
721 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
722
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
724 #ifdef DM_USE_STDIO
74
23ac82365a65 Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
725 DMResource * dmf_create_stdio(const char *filename, const char *mode)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
726 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
727 DMResource *handle = dmres_new(NULL, filename, 0, 0);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
728 if (handle == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
729 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
731 handle->fops = &dfStdioFileOps;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732
74
23ac82365a65 Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
733 handle->fh = fopen(filename, mode);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
734 handle->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
735
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736 if (handle->fh != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738 dmres_ref(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739 return handle;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
740 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
741 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
743 dmres_free(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
744 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
745 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
746 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
747
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
748
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
749 DMResource * dmf_create_stdio_stream(FILE *fh)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
750 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
751 DMResource *handle = dmres_new(NULL, "", 0, 0);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752 if (handle == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
753 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
754
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755 handle->fops = &dfStdioFHOps;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
756 handle->fh = fh;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
757 dmres_ref(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758 return handle;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
763 void dmf_close(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765 if (f == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
766 return;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
767
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
768 if (f->fops->fclose != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
769 f->fops->fclose(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
770
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 dmres_unref(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
773
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
774
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775 int dmferror(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
776 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
778 return f->fops->ferror(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
779 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
780
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
781 int dmfseek(DMResource * f, off_t offset, int whence)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
782 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
783 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
784 return f->fops->fseek(f, offset, whence);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
786
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
787 off_t dmfsize(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
788 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
789 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
790 return f->fops->fsize(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
792
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
793 off_t dmftell(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
794 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
795 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
796 return f->fops->ftell(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
797 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
798
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
799 BOOL dmfeof(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
800 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
801 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
802 return f->fops->feof(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
803 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
804
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
805 int dmfgetc(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
806 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
807 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
808 return f->fops->fgetc(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
809 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
810
74
23ac82365a65 Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
811 int dmfputc(int v, DMResource * f)
72
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
812 {
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
813 f->atime = time(NULL);
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
814 return f->fops->fputc(v, f);
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
815 }
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
816
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
817 size_t dmfread(void *ptr, size_t size, size_t nmemb, DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
818 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
819 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
820 return f->fops->fread(ptr, size, nmemb, f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
821 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
822
72
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
823 size_t dmfwrite(void *ptr, size_t size, size_t nmemb, DMResource * f)
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
824 {
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
825 f->atime = time(NULL);
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
826 return f->fops->fwrite(ptr, size, nmemb, f);
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
827 }
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
828
235
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
829 char *dmfgets(char *s, int size, DMResource * f)
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
830 {
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
831 char *p = s, c;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
832 int n = 0;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
833
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
834 while ((c = f->fops->fgetc(f)) != EOF)
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
835 {
237
6d9d43bb68eb Oops, another silly issue in dmfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 236
diff changeset
836 n++;
235
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
837 if (c == '\n')
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
838 break;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
839 else
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
840 if (n < size - 1)
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
841 *p++ = c;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
842 }
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
843 *p = 0;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
844
236
8189be40ed7c Oops, dmfgets() had a nasty think-o and counted the remaining buffer space
Matti Hamalainen <ccr@tnsp.org>
parents: 235
diff changeset
845 return (n > 0) ? s : NULL;
235
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
846 }
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
847
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
848
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
849 int dmres_ref(DMResource *node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
850 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
851 if (node->lib != NULL) dmMutexLock(node->lib->mutex);
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
852 node->atime = time(NULL);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
853 node->refcount++;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
854 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
855
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
856 return node->refcount;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
857 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
858
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
859
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
860 int dmres_unref(DMResource *node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
861 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
862 if (node->lib != NULL) dmMutexLock(node->lib->mutex);
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
863 node->refcount--;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
864 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
865
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
866 return node->refcount;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
867 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
868
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
869
114
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
870 #define NADDFLAG(flg, ch) \
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
871 do { \
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
872 if ((flags & (flg)) && offs < size - 1) \
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
873 str[offs++] = ch; \
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
874 } while (0)
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
875
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
876 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
877 {
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
878 size_t offs = 0;
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
879 NADDFLAG(DMF_PRELOAD_RAW, 'r');
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
880 NADDFLAG(DMF_PRELOAD_RES, 'e');
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
881 NADDFLAG(DMF_PERSIST, 'p');
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
882 NADDFLAG(DMF_STREAM, 's');
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
883 if (offs < size)
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
884 str[offs] = 0;
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
885 }
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
886
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
887 #undef NADDFLAG
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
888
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
889
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
890 int dmres_symbolic_to_flags(const char *str)
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
891 {
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
892 int offs, flags;
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
893
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
894 for (flags = offs = 0; str[offs]; offs++)
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
895 switch (tolower(str[offs]))
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
896 {
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
897 case 'r': flags |= DMF_PRELOAD_RAW; break;
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
898 case 'e': flags |= DMF_PRELOAD_RES; break;
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
899 case 'p': flags |= DMF_PERSIST; break;
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
900 case 's': flags |= DMF_STREAM; break;
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
901 }
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
902 return flags;
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
903 }
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
904
e9de22e5a6d5 Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
905
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
906 int dmres_load_resfile(DMResourceLib *lib, const char *filename)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
907 {
33
b2fe4301384d Silence a minor warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
908 int ret = DMERR_OK;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
909 char line[256];
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
910 FILE *f = fopen(filename, "r");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
911 if (f == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
912 return DMERR_FOPEN;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
913
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
914 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
915
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
916 while (fgets(line, sizeof(line) - 1, f) != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
917 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
918 int fnstart, fsep;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
919 for (fnstart = 0; isspace(line[fnstart]); fnstart++);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
920 for (fsep = fnstart; line[fsep] && line[fsep] != '|'; fsep++);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
921 if (line[fsep] == '|')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
922 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
923 int flags, i;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
924 for (i = fsep - 1; i > 0 && isspace(line[i]); i--)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
925 line[i] = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
926
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
927 for (i = fsep; isspace(line[i]); i++);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
928 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
929 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
930
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
931 dmMutexUnlock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
932 fclose(f);
33
b2fe4301384d Silence a minor warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
933
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
934 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
935 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
936
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
937
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
938 int dmres_write_resfile(DMResourceLib *lib, const char *filename)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
939 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
940 int ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
941 DMResource *node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
942 FILE *f = fopen(filename, "w");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
943 if (f == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
944 return DMERR_FOPEN;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
945
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
946 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
947
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
948 for (node = lib->resources; node != NULL; node = node->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
949 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
950 char tmp[64];
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
951 dmres_flags_to_symbolic(tmp, sizeof(tmp), node->flags);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
952 if (fprintf(f, "%s|%s\n", node->filename, tmp) < 0)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
953 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
954 ret = DMERR_FWRITE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
955 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
956 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
957 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
958
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
959 error:
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
960 dmMutexUnlock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
961 fclose(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
962 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
963 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
964
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
965
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
966 /* Resources subsystem initialization and shutdown routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
967 */
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
968 int dmres_init(DMResourceLib **plib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
969 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
970 DMResourceLib *lib;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
971
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
972 // Allocate the resource library structure
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
973 if ((*plib = lib = dmMalloc0(sizeof(DMResourceLib))) == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
974 return DMERR_MALLOC;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
975
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
976 // Basic data
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
977 lib->mutex = dmCreateMutex();
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
978 lib->flags = flags;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
979 lib->resPath = dm_strdup((path != NULL) ? path : DMRES_DATA_PATH);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
980
107
1c2ff205fa0e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
981
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
982 if (flags & DRF_USE_PACK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
983 {
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
984 #ifdef DM_USE_PACKFS
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
985 int ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
986 DMPackEntry *node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
987
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
988 lib->packFilename = dm_strdup((filename != NULL) ? filename : DMRES_DATA_PACK);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
989
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
990 // Initialize PACK, open as read-only
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
991 ret = dm_pack_open(lib->packFilename, &lib->packFile, TRUE);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
992 if (ret != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
993 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
994 dmError("Error opening PACK file '%s', #%i: %s\n",
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
995 lib->packFilename, ret, dmErrorStr(ret));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
996
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
997 return DMERR_INIT_FAIL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
998 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
999
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1000 // Initialize resources from a PACK file
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1001 for (node = lib->packFile->entries; node != NULL; node = node->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1002 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1003 DMResource *res = dmres_new(lib, node->filename, node->resFlags & DMF_MASK, node->size);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1004 if (res == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1005 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1006 dmError("Could not allocate memory for resource node '%s' [0x%08x], %d.\n",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1007 node->filename, node->resFlags, node->size);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1008 return DMERR_INIT_FAIL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1009 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1010
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1011 dmres_insert(lib, res);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1012 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1013
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1014 #else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1015 // PACK not compiled in, FAIL!
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1016 return DMERR_INIT_FAIL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1017 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1018 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1019 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1020 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1021 // Initialize resources from a resource directory
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1022 char *resFilename = dm_strdup_printf("%s%s", lib->resPath, DMRES_RES_FILE);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1023 int ret = dmres_load_resfile(lib, resFilename);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1024 dmFree(resFilename);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1025
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1026 if (ret != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1027 return DMERR_INIT_FAIL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1028 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1029
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1030 // Okay, classify resources
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1031 if (lib->resources != NULL && classifier != NULL)
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1032 {
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1033 DMResource *node;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1034 for (node = lib->resources; node != NULL; node = node->next)
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1035 {
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1036 int ret = classifier(node);
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1037 if (ret != DMERR_OK)
107
1c2ff205fa0e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
1038 return ret;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1039 }
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1040 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1041
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1042 // Initialization complete
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1043 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1044 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1045
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1046
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1047 int dmres_close(DMResourceLib *lib)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1048 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1049 DMResource *node;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1050
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1051 if (lib == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1052 return DMERR_NULLPTR;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1053
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1054 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1055
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1056 // Shutdown possible subsystems
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
1057 #ifdef DM_USE_PACKFS
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1058 if (lib->flags & DRF_USE_PACK)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1059 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1060 int res = dm_pack_close(lib->packFile);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1061 if (res != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1062 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1063 dmError("Error closing PACK, #%i: %s\n",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1064 res, dmErrorStr(res));
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1065 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1066
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1067 dmFree(lib->packFilename);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1068 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1069 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1070
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1071 // Free resource entries
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1072 node = lib->resources;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1073 while (node != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1074 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1075 DMResource *next = node->next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1076 dmres_free(node);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1077 node = next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1078 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1079
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1080 // Etc.
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1081 dmFree(lib->resPath);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1082 dmMutexUnlock(lib->mutex);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1083 dmDestroyMutex(lib->mutex);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1084 return DMERR_OK;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1085 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1086
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1087
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1088 int dmres_preload(DMResourceLib *lib, BOOL start, int *loaded, int *total)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1089 {
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1090 int ret = DMERR_OK;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1091
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1092 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1093
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1094 // Initialize preloading
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1095 if (lib->preload == NULL || start)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1096 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1097 DMResource *node;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1098
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1099 lib->preload = lib->resources;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1100 *loaded = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1101 *total = 0;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1102
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1103 // Calculate total number of resources to be preloaded
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1104 for (node = lib->resources; node != NULL; node = node->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1105 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1106 if ((lib->flags & (DRF_PRELOAD_ALL | DRF_PRELOAD_RES)) ||
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1107 (node->flags & (DMF_PRELOAD_RAW | DMF_PRELOAD_RES)))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1108 (*total)++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1109 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1110 }
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1111 else
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1112 if (lib->preload != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1113 {
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1114 // Initialize fops and preload
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1115 dmf_init_fops(lib->preload);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1116 if ((ret = dmf_preload(lib->preload)) != DMERR_OK)
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1117 goto error;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1118
29
e9f562f07cb0 Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1119 (*loaded)++;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1120 lib->preload = lib->preload->next;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1121 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1122
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1123 dmMutexUnlock(lib->mutex);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1124 return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1125
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1126 error:
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1127 dmMutexUnlock(lib->mutex);
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1128 return ret;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1129 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1130
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1131
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1132 void dmres_prune(DMResourceLib *lib, int agems, int flags)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1133 {
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1134 DMResource *node;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1135 int currtime = time(NULL);
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1136 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1137
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1138 for (node = lib->resources; node != NULL; node = node->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1139 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1140 // Check if node has refcount of 0 and is
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1141 // not marked as persistent resource
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1142 if (node->refcount == 0 &&
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1143 (node->flags & DMF_PERSIST) == 0 &&
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1144 (node->flags & (DMF_LOADED_RES | DMF_LOADED_RAW)))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1145 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1146 // Check if we match either one of atime or mtime
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1147 if (((flags & DMPRUNE_ATIME) &&
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1148 currtime - node->atime >= agems) ||
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1149 ((flags & DMPRUNE_MTIME) &&
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1150 currtime - node->mtime >= agems))
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1151 {
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1152 dmres_free_res_data(node);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1153 dmres_free_raw_data(node);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1154 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1155 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1156 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1157
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1158 dmMutexUnlock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1159 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1160
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1161
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1162 /* Helper resource access routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1163 */
553
b60220fd1669 s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents: 439
diff changeset
1164 int dmf_read_str(DMResource *f, void *s, size_t l)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1165 {
553
b60220fd1669 s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents: 439
diff changeset
1166 return dmfread(s, 1, l, f) == l;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1167 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1168
437
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1169 BOOL dmf_read_byte(DMResource *f, Uint8 *val)
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1170 {
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1171 int tmp = dmfgetc(f);
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1172 *val = tmp;
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1173 return tmp != EOF;
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1174 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1175
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1176 #define DM_DEFINE_FUNC(xname, xtype, xmacro) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1177 BOOL dmf_read_ ## xname (DMResource *f, xtype *v) { \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1178 xtype result; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1179 if (dmfread(&result, sizeof( xtype ), 1, f) != 1) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1180 return FALSE; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1181 *v = DM_ ## xmacro ## _TO_NATIVE (result); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1182 return TRUE; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1183 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1184
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1185 DM_DEFINE_FUNC(le16, Uint16, LE16)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1186 DM_DEFINE_FUNC(le32, Uint32, LE32)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1187
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1188 DM_DEFINE_FUNC(be16, Uint16, BE16)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1189 DM_DEFINE_FUNC(be32, Uint32, BE32)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1190
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1191 #ifdef DM_HAVE_64BIT
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1192 DM_DEFINE_FUNC(le64, Uint64, LE64)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1193 DM_DEFINE_FUNC(be64, Uint64, BE64)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1194 #endif