forked from kilobyte/lastbeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileman.c
563 lines (457 loc) · 13.4 KB
/
fileman.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* Allegro port Copyright (C) 2014 Gavin Smith. */
/*-------------------------------------------------------*/
/* */
/* F I L E M A N A G E R */
/* [c] copyright 1993 by Alpha-Helix */
/* written by Dany Schoch */
/* */
/* Revision List: */
/* 27. May 93: Memory peak meter added. */
/* 28. : >64kB file load error corrected. */
/* 30. June : 'shutfilemanager' crash corrected. */
/* 22. Sept : 'openfile' added. */
/* 30. Nov : 'errno.h' error compatibility added. */
/* 19. Dez : 'printbuffer' added. */
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include "fileman.h"
#define TRUE 1
#define FALSE 0
typedef unsigned long ulong;
typedef unsigned int uint;
#define HDRSIZE 30
/* Size of file header as it appears on disk. sizeof(filestrc) may
be bigger because of padding. */
#define SIZE_OFFSET 14
#define FLAGS_OFFSET (14 + 4)
#define FPTR_OFFSET (14 + 4 + 2)
#define SIZE_OF_FILE_HEADER (14 + 4 + 2 + 4)
struct filestrc {
char name[14]; // Name of file.
ulong size; // It's size.
int flags; // Buffering flags.
long fptr; // File pointer to access it's data.
};
/* TODO: allocate as much as is needed */
#define MAXPATH 256
struct dbasestrc {
char basename[MAXPATH]; // Path and name of the open data base.
FILE *filvar; // File handle of data base.
int nfiles; // # of files in data base.
struct filestrc file[];
};
struct entrystrc {
char name[14]; // Name of the buffered file.
ulong size;
char *addr;
int hits; // Cache hit counter.
int flags;
};
static char *xmspointer; // Pointer to free XMS.
static ulong freexms; // Free extended mem.
static int nentries; // # of table entries.
static struct entrystrc *entry; // Entry table.
static void (*error)(char *text, int code, ...);
static void errf(char *text, int code, ...)
{
return;
}
/*------------------------------------------------------
Function: initfilemanager
Description: Initializes the Alpha-Helix file manager.
handles: max # of files that can be buffered.
minsize:\ extended memory that will be allocated
maxsize:/ (in kB)
err : Pointer to alternative error handler.
Returns: kBytes allocated on success.
0 not enough free XMS memory.
-1 not enough main memory.
------------------------------------------------------*/
int initfilemanager(int handles,
int minsize, int maxsize,
void (*err)(char *text, int code, ...))
{
// Install custom error handler.
if (err == NULL) error = errf; else error = err;
entry = NULL;
/* Always allocate the maxsize. */
xmspointer = malloc (freexms = maxsize);
if (!xmspointer) return -1;
// Allocate memory for the buffer table.
nentries = handles;
entry = (struct entrystrc *)calloc(nentries, sizeof(struct entrystrc));
if (entry == NULL) {
return -1; // Fatal error.
}
return 1; // Successful
}
/*------------------------------------------------------
Function: shutfilemanager
Description: Leaves the filemanager and frees the allocated
memory.
NOTE: memory allocated by 'opendatabase'
won't be freed.
------------------------------------------------------*/
void shutfilemanager(void)
{
if (entry != NULL) free(entry);
#ifdef DEBUG
printf("\nDOS free memory: %ld bytes.\n", mindosmem);
#endif
}
#ifdef DEBUG
void printbuffer(void)
{
int i;
printf("Name Size Addr Hits\n");
printf("--------------------------------------\n");
for (i = 0; i < nentries; i++) {
if (entry[i].size != 0)
printf("%s %ld %lx %d\n",
entry[i].name, entry[i].size, entry[i].addr, entry[i].hits);
}
}
#endif
// -- Start of private area.
static struct entrystrc *findfreeentry(void)
{
int i;
for (i = 0; i < nentries; i++) {
if (entry[i].size == 0) break;
}
if (entry[i].size == 0) return &entry[i]; else return NULL;
}
static struct entrystrc *findentry(char *file)
{
int i;
struct entrystrc *result;
result = NULL;
if (entry != NULL) {
for (i = 0; i < nentries; i++) {
if (strcmp(file, entry[i].name) == 0) {
result = &entry[i];
break;
}
}
}
return result;
}
static void freeentry(int i)
{
ulong size;
struct entrystrc *result;
// Move memory blocks.
result = &entry[i];
size = result->size;
memmove (result->addr, result->addr+size,
xmspointer-result->addr-result->size);
freexms += size;
xmspointer -= size; // Adjust free xms pointer.
// Move index table.
if (i < nentries-1) {
memmove(result, result+1, (nentries-i-1)*sizeof(struct entrystrc));
}
for ( ; i < nentries-1; i++) entry[i].addr -= size;
entry[nentries-1].size = 0; // Mark free slot.
}
static int lightxms(void)
{
int count;
int i;
struct entrystrc *result;
count = 0;
do {
// Look for a block to remove.
result = NULL;
for (i = 0; i < nentries; i++) {
if ((entry[i].hits == count) && (entry[i].size != 0)) {
result = &entry[i];
break;
}
}
if (result) {
freeentry(i);
// Decrement hitcounts.
for (i = 0; i < nentries; i++)
if (entry[i].hits > 0) entry[i].hits--;
return TRUE;
}
} while (count++ < 20);
return FALSE;
}
static void buffer(char *file, void *data, ulong size, int flags)
{
struct entrystrc *result;
if ((flags & M_XMS)) {
// Buffer in extended memory.
if (!(flags & M_NOFREEUP)) {
while (freexms < size)
if (!lightxms()) break; // Try to make space.
}
result = findfreeentry();
if (result != NULL && (freexms >= size)) {
strcpy(result->name, file);
result->size = size;
result->addr = xmspointer;
result->hits = 0;
result->flags = flags;
memmove(result->addr, data, size);
freexms -= size;
xmspointer += size; // Move pointer to remaining free XMS.
}
}
}
// --- End of private area.
/*------------------------------------------------------
Function: opendatabase
Description: Opens a special type of file: The Alpha-Helix
James Bond Baller game data library file
type.
------------------------------------------------------*/
void *opendatabase(char *file)
{
FILE *filvar;
int nfiles;
uint nread;
struct dbasestrc *ptr;
char hdr[HDRSIZE];
int mode;
char byte;
#ifndef DATADIR
#define DATADIR "."
#endif
char *fullpath = alloca (strlen (DATADIR) + 1 + strlen (file) + 1);
sprintf (fullpath, "%s/%s", DATADIR, file);
/* Look for installed data file; failing that, look in current
directory. */
if (!(filvar = fopen(fullpath, "rb"))
&& !(filvar = fopen(file, "rb"))) {
(*error)("opendatabase", ENOENT, file);
return NULL;
}
fread (hdr, HDRSIZE, 1, filvar);
fread (&byte, 1, 1, filvar);
mode = byte;
fread (&byte, 1, 1, filvar);
mode += byte << 8;
fread (&byte, 1, 1, filvar);
nfiles = byte;
fread (&byte, 1, 1, filvar);
nfiles += byte << 8;
ptr = malloc(sizeof(struct dbasestrc) + nfiles*SIZE_OF_FILE_HEADER);
if (ptr == NULL) {
fclose(filvar);
(*error)("opendatabase", ENOENT);
return NULL;
}
// Fill in the newly created structure.
strcpy(ptr->basename, file);
ptr->filvar = filvar;
ptr->nfiles = nfiles;
fread ((void *)ptr->file,
nfiles*SIZE_OF_FILE_HEADER,
1,
filvar);
return (void *)ptr;
}
/*------------------------------------------------------
Function: closedatabase
Description: Undoes the effect of a previous 'opendatabase'.
------------------------------------------------------*/
void closedatabase(void *database)
{
struct dbasestrc *ptr;
ptr = (struct dbasestrc *)database;
fclose(ptr->filvar);
free(ptr);
}
/*------------------------------------------------------
Function: loadfile
Description: Reserves enough memory to hold the file
in memory and loads it from the database.
------------------------------------------------------*/
unsigned char *loadfile(void *database, char *file)
{
struct entrystrc *result;
struct dbasestrc *ptr;
unsigned char *data;
unsigned char *p;
ulong fsize, msize; // file and memory size of file respectively.
int i, j;
uint nread;
ptr = (struct dbasestrc *)database;
result = findentry(file);
if (result != NULL) {
// File is in buffer.
if (result->hits < 20) result->hits++;
// Allocated memory and copy block.
if ((data = malloc(result->size)) == NULL) {
(*error)("loadfile", ENOMEM);
return NULL;
}
memmove (data, result->addr, result->size);
} else {
ulong size;
int flags;
long fptr;
unsigned char *rec_ptr;
// File must be read from disk.
for (i = 0, j = -1; i < ptr->nfiles; i++) {
rec_ptr = (unsigned char *) (&ptr->file) + i*SIZE_OF_FILE_HEADER;
/* File record begins with name so a pointer to it is a pointer
to the name. */
if (strcmp(file, rec_ptr) == 0) {
j = i;
break;
}
}
if (j == -1) {
(*error)("loadfile", ENOENT, file);
return NULL;
}
/* Read 4 byte size record */
size = rec_ptr [SIZE_OFFSET];
size += rec_ptr [SIZE_OFFSET + 1] << 8;
size += rec_ptr [SIZE_OFFSET + 2] << 16;
size += rec_ptr [SIZE_OFFSET + 3] << 24;
/* Read 2 byte flags record */
flags = rec_ptr [FLAGS_OFFSET];
flags += rec_ptr [FLAGS_OFFSET + 1] << 8;
/* Read 4 byte fptr record */
fptr = rec_ptr [FPTR_OFFSET];
fptr += rec_ptr [FPTR_OFFSET + 1] << 8;
fptr += rec_ptr [FPTR_OFFSET + 2] << 16;
fptr += rec_ptr [FPTR_OFFSET + 3] << 24;
fseek(ptr->filvar, fptr, SEEK_SET);
fsize = size;
msize = (fsize + 1) & 0xfffffffel; // make it even.
if ((data = malloc(msize)) == NULL) {
(*error)("loadfile", ENOENT);
return NULL;
}
p = data;
while (fsize > 65000) {
fread(p, 1, 65000, ptr->filvar);
fsize -= 65000; p += 65000;
}
nread = fread(p, 1, fsize, ptr->filvar);
buffer(file, data, size, flags);
}
return data;
}
/*------------------------------------------------------
Function: loadfiledirect
Description: Allocates memory for the file and loads
it if present from the buffer, otherwise
from disk.
------------------------------------------------------*/
void *loadfiledirect(char *file, int flags)
{
FILE *filvar;
struct entrystrc *result;
void *ptr;
char *p;
ulong fsize, msize; // real size and size in memory of file.
uint nread;
result = findentry(file);
if (result != NULL) {
// File is in buffer.
if (result->hits < 20) result->hits++;
// Allocated memory and copy block.
if ((ptr = malloc(result->size)) == NULL) {
(*error)("loadfiledirect", ENOMEM);
return NULL;
}
memmove (ptr, result->addr, result->size);
} else {
// File must be read from disk.
if (!(filvar = fopen (file, "rb"))) {
(*error)("loadfiledirect", ENOENT, file);
return NULL;
}
/* Calculate length of file */
fseek(filvar, 0, SEEK_END);
fsize = ftell (filvar);
rewind (filvar);
msize = (fsize + 1) & 0xfffffffel; // round it up.
if ((ptr = malloc(msize)) == NULL) {
fclose(filvar);
(*error)("loadfiledirect", ENOMEM);
return NULL;
}
p = ptr;
while (fsize > 65000) {
nread = fread(p, 1, 65000, filvar);
fsize -= 65000; p += 65000;
}
nread = fread(p, 1, fsize, filvar);
fclose(filvar);
buffer(file, ptr, msize, flags);
}
return ptr;
}
void unloadfile(void *ptr)
{
free(ptr);
}
/*------------------------------------------------------
Function: openfile
Description: Same as loadfile, but doesn't read it into
memory. Just sets the file pointer to
the start of the file.
Returns: File handle for further access, null if failed.
------------------------------------------------------*/
FILE *openfile(void *database, char *file)
{
/* Not converted yet. */
return 0;
#if 0
struct dbasestrc *ptr; // Pointer to data base directory.
int i, j;
FILE *filvar;
ptr = (struct dbasestrc *)database;
for (i = 0, j = -1; i < ptr->nfiles; i++) {
if (strcmp(file, ptr->file[i].name) == 0) {
j = i;
break;
}
}
if (j == -1) {
(*error)("openfile", ENOENT, file);
return 0;
}
// Re-open database to get a free file pointer.
if (!(filvar = fopen (ptr->basename, "rb"))) {
(*error)("openfile", ENOENT, ptr->basename);
return 0;
}
// Seek to data.
fseek(filvar, ptr->file[j].fptr, SEEK_SET);
return filvar;
#endif
}
/*------------------------------------------------------
Function: openfiledirect
Description: In fact this is the same as open the file
for read only using fopen.
------------------------------------------------------*/
FILE *openfiledirect(char *file)
{
FILE *filvar;
if (!(filvar = fopen (file, "rb"))) {
(*error)("openfiledirect", ENOENT, file);
return 0;
}
return filvar;
}
/*------------------------------------------------------
Function: closefile
Description:
------------------------------------------------------*/
void closefile(FILE *filvar)
{
fclose(filvar);
}