annotate src/dmres.c @ 872:b01d04e44b6f

Assume we always have 64bit type.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 03 Feb 2015 20:58:34 +0200
parents 27949209238b
children 985225a93aeb
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
863
27949209238b Update copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
5 * (C) Copyright 2003-2015 Tecnic Software productions (TNSP)
0
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
719
eb4bd1f7b679 Sanitize some more includes.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
10 #ifdef DM_USE_PACKFS
eb4bd1f7b679 Sanitize some more includes.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
11 #include <zlib.h>
eb4bd1f7b679 Sanitize some more includes.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
12 #endif
eb4bd1f7b679 Sanitize some more includes.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
13
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
14 #ifdef DM_USE_STDIO
630
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
15 #include <sys/types.h>
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
16 #include <sys/stat.h>
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
17 #include <unistd.h>
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
18 #include <dirent.h>
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
19 #endif
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
20
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
22 DMResource *dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 {
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
24 DMResource *node = dmMalloc0(sizeof(DMResource));
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
25 if (node == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
28 node->lib = lib;
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
29 node->filename = dm_strdup(filename);
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
30 node->rawSize = 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
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
36 void dmResourceFreeResData(DMResource *node)
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
37 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
38 if (node->resData != NULL &&
27
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
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
45 node->resData = NULL;
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
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
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
50 void dmResourceFreeRawData(DMResource *node)
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
51 {
731
0e164aaa7409 Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents: 730
diff changeset
52 if ((node->flags & DMF_UNALLOCATED) == 0)
0e164aaa7409 Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents: 730
diff changeset
53 {
0e164aaa7409 Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents: 730
diff changeset
54 dmFree(node->rawData);
0e164aaa7409 Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents: 730
diff changeset
55 node->rawData = NULL;
0e164aaa7409 Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents: 730
diff changeset
56 node->flags &= ~DMF_LOADED_RAW;
0e164aaa7409 Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents: 730
diff changeset
57 }
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
58 }
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
59
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
60
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
61 void dmResourceFree(DMResource *node)
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
62 {
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
63 if (node != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
65 dmResourceFreeResData(node);
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
66 dmResourceFreeRawData(node);
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
67 dmFree(node->filename);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
68 dmFree(node);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
73 void dmResourceInsert(DMResourceLib *lib, DMResource * node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 {
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
75 if (lib == NULL || node == NULL)
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
76 return;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
77
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
78 node->lib = lib;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
79
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
80 if (lib->resources != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
82 node->prev = lib->resources->prev;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
83 lib->resources->prev->next = node;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
84 lib->resources->prev = node;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 else
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 lib->resources = node->prev = node;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 node->next = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
95 void dmResourceDelete(DMResourceLib *lib, DMResource * node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
97 if (lib == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
98 return;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
99
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 if (node->prev)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 node->prev->next = node->next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 if (node->next)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 node->next->prev = node->prev;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 else
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
106 lib->resources->prev = node->prev;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 node->prev = node->next = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
112 DMResource * dmResourceFind(DMResourceLib *lib, const char *filename)
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 DMResource *node, *found = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
116 if (lib == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
117 return NULL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
119 dmMutexLock(lib->mutex);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
120
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
121 for (node = lib->resources; node != NULL; node = node->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 if (strcmp(node->filename, filename) == 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 found = node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
130 dmMutexUnlock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 return found;
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
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
136 #ifdef DM_USE_STDIO
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 /* Basic stdio file routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 static int dm_stdio_fopen(DMResource *handle)
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 char *rfilename = dm_strdup_printf("%s%s", DMRES_DATA_PATH, handle->filename);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 if (rfilename == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 return DMERR_MALLOC;
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 handle->fh = fopen(rfilename, "rb");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 dmFree(rfilename);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 handle->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 return (handle->fh != NULL) ? DMERR_OK : DMERR_FOPEN;
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 static void dm_stdio_fclose(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 if (f->fh != NULL)
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 fclose(f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 f->fh = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 }
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
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 static int dm_stdio_ferror(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 return f->error;
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_fseek(DMResource *f, const off_t pos, const int whence)
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 int ret = fseek(f->fh, pos, whence);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 return ret;
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
177 static int dm_stdio_freset(DMResource * f)
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
178 {
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
179 if (f->fh != NULL)
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
180 return dm_stdio_fseek(f, 0L, SEEK_SET);
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
181 else
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
182 return DMERR_OK;
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
183 }
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
184
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
185
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 static off_t dm_stdio_fsize(DMResource *f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 off_t savePos, fileSize;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 // Check if the size is cached
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
191 if (f->rawSize != 0)
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
192 return f->rawSize;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 // 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
195 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
196 if (fseeko(f->fh, 0L, SEEK_END) != 0)
0
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 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201
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
202 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
203 if (fseeko(f->fh, savePos, SEEK_SET) != 0)
0
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 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
209 f->rawSize = fileSize;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 return fileSize;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 static off_t dm_stdio_ftell(DMResource * f)
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 return ftell(f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 static BOOL dm_stdio_feof(DMResource * f)
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 return feof(f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 }
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 static int dm_stdio_fgetc(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 int ret = fgetc(f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
234 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
235 {
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
236 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
237 f->error = dmGetErrno();
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
238 return ret;
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
239 }
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
240
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
241
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 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
243 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 size_t ret = fread(ptr, size, nmemb, f->fh);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 f->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
250 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
251 {
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
252 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
253 f->error = dmGetErrno();
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
254 return ret;
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
255 }
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
256
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
257
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 static int dm_stdio_preload(DMResource *handle)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 int ret = dm_stdio_fopen(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 if (ret != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 dm_stdio_fsize(handle);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
266 handle->rawData = dmMalloc(handle->rawSize);
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
267 if (handle->rawData == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 return DMERR_MALLOC;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
270 if (dm_stdio_fread(handle->rawData, sizeof(Uint8), handle->rawSize, handle) != handle->rawSize)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 return DMERR_FREAD;
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 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 }
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 DMResourceOps dfStdioFileOps =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 {
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
279 "Stdio",
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
280
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
281 dm_stdio_freset,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 dm_stdio_ferror,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 dm_stdio_fseek,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 dm_stdio_fsize,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 dm_stdio_ftell,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 dm_stdio_feof,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 dm_stdio_fgetc,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
288 dm_stdio_fputc,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 dm_stdio_fread,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
290 dm_stdio_fwrite,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 dm_stdio_fopen,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 dm_stdio_fclose,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 dm_stdio_preload
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 DMResourceOps dfStdioFHOps =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 {
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
299 "StdioFH",
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
300
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
301 dm_stdio_freset,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 dm_stdio_ferror,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 dm_stdio_fseek,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 dm_stdio_fsize,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 dm_stdio_ftell,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 dm_stdio_feof,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 dm_stdio_fgetc,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
308 dm_stdio_fputc,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 dm_stdio_fread,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
310 dm_stdio_fwrite,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 NULL
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 #endif
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 // 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
320 #ifdef __WIN32
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 #undef ferror
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 #undef feof
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 /*
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 * PACK file routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 */
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
329 #ifdef DM_USE_PACKFS
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 static int dm_pack_preload(DMResource *handle)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 DMPackEntry *node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 int res = DMERR_OK, cres, cdataLeft;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 z_stream cstream;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 Uint8 * cbuffer = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
337 if (handle->lib == NULL || handle->lib->packFile == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
338 return DMERR_NULLPTR;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
339
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 // Search PACK nodelist for file
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
341 if ((node = dmPackFind(handle->lib->packFile->entries, handle->filename)) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 dmError("Entry '%s' not found in PACK file.\n", handle->filename);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 res = DMERR_NOT_FOUND;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 goto error;
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 // Seek to entry
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
349 if (fseek(handle->lib->packFile->file, node->offset, SEEK_SET) == -1)
0
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 dmError("Could not seek node position in PACK file.\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 res = DMERR_FSEEK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 goto error;
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 // Allocate a structures and buffers
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 cbuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 if (cbuffer == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 res = DMERR_MALLOC;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 }
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 // Initialize fields
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
365 handle->rawOffset = 0;
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
366 handle->rawSize = node->size;
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
367 handle->rawData = (Uint8 *) dmMalloc(node->size);
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
368 if (handle->rawData == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 res = DMERR_MALLOC;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 // Initialize decompression
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 cstream.zalloc = (alloc_func) Z_NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 cstream.zfree = (free_func) Z_NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 cstream.opaque = (voidpf) Z_NULL;
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
378 cstream.next_out = handle->rawData;
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
379 cstream.avail_out = handle->rawSize;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 cdataLeft = node->length;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 cres = inflateInit(&(cstream));
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 if (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 dmError("Could not initialize zlib stream inflation.\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 res = DMERR_INIT_FAIL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 goto error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 }
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 // Uncompress the data
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 while (cdataLeft > 0 &&
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 cstream.avail_out > 0 && cres == Z_OK)
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 cstream.avail_in = fread(
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 cbuffer, sizeof(Uint8),
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 (cdataLeft >= DPACK_TMPSIZE) ? DPACK_TMPSIZE : cdataLeft,
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
396 handle->lib->packFile->file);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 cdataLeft -= cstream.avail_in;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 cstream.next_in = cbuffer;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 cres = inflate(&cstream, Z_FULL_FLUSH);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 // Cleanup
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 inflateEnd(&(cstream));
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 error:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 dmFree(cbuffer);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 return res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
412 static int dm_pack_fopen(DMResource * f)
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
413 {
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
414 if ((f->flags & DMF_LOADED_RAW) == 0)
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
415 {
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
416 int res = dm_pack_preload(f);
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
417 if (res == DMERR_OK)
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
418 f->flags |= DMF_LOADED_RAW;
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
419
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
420 return res;
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
421 }
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
422 else
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
423 return DMERR_OK;
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
424 }
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
425
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
426
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
427 static void dm_pack_fclose(DMResource * f)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 {
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
429 if ((f->flags & DMF_PERSIST) == 0)
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
430 dmResourceFreeRawData(f);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 }
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
432 #endif
0
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
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
435 static int dm_mem_freset(DMResource * f)
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
436 {
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
437 f->rawOffset = 0;
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
438 return DMERR_OK;
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
439 }
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
440
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
441
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 static int dm_mem_ferror(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 return f->error;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 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
449 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 off_t newPos;
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 // Calculate the new position
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 switch (whence)
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 case SEEK_SET:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 newPos = offset;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 break;
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 case SEEK_CUR:
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
460 newPos = f->rawOffset + offset;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 break;
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 case SEEK_END:
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
464 newPos = f->rawSize + offset;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 break;
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 default:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 // Set the new position
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
472 f->rawOffset = newPos;
0
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 // Check the new position
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
475 if (newPos < 0 && (size_t) newPos >= f->rawSize)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 return 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 static off_t dm_mem_fsize(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
484 return f->rawSize;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 static off_t dm_mem_ftell(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
490 return f->rawOffset;
0
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 static BOOL dm_mem_feof(DMResource * f)
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 // Check for EOF
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
497 if ((size_t) f->rawOffset <= f->rawSize)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 return TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 static int dm_mem_fgetc(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 // Check for EOF
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
507 if ((size_t) f->rawOffset < f->rawSize)
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
508 return f->rawData[f->rawOffset++];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 return EOF;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 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
515 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 size_t length = (size * nmemb);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 // Check if we can read the whole chunk
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
519 if (((size_t) f->rawOffset + length) >= f->rawSize)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
521 nmemb = (f->rawSize - f->rawOffset) / size;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 length = size * nmemb;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
525 memcpy(buf, f->rawData + f->rawOffset, length);
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
526 f->rawOffset += length;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 return nmemb;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530
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
531 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
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 // Check for EOF
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
534 if ((size_t) f->rawOffset < f->rawSize)
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
535 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
536 f->rawData[f->rawOffset++] = ch;
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
537 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
538 }
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
539 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
540 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
541 }
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
542
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
543
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
544 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
545 {
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
546 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
547
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
548 // Check if we can write the whole chunk
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
549 if (((size_t) f->rawOffset + length) >= f->rawSize)
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
550 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
551 nmemb = (f->rawSize - f->rawOffset) / size;
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
552 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
553 }
bc1da1f4cb4b Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents: 437
diff changeset
554
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
555 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
556 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
557 memcpy(f->rawData + f->rawOffset, buf, length);
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
558 f->rawOffset += length;
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
559 }
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
560 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
561 }
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
562
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
563
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
564 #ifdef DM_USE_PACKFS
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 DMResourceOps dfPackFileOps =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 {
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
567 "PackFS",
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
568
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
569 dm_mem_freset,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 dm_mem_ferror,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 dm_mem_fseek,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 dm_mem_fsize,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573 dm_mem_ftell,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 dm_mem_feof,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 dm_mem_fgetc,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
576 NULL,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577 dm_mem_fread,
59
c560703e85ed Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
578 NULL,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
580 dm_pack_fopen,
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
581 dm_pack_fclose,
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
582 dm_pack_preload,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 };
166
112eb95f2fba Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
584 #endif
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 DMResourceOps dfMemIOFileOps =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 {
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
589 "MemIO",
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
590
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
591 dm_mem_freset,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 dm_mem_ferror,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 dm_mem_fseek,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 dm_mem_fsize,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 dm_mem_ftell,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 dm_mem_feof,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 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
598 dm_mem_fputc,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599 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
600 dm_mem_fwrite,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 NULL,
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
603 dmResourceFree,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 NULL
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608 /* FS file handling functions. These functions call the actual
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609 * functions depending on where the file is located.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610 */
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
611 static int dmResourcePreload(DMResource *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
612 {
733
b505b81a43de Fix resource loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 732
diff changeset
613 int ret = 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
614
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
615 // Check if we want to preload raw data?
737
897646df1de1 Handle preloading of resources that do not have rops.
Matti Hamalainen <ccr@tnsp.org>
parents: 736
diff changeset
616 if ((handle->lib->flags & DRF_PRELOAD_RAW) ||
897646df1de1 Handle preloading of resources that do not have rops.
Matti Hamalainen <ccr@tnsp.org>
parents: 736
diff changeset
617 handle->rops == NULL || 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
618 {
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
619 if (handle->flags & DMF_LOADED_RAW)
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
620 ret = DMERR_OK;
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
621 else
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
622 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
623 {
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
624 ret = handle->fops->preload(handle);
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
625 if (ret == DMERR_OK)
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
626 handle->flags |= DMF_LOADED_RAW | DMF_PERSIST;
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
627 }
733
b505b81a43de Fix resource loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 732
diff changeset
628 else
b505b81a43de Fix resource loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 732
diff changeset
629 ret = DMERR_INIT_FAIL;
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
630
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
631 dmfreset(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
632 }
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
633
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
634 // Check if resource data is to be preloaded
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
635 if (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
636 {
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
637 if (handle->flags & DMF_LOADED_RES)
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
638 ret = DMERR_OK;
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
639 else
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
640 if (handle->rops != NULL &&
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
641 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
642 {
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
643 if ((ret = handle->fops->fopen(handle)) == DMERR_OK)
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
644 {
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
645 ret = handle->rops->load(handle);
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
646 handle->fops->fclose(handle);
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
647 }
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
648
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
649 if (ret == DMERR_OK)
602
c1a5652e473d Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
650 handle->flags |= DMF_LOADED_RES;
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
651 }
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
652
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
653 dmfreset(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
654 }
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
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
656 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
657 }
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
658
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
659
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
660 int dmf_open(DMResourceLib *lib, const char *filename, DMResource **phandle)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661 {
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
662 DMResource *handle;
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
663 int res;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
664
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 // Check master directory for resource
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
666 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667 {
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
668 #ifdef DM_USE_STDIO
613
15cd5b8a29b2 Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
669 if (lib->flags & DRF_USE_STDIO)
15cd5b8a29b2 Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
670 {
15cd5b8a29b2 Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
671 // Hmm.. does not exist? Fall back to a stdio file
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
672 *phandle = handle = dmResourceNew(lib, filename, 0);
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
673 if (handle == NULL)
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
674 return DMERR_MALLOC;
613
15cd5b8a29b2 Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
675
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
676 handle->fops = &dfStdioFileOps;
613
15cd5b8a29b2 Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
677 }
15cd5b8a29b2 Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
678 else
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
679 return DMERR_INIT_FAIL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680 #else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681 // Stdio not enabled, fail
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
682 return DMERR_INIT_FAIL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 // Check if the data is preloaded
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
687 if ((res = handle->fops->fopen(handle)) == DMERR_OK)
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
688 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
689 dmResourceRef(handle);
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
690 if (handle->flags & DMF_TEMPORARY)
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
691 {
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
692 handle->flags &= ~DMF_TEMPORARY;
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
693 dmResourceInsert(lib, handle);
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
694 }
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
695 }
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
696 else
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
697 if (handle->flags & DMF_TEMPORARY)
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
698 {
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
699 dmResourceFree(handle);
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
700 *phandle = handle = NULL;
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
701 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702
737
897646df1de1 Handle preloading of resources that do not have rops.
Matti Hamalainen <ccr@tnsp.org>
parents: 736
diff changeset
703 dmfreset(handle);
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
704 return res;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
708 int dmf_create_memio(DMResourceLib *lib, const char *filename,
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
709 Uint8 *buf, const size_t size, DMResource **phandle)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711 DMResource *handle;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
713 // Check master directory for resource
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
714 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715 {
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
716 if ((*phandle = handle = dmResourceNew(lib, filename, size)) == NULL)
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
717 return DMERR_MALLOC;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
718
731
0e164aaa7409 Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents: 730
diff changeset
719 handle->flags = DMF_LOADED_RAW | DMF_UNALLOCATED;
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
720 handle->fops = &dfMemIOFileOps;
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
721 handle->rawData = buf;
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
722 dmResourceInsert(lib, handle);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723 }
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 // Increase refcount
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
726 dmResourceRef(handle);
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
727 dmfreset(handle);
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
728 return DMERR_OK;
0
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
731
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
732 #ifdef DM_USE_STDIO
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
733 int dmf_create_stdio(const char *filename, const char *mode, DMResource **phandle)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
734 {
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
735 DMResource *handle;
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
736 if ((*phandle = handle = dmResourceNew(NULL, filename, 0)) == NULL)
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
737 return DMERR_MALLOC;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
739 handle->fops = &dfStdioFileOps;
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
740 handle->fh = fopen(filename, mode);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
741 handle->error = dmGetErrno();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
743 if (handle->fh == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
744 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
745 dmResourceFree(handle);
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
746 return handle->error;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
747 }
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
748
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
749 dmResourceRef(handle);
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
750 return DMERR_OK;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
751 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
753
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
754 int dmf_create_stdio_stream(FILE *fh, DMResource **phandle)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755 {
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
756 DMResource *handle;
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
757 if ((*phandle = handle = dmResourceNew(NULL, "", 0)) == NULL)
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
758 return DMERR_MALLOC;
0
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 handle->fops = &dfStdioFHOps;
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
761 handle->fh = fh;
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
762 dmResourceRef(handle);
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 728
diff changeset
763 return DMERR_OK;
0
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 #endif
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 void dmf_close(DMResource * f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
769 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
770 if (f == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 return;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
773 if (f->fops->fclose != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
774 f->fops->fclose(f);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
776 dmResourceUnref(f);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777 }
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
732
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
780 int dmfreset(DMResource *f)
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
781 {
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
782 if (f == NULL)
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
783 return DMERR_NULLPTR;
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
784
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
785 if (f->fops == NULL || f->fops->freset == NULL)
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
786 return DMERR_OK;
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
787
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
788 return f->fops->freset(f);
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
789 }
2f02d9b4caa3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 731
diff changeset
790
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791 int dmferror(DMResource * f)
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 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
794 return f->fops->ferror(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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
797 int dmfseek(DMResource * f, off_t offset, int whence)
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 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
800 return f->fops->fseek(f, offset, 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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
803 off_t dmfsize(DMResource * f)
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 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
806 return f->fops->fsize(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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
809 off_t dmftell(DMResource * 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 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
812 return f->fops->ftell(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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
815 BOOL dmfeof(DMResource * 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 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
818 return f->fops->feof(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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
821 int dmfgetc(DMResource * 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 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
824 return f->fops->fgetc(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
74
23ac82365a65 Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
827 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
828 {
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
829 f->atime = time(NULL);
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
830 return f->fops->fputc(v, f);
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
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
833 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
834 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
835 f->atime = time(NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
836 return f->fops->fread(ptr, size, nmemb, 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
72
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
839 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
840 {
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
841 f->atime = time(NULL);
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
842 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
843 }
be6160981428 Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
844
235
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
845 char *dmfgets(char *s, int size, DMResource * f)
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
846 {
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
847 char *p = s, c;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
848 int n = 0;
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 while ((c = f->fops->fgetc(f)) != EOF)
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
851 {
237
6d9d43bb68eb Oops, another silly issue in dmfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 236
diff changeset
852 n++;
235
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
853 if (c == '\n')
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
854 break;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
855 else
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
856 if (n < size - 1)
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
857 *p++ = c;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
858 }
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
859 *p = 0;
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
860
236
8189be40ed7c Oops, dmfgets() had a nasty think-o and counted the remaining buffer space
Matti Hamalainen <ccr@tnsp.org>
parents: 235
diff changeset
861 return (n > 0) ? s : NULL;
235
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
862 }
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
863
4a672d96978f Add dmfgets() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
864
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
865 int dmResourceRef(DMResource *node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
867 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
868 node->atime = time(NULL);
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
869 node->refcount++;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
870 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
871
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
872 return node->refcount;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
873 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
874
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
875
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
876 int dmResourceUnref(DMResource *node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
877 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
878 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
879 node->refcount--;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
880 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
881
27
21c14afbf63d Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
882 return node->refcount;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
883 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
884
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
885
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
886 #ifdef DM_USE_STDIO
727
3d74dc794a0a Fix function name.
Matti Hamalainen <ccr@tnsp.org>
parents: 722
diff changeset
887 static int dmResourcesLoadDirectory(DMResourceLib *lib, const char *path)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
888 {
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
889 int res = DMERR_OK;
630
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
890 struct dirent *dh;
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
891 DIR *hdir = opendir(path);
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
892 if (hdir == NULL)
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
893 return dmGetErrno();
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
894
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
895 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
896
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
897 do
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
898 {
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
899 DMResource *node = NULL;
630
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
900 dh = readdir(hdir);
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
901 if (dh != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
902 {
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
903 struct stat sbuf;
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
904 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
905 if (stat(fname, &sbuf) == -1)
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
906 {
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
907 res = dmGetErrno();
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
908 dmError("Could not stat() %s, #%d: %s\n",
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
909 fname, res, dmErrorStr(res));
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
910 dmFree(fname);
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
911 goto out;
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
912 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
913
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
914 if (S_ISREG(sbuf.st_mode))
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
915 node = dmResourceNew(lib, dh->d_name, sbuf.st_size);
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
916 }
630
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
917
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
918 if (node != NULL)
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
919 {
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
920 node->fops = &dfStdioFileOps;
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
921 dmResourceInsert(lib, node);
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
922 }
630
6e2b53f2878b Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
923 } while (dh != NULL);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
924
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
925 out:
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
926 dmMutexUnlock(lib->mutex);
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 #ifdef __WIN32
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
929 #else
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
930 closedir(hdir);
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
931 #endif
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
932
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
933 return res;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
934 }
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
935 #endif
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
936
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
937
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
938 /* Resources subsystem initialization and shutdown routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
939 */
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
940 int dmResourcesInit(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
941 {
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
942 DMResourceLib *lib;
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
943 BOOL initialized = FALSE;
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
944
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
945 // Allocate the resource library structure
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
946 if ((*plib = lib = dmMalloc0(sizeof(DMResourceLib))) == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
947 return DMERR_MALLOC;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
948
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
949 // Basic data
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
950 lib->mutex = dmCreateMutex();
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
951 lib->flags = flags;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
952 lib->resPath = dm_strdup((path != NULL) ? path : DMRES_DATA_PATH);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
953
107
1c2ff205fa0e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
954
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
955 #ifdef DM_USE_PACKFS
734
56c1d288f0e2 Add some debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 733
diff changeset
956 if (flags & DRF_USE_PACK)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
957 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
958 int ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
959 DMPackEntry *node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
960
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
961 lib->packFilename = dm_strdup((filename != NULL) ? filename : DMRES_DATA_PACK);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
962
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
963 // Initialize PACK, open as read-only
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
964 ret = dmPackOpen(lib->packFilename, &lib->packFile, TRUE);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
965 if (ret != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
966 {
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
967 if ((flags & DRF_USE_STDIO) == 0)
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
968 {
734
56c1d288f0e2 Add some debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 733
diff changeset
969 dmError("Error opening PACK file '%s', #%d: %s\n",
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
970 lib->packFilename, ret, dmErrorStr(ret));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
971
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
972 return DMERR_INIT_FAIL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
973 }
734
56c1d288f0e2 Add some debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 733
diff changeset
974 else
56c1d288f0e2 Add some debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 733
diff changeset
975 dmError("Failed to open PACK, falling back to STDIO, '%s' %d: %s\n",
56c1d288f0e2 Add some debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 733
diff changeset
976 lib->packFilename, ret, dmErrorStr(ret));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
977 }
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
978 else
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 // Initialize resources from a PACK file
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
981 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
982 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
983 DMResource *res = dmResourceNew(lib, node->filename, node->size);
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
984 if (res == NULL)
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
985 {
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
986 dmError("Could not allocate memory for resource node '%s' [0x%08x], %d.\n",
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
987 node->filename, node->flags, node->size);
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
988 return DMERR_INIT_FAIL;
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
989 }
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
990
736
ea42faa02a9a Set packfile fileops in the appropriate handles.
Matti Hamalainen <ccr@tnsp.org>
parents: 734
diff changeset
991 res->fops = &dfPackFileOps;
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
992 dmResourceInsert(lib, res);
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
993 }
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
994
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
995 initialized = TRUE;
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
996 }
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
997 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
998 #endif
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
999
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
1000 #ifdef DM_USE_STDIO
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
1001 if (!initialized && (flags & DRF_USE_STDIO))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1002 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1003 // Initialize resources from a resource directory
727
3d74dc794a0a Fix function name.
Matti Hamalainen <ccr@tnsp.org>
parents: 722
diff changeset
1004 int ret = dmResourcesLoadDirectory(lib, lib->resPath);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1005 if (ret != DMERR_OK)
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
1006 return ret;
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
1007 initialized = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1008 }
625
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
1009 #endif
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
1010
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
1011 if (!initialized)
e8fc083b7499 More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
1012 return DMERR_INIT_FAIL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1013
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1014 // Okay, classify resources
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1015 if (lib->resources != NULL && classifier != NULL)
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1016 {
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1017 DMResource *node;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1018 for (node = lib->resources; node != NULL; node = node->next)
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1019 {
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1020 int ret = classifier(node);
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1021 if (ret != DMERR_OK)
107
1c2ff205fa0e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
1022 return ret;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1023 }
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1024 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1025
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1026 // Initialization complete
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1027 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1028 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1029
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1030
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1031 int dmResourcesClose(DMResourceLib *lib)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1032 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1033 DMResource *node;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1034
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1035 if (lib == NULL)
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1036 return DMERR_NULLPTR;
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1037
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1038 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1039
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1040 // Shutdown possible subsystems
124
d5cfd29c87c4 Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
1041 #ifdef DM_USE_PACKFS
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1042 if (lib->flags & DRF_USE_PACK)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1043 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1044 int res = dmPackClose(lib->packFile);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1045 if (res != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1046 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1047 dmError("Error closing PACK, #%i: %s\n",
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1048 res, dmErrorStr(res));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1049 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1050
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1051 dmFree(lib->packFilename);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1052 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1053 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1054
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1055 // Free resource entries
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1056 node = lib->resources;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1057 while (node != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1058 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1059 DMResource *next = node->next;
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1060 dmResourceFree(node);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1061 node = next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1062 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1063
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1064 // Etc.
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1065 dmFree(lib->resPath);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1066 dmMutexUnlock(lib->mutex);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1067 dmDestroyMutex(lib->mutex);
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1068 return DMERR_OK;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1069 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1070
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1071
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1072 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1073 {
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1074 int ret = DMERR_OK;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1075
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1076 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1077
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1078 // Initialize preloading
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1079 if (lib->preload == NULL || start)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1080 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1081 DMResource *node;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1082
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1083 lib->preload = lib->resources;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1084 *loaded = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1085 *total = 0;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1086
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1087 // Calculate total number of resources to be preloaded
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1088 if (lib->flags & (DRF_PRELOAD_RAW | DRF_PRELOAD_RES))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1089 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1090 for (node = lib->resources; node != NULL; node = node->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1091 (*total)++;
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 }
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1094 else
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1095 if (lib->preload != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1096 {
728
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
1097 // Attempt to preload the resource
867fc7f205a8 More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 727
diff changeset
1098 if ((ret = dmResourcePreload(lib->preload)) != DMERR_OK)
628
0533f94e843b Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents: 627
diff changeset
1099 {
0533f94e843b Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents: 627
diff changeset
1100 dmError("Error preloading '%s', %d: %s\n",
0533f94e843b Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents: 627
diff changeset
1101 lib->preload->filename, ret, dmErrorStr(ret));
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
1102 goto error;
628
0533f94e843b Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents: 627
diff changeset
1103 }
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1104
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
1105 (*loaded)++;
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1106 lib->preload = lib->preload->next;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1107 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1108
359
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 return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS;
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1111
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1112 error:
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1113 dmMutexUnlock(lib->mutex);
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1114 return ret;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1115 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1116
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1117
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1118 void dmResourcePrune(DMResourceLib *lib, const int agems, int const flags)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1119 {
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1120 DMResource *node;
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1121 const int stamp = time(NULL);
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1122 dmMutexLock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1123
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1124 for (node = lib->resources; node != NULL; node = node->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1125 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1126 // Check if node has refcount of 0 and is
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1127 // not marked as persistent resource
26
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1128 if (node->refcount == 0 &&
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1129 (node->flags & DMF_PERSIST) == 0 &&
2f463a59d732 Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1130 (node->flags & (DMF_LOADED_RES | DMF_LOADED_RAW)))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1131 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1132 if (((flags & DMPRUNE_ATIME) && stamp - node->atime >= agems) ||
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1133 ((flags & DMPRUNE_MTIME) && stamp - node->mtime >= agems))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1134 {
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1135 dmResourceFreeResData(node);
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
1136 dmResourceFreeRawData(node);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1137 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1138 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1139 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1140
359
59045853853d Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
1141 dmMutexUnlock(lib->mutex);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1142 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1143
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1144
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1145 /* Helper resource access routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1146 */
553
b60220fd1669 s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents: 439
diff changeset
1147 int dmf_read_str(DMResource *f, void *s, size_t l)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1148 {
553
b60220fd1669 s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents: 439
diff changeset
1149 return dmfread(s, 1, l, f) == l;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1150 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1151
437
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1152 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
1153 {
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1154 int tmp = dmfgetc(f);
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1155 *val = tmp;
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1156 return tmp != EOF;
3d9c044ec08d Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 406
diff changeset
1157 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1158
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1159 #define DM_DEFINE_FUNC(xname, xtype, xmacro) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1160 BOOL dmf_read_ ## xname (DMResource *f, xtype *v) { \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1161 xtype result; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1162 if (dmfread(&result, sizeof( xtype ), 1, f) != 1) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1163 return FALSE; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1164 *v = DM_ ## xmacro ## _TO_NATIVE (result); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1165 return TRUE; \
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1168 DM_DEFINE_FUNC(le16, Uint16, LE16)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1169 DM_DEFINE_FUNC(le32, Uint32, LE32)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1170
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1171 DM_DEFINE_FUNC(be16, Uint16, BE16)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1172 DM_DEFINE_FUNC(be32, Uint32, BE32)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1173
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1174 DM_DEFINE_FUNC(le64, Uint64, LE64)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1175 DM_DEFINE_FUNC(be64, Uint64, BE64)