annotate dmres.c @ 625:e8fc083b7499

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