Skip to content

Latest commit

 

History

History
964 lines (767 loc) · 23.5 KB

physfs.c

File metadata and controls

964 lines (767 loc) · 23.5 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* PhysicsFS; a portable, flexible file i/o abstraction.
*
* Documentation is in physfs.h. It's verbose, honest. :)
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "physfs.h"
Jul 6, 2001
Jul 6, 2001
17
18
19
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
20
21
22
23
24
25
26
27
28
29
30
typedef struct __PHYSFS_ERRMSGTYPE__
{
int tid;
int errorAvailable;
char errorString[80];
struct __PHYSFS_ERRMSGTYPE__ *next;
} ErrMsg;
typedef struct __PHYSFS_SEARCHDIRINFO__
{
char *dirName;
Jul 6, 2001
Jul 6, 2001
31
DirReader *reader;
32
33
34
struct __PHYSFS_SEARCHDIRINFO__ *next;
} SearchDirInfo;
Jul 6, 2001
Jul 6, 2001
35
36
37
38
39
typedef struct __PHYSFS_FILEHANDLELIST__
{
FileHandle *handle;
struct __PHYSFS_FILEHANDLELIST__ *next;
} FileHandleList;
Jul 6, 2001
Jul 6, 2001
41
42
/* The various i/o drivers... */
Jul 6, 2001
Jul 6, 2001
43
44
#if (defined PHYSFS_SUPPORTS_ZIP)
Jul 6, 2001
Jul 6, 2001
45
46
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
Jul 6, 2001
Jul 6, 2001
47
#endif
Jul 6, 2001
Jul 6, 2001
49
extern const DirFunctions __PHYSFS_DirFunctions_DIR;
Jul 6, 2001
Jul 6, 2001
50
Jul 6, 2001
Jul 6, 2001
51
static const PHYSFS_ArchiveInfo *supported_types[] =
52
53
{
#if (defined PHYSFS_SUPPORTS_ZIP)
Jul 6, 2001
Jul 6, 2001
54
&__PHYSFS_ArchiveInfo_ZIP,
55
56
57
58
59
#endif
NULL
};
Jul 6, 2001
Jul 6, 2001
60
61
62
63
64
65
66
67
68
static const DirFunctions *dirFunctions[] =
{
#if (defined PHYSFS_SUPPORTS_ZIP)
&__PHYSFS_DirFunctions_ZIP,
#endif
&__PHYSFS_DirFunctions_DIR,
NULL
};
Jul 6, 2001
Jul 6, 2001
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* General PhysicsFS state ... */
static int initialized = 0;
static ErrMsg *errorMessages = NULL;
static SearchDirInfo *searchPath = NULL;
static FileHandleList *openWriteList = NULL;
static FileHandleList *openReadList = NULL;
static char *baseDir = NULL;
static char *userDir = NULL;
static char *writeDir = NULL;
static int allowSymLinks = 0;
/* functions ... */
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
static ErrMsg *findErrorForCurrentThread(void)
{
ErrMsg *i;
int tid;
if (errorMessages != NULL)
{
tid = __PHYSFS_platformGetThreadID();
for (i = errorMessages; i != NULL; i = i->next)
{
if (i->tid == tid)
return(i);
} /* for */
} /* if */
return(NULL); /* no error available. */
} /* findErrorForCurrentThread */
Jul 6, 2001
Jul 6, 2001
108
void __PHYSFS_setError(const char *str)
109
110
111
112
113
114
115
116
117
118
{
ErrMsg *err = findErrorForCurrentThread();
if (err == NULL)
{
err = (ErrMsg *) malloc(sizeof (ErrMsg));
if (err == NULL)
return; /* uhh...? */
err->tid = __PHYSFS_platformGetThreadID();
Jul 6, 2001
Jul 6, 2001
119
120
err->next = errorMessages;
errorMessages = err;
121
122
123
124
125
} /* if */
err->errorAvailable = 1;
strncpy(err->errorString, str, sizeof (err->errorString));
err->errorString[sizeof (err->errorString) - 1] = '\0';
Jul 6, 2001
Jul 6, 2001
126
} /* __PHYSFS_setError */
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
const char *PHYSFS_getLastError(void)
{
ErrMsg *err = findErrorForCurrentThread();
if ((err == NULL) || (!err->errorAvailable))
return(NULL);
err->errorAvailable = 0;
return(err->errorString);
} /* PHYSFS_getLastError */
void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
{
if (ver != NULL)
{
ver->major = PHYSFS_VER_MAJOR;
ver->minor = PHYSFS_VER_MINOR;
ver->patch = PHYSFS_VER_PATCH;
} /* if */
} /* PHYSFS_getLinkedVersion */
Jul 6, 2001
Jul 6, 2001
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
static const char *calculateUserDir(void)
{
char *retval = NULL;
const char *str = NULL;
str = __PHYSFS_platformGetUserDir();
if (str != NULL)
retval = str;
else
{
const char *dirsep = PHYSFS_getDirSeparator();
const char *uname = __PHYSFS_platformGetUserName();
str = (uname != NULL) ? uname : "default";
retval = malloc(strlen(baseDir) + strlen(str) +
(strlen(dirsep) * 2) + 6);
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
else
sprintf(retval, "%s%susers%s%s", baseDir, dirsep, dirsep, str);
if (uname != NULL)
free(uname);
} /* else */
return(retval);
} /* calculateUserDir */
182
183
184
185
186
int PHYSFS_init(const char *argv0)
{
BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
BAIL_IF_MACRO(argv0 == NULL, ERR_INVALID_ARGUMENT, 0);
Jul 6, 2001
Jul 6, 2001
187
188
189
190
191
192
193
194
195
196
197
198
baseDir = calculateBaseDir(argv0);
if (baseDir == NULL)
return(0);
userDir = calculateUserDir();
if (userDir == NULL)
{
free(baseDir);
baseDir = NULL;
return(0);
} /* if */
199
200
201
202
203
204
205
initialized = 1;
return(1);
} /* PHYSFS_init */
static void freeSearchDir(SearchDirInfo *sdi)
{
Jul 6, 2001
Jul 6, 2001
206
FileHandleList *i;
Jul 6, 2001
Jul 6, 2001
207
Jul 6, 2001
Jul 6, 2001
208
209
210
211
212
213
assert(sdi != NULL);
for (i = openReadList; i != NULL; i = i->next)
{
BAIL_IF_MACRO(i->handle->dirReader == sdi->reader,
ERR_FILES_OPEN_READ, 0);
} /* for */
Jul 6, 2001
Jul 6, 2001
214
215
sdi->reader->close(sdi->reader);
216
217
218
219
220
free(sdi->dirName);
free(sdi);
} /* freeSearchDir */
Jul 6, 2001
Jul 6, 2001
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
static void closeFileHandleList(FileHandleList **list)
{
FileHandleList *i;
FileHandleList *next = NULL;
for (i = *list; i != NULL; i = next)
{
next = i->next;
i->handle->close(i->handle);
} /* for */
*list = NULL;
} /* closeAllFiles */
236
237
238
239
240
static void freeSearchPath(void)
{
SearchDirInfo *i;
SearchDirInfo *next = NULL;
Jul 6, 2001
Jul 6, 2001
241
242
closeFileHandleList(&openReadList);
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
if (searchPath != NULL)
{
for (i = searchPath; i != NULL; i = next)
{
next = i;
freeSearchDir(i);
} /* for */
searchPath = NULL;
} /* if */
} /* freeSearchPath */
void PHYSFS_deinit(void)
{
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
Jul 6, 2001
Jul 6, 2001
259
closeFileHandleList(&openWriteList);
260
261
262
263
PHYSFS_setWriteDir(NULL);
freeSearchPath();
freeErrorMessages();
Jul 6, 2001
Jul 6, 2001
264
if (baseDir != NULL)
Jul 6, 2001
Jul 6, 2001
265
{
Jul 6, 2001
Jul 6, 2001
266
free(baseDir);
Jul 6, 2001
Jul 6, 2001
267
268
269
270
271
272
273
274
baseDir = NULL;
} /* if */
if (userDir != NULL)
{
free(userDir);
userDir = NULL;
} /* if */
Jul 6, 2001
Jul 6, 2001
275
276
allowSymLinks = 0;
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
initialized = 0;
return(1);
} /* PHYSFS_deinit */
const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
{
return(supported_types);
} /* PHYSFS_supportedArchiveTypes */
void PHYSFS_freeList(void *list)
{
void **i;
for (i = (void **) list; *i != NULL; i++)
free(*i);
free(list);
} /* PHYSFS_freeList */
const char *PHYSFS_getDirSeparator(void)
{
return(__PHYSFS_pathSeparator);
} /* PHYSFS_getDirSeparator */
char **PHYSFS_getCdRomDirs(void)
{
return(__PHYSFS_platformDetectAvailableCDs());
} /* PHYSFS_getCdRomDirs */
const char *PHYSFS_getBaseDir(void)
{
return(baseDir); /* this is calculated in PHYSFS_init()... */
} /* PHYSFS_getBaseDir */
const char *PHYSFS_getUserDir(void)
{
Jul 6, 2001
Jul 6, 2001
318
return(userDir); /* this is calculated in PHYSFS_init()... */
319
320
321
322
323
324
325
326
327
328
329
} /* PHYSFS_getUserDir */
const char *PHYSFS_getWriteDir(void)
{
return(writeDir);
} /* PHYSFS_getWriteDir */
int PHYSFS_setWriteDir(const char *newDir)
{
Jul 6, 2001
Jul 6, 2001
330
BAIL_IF_MACRO(openWriteList != NULL, ERR_FILES_OPEN_WRITE, 0);
331
332
333
334
335
336
337
if (writeDir != NULL)
{
free(writeDir);
writeDir = NULL;
} /* if */
Jul 6, 2001
Jul 6, 2001
338
339
340
if (newDir != NULL)
{
BAIL_IF_MACRO(!createDirs_dependent(newDir), ERR_NO_DIR_CREATE, 0);
Jul 6, 2001
Jul 6, 2001
342
343
writeDir = malloc(strlen(newDir) + 1);
BAIL_IF_MACRO(writeDir == NULL, ERR_OUT_OF_MEMORY, 0);
Jul 6, 2001
Jul 6, 2001
345
346
strcpy(writeDir, newDir);
} /* if */
347
348
349
350
351
return(1);
} /* PHYSFS_setWriteDir */
Jul 6, 2001
Jul 6, 2001
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
static DirReader *getDirReader(const char *d)
{
DirFunctions **i;
for (i = dirFunctions; *i != NULL; i++)
{
if ((*i)->isArchive(d))
return( (*i)->openArchive(d) );
} /* for */
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
return(NULL);
} /* getDirReader */
367
368
369
370
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
{
char *str = NULL;
SearchDirInfo *sdi = NULL;
Jul 6, 2001
Jul 6, 2001
371
DirReader *dirReader = NULL;
372
373
374
BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
Jul 6, 2001
Jul 6, 2001
375
reader = getDirReader(newDir); /* This sets the error message. */
376
377
378
379
if (reader == NULL)
return(0);
sdi = (SearchDirInfo *) malloc(sizeof (SearchDirInfo));
Jul 6, 2001
Jul 6, 2001
380
381
if (sdi == NULL)
reader->close(reader);
382
383
384
385
386
387
BAIL_IF_MACRO(sdi == NULL, ERR_OUT_OF_MEMORY, 0);
sdi->dirName = (char *) malloc(strlen(newDir) + 1);
if (sdi->dirName == NULL)
{
free(sdi);
Jul 6, 2001
Jul 6, 2001
388
reader->close(reader);
Jul 6, 2001
Jul 6, 2001
389
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
390
391
392
return(0);
} /* if */
Jul 6, 2001
Jul 6, 2001
393
sdi->dirReader = dirReader;
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
strcpy(sdi->dirName, newDir);
if (appendToPath)
{
sdi->next = searchPath;
searchPath = sdi;
} /* if */
else
{
SearchDirInfo *i = searchPath;
SearchDirInfo *prev = NULL;
sdi->next = NULL;
while (i != NULL)
prev = i;
if (prev == NULL)
searchPath = sdi;
else
prev->next = sdi;
} /* else */
return(1);
} /* PHYSFS_addToSearchPath */
int PHYSFS_removeFromSearchPath(const char *oldDir)
{
SearchDirInfo *i;
SearchDirInfo *prev = NULL;
Jul 6, 2001
Jul 6, 2001
424
SearchDirInfo *next = NULL;
425
426
427
428
429
430
431
BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
for (i = searchPath; i != NULL; i = i->next)
{
if (strcmp(i->dirName, oldDir) == 0)
{
Jul 6, 2001
Jul 6, 2001
432
433
434
435
next = i->next;
if (!freeSearchDir(i))
return(0);
436
if (prev == NULL)
Jul 6, 2001
Jul 6, 2001
437
searchPath = next;
438
else
Jul 6, 2001
Jul 6, 2001
439
prev->next = next;
Jul 6, 2001
Jul 6, 2001
440
441
442
443
444
445
return(1);
} /* if */
prev = i;
} /* for */
Jul 6, 2001
Jul 6, 2001
446
__PHYSFS_setError(ERR_NOT_IN_SEARCH_PATH);
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
return(0);
} /* PHYSFS_removeFromSearchPath */
char **PHYSFS_getSearchPath(void)
{
int count = 1;
int x;
SearchDirInfo *i;
char **retval;
for (i = searchPath; i != NULL; i = i->next)
count++;
retval = (char **) malloc(sizeof (char *) * count);
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
count--;
retval[count] = NULL;
for (i = searchPath, x = 0; x < count; i = i->next, x++)
{
retval[x] = (char *) malloc(strlen(i->dirName) + 1);
if (retval[x] == NULL) /* this is friggin' ugly. */
{
while (x > 0)
{
x--;
free(retval[x]);
} /* while */
free(retval);
Jul 6, 2001
Jul 6, 2001
478
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
479
480
481
482
483
484
485
486
487
488
return(NULL);
} /* if */
strcpy(retval[x], i->dirName);
} /* for */
return(retval);
} /* PHYSFS_getSearchPath */
Jul 6, 2001
Jul 6, 2001
489
int PHYSFS_setSaneConfig(const char *appName, const char *archiveExt,
490
491
int includeCdRoms, int archivesFirst)
{
Jul 6, 2001
Jul 6, 2001
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
const char *basedir = PHYSFS_getBaseDir();
const char *userdir = PHYSFS_getUserDir();
const char *dirsep = PHYSFS_getDirSeparator();
char *str;
int rc;
/* set write dir... */
str = malloc(strlen(userdir) + (strlen(appName) * 2) +
(strlen(dirsep) * 2) + 2);
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
sprintf(str, "%s%s.%s", userdir, dirsep, appName);
rc = PHYSFS_setWriteDir(str);
if (!rc)
return(0); /* error set by PHYSFS_setWriteDir() ... */
/* Put write dir related dirs on search path... */
PHYSFS_addToSearchPath(str, 1);
PHYSFS_mkdir(appName); /* don't care if this fails. */
strcat(str, dirSep);
strcat(str, appName);
PHYSFS_addToSearchPath(str, 1);
free(str);
/* Put base path stuff on search path... */
PHYSFS_addToSearchPath(basedir, 1);
str = malloc(strlen(basedir) + (strlen(appName) * 2) +
(strlen(dirsep) * 2) + 2);
if (str != NULL)
{
sprintf(str, "%s%s.%s", basedir, dirsep, appName);
PHYSFS_addToSearchPath(str, 1);
free(str);
} /* if */
/* handle CD-ROMs... */
if (includeCdRoms)
{
char **cds = PHYSFS_getCdRomDirs();
char **i;
for (i = cds; *i != NULL; i++)
{
PHYSFS_addToSearchPath(*i);
str = malloc(strlen(*i) + strlen(appName) + strlen(dirsep) + 1);
if (str != NULL)
{
sprintf(str, "%s%s%s", *i, dirsep, appName);
PHYSFS_addToSearchPath(str);
free(str);
} /* if */
} /* for */
PHYSFS_freeList(cds);
} /* if */
/* Root out archives, and add them to search path... */
if (archiveExt != NULL)
{
char **rc = PHYSFS_enumerateFiles("");
char **i;
int extlen = strlen(archiveExt);
char *ext;
for (i = rc; *i != NULL; i++)
{
int l = strlen(*i);
if ((l > extlen) && ((*i)[l - extlen - 1] == '.'));
{
ext = (*i) + (l - extlen);
if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
{
const char *d = PHYSFS_getRealDir(*i);
str = malloc(strlen(d) + strlen(dirsep) + l + 1);
if (str != NULL)
{
sprintf(str, "%s%s%s", d, dirsep, *i);
PHYSFS_addToSearchPath(d, str);
free(str);
} /* if */
} /* if */
} /* if */
} /* for */
PHYSFS_freeList(rc);
} /* if */
return(1);
577
578
579
} /* PHYSFS_setSaneConfig */
Jul 6, 2001
Jul 6, 2001
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* string manipulation in C makes my ass itch. */
/* be sure to free this crap after you're done with it. */
static char *convertToDependentNotation(const char *prepend,
const char *dirName,
const char *append)
{
const char *dirsep = PHYSFS_getDirSeparator();
int sepsize = strlen(dirsep);
char *str;
char *i1;
char *i2;
size_t allocSize;
allocSize = strlen(dirName) + strlen(writeDir) + sepsize + 1;
if (prepend != NULL)
allocSize += strlen(prepend) + sepsize;
if (append != NULL)
allocSize += strlen(append) + sepsize;
/* make sure there's enough space if the dir separator is bigger. */
if (sepsize > 1)
{
for (str = dirName; *str != '\0'; str++)
{
if (*str == '/')
allocSize += (sepsize - 1);
} /* for */
} /* if */
str = (char *) malloc(allocSize);
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
*str = '\0';
if (prepend)
{
strcpy(str, prepend);
strcat(str, dirsep);
} /* if */
for (i1 = dirName, i2 = str + strlen(str); *i1 != '\0'; i1++, i2++)
{
if (*i1 == '/')
{
strcpy(i2, dirsep);
i2 += sepsize;
} /* if */
else
{
*i2 = *i1;
} /* else */
} /* for */
*i2 = '\0';
if (append)
{
strcat(str, dirsep);
strcpy(str, append);
} /* if */
return(str);
} /* convertToDependentNotation */
643
644
int PHYSFS_mkdir(const char *dirName)
{
Jul 6, 2001
Jul 6, 2001
645
646
647
648
649
650
651
652
653
654
655
656
char *str;
int rc;
BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, NULL);
str = convertToDependentNotation(writeDir, dirName, NULL);
if (str == NULL) /* __PHYSFS_setError is called in convert call. */
return(0);
rc = createDirs_dependent(str);
free(str);
return(rc);
657
658
659
660
661
} /* PHYSFS_mkdir */
int PHYSFS_delete(const char *filename)
{
Jul 6, 2001
Jul 6, 2001
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
char *str;
int rc;
BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, NULL);
str = convertToDependentNotation(writeDir, fileName, NULL);
if (str == NULL) /* __PHYSFS_setError is called in convert call. */
return(0);
rc = remove(str);
free(str);
rc = (rc == 0);
if (!rc)
__PHYSFS_setError(strerror(errno));
return(rc);
679
680
681
682
683
} /* PHYSFS_delete */
void PHYSFS_permitSymbolicLinks(int allow)
{
Jul 6, 2001
Jul 6, 2001
684
allowSymLinks = allow;
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
} /* PHYSFS_permitSymbolicLinks */
/**
* Figure out where in the search path a file resides. The file is specified
* in platform-independent notation. The returned filename will be the
* element of the search path where the file was found, which may be a
* directory, or an archive. Even if there are multiple matches in different
* parts of the search path, only the first one found is used, just like
* when opening a file.
*
* So, if you look for "maps/level1.map", and C:\mygame is in your search
* path and C:\mygame\maps\level1.map exists, then "C:\mygame" is returned.
*
* If a match is a symbolic link, and you've not explicitly permitted symlinks,
* then it will be ignored, and the search for a match will continue.
*
* @param filename file to look for.
* @return READ ONLY string of element of search path containing the
* the file in question. NULL if not found.
*/
const char *PHYSFS_getRealDir(const char *filename)
{
} /* PHYSFS_getRealDir */
Jul 6, 2001
Jul 6, 2001
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
static void countList(LinkedStringList *list)
{
int retval = 0;
LinkedStringList *i;
assert(list != NULL);
for (i = list; i != NULL; i = i->next)
retval++;
return(retval);
} /* countList */
static char **convertStringListToPhysFSList(LinkedStringList *finalList)
{
int i;
LinkedStringList *next = NULL;
int len = countList(finalList);
char **retval = (char **) malloc((len + 1) * sizeof (char *));
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
for (i = 0; i < len; i++)
{
next = finalList->next;
if (retval == NULL)
free(finalList->str);
else
retval[i] = finalList->str;
free(finalList);
finalList = next;
} /* for */
if (retval != NULL);
retval[i] = NULL;
return(retval);
} /* convertStringListToPhysFSList */
static void insertStringListItem(LinkedStringList **final,
LinkedStringList *item)
{
LinkedStringList *i;
LinkedStringList *prev = NULL;
int rc;
for (i = *final; i != NULL; i = i->next)
{
rc = strcmp(i->str, item->str);
if (rc == 0) /* already in list. */
{
free(item->str);
free(item);
return;
} /* if */
else if (rc > 0) /* insertion point. */
{
if (prev == NULL)
*final = item;
else
prev->next = item;
item->next = i;
return;
} /* else if */
prev = i;
} /* for */
} /* insertStringListItem */
/* if we run out of memory anywhere in here, we give back what we can. */
static void interpolateStringLists(LinkedStringList **final,
LinkedStringList *newList)
{
LinkedStringList *next = NULL;
while (newList != NULL)
{
next = newList->next;
insertStringListItem(final, newList);
newList = next;
} /* while */
} /* interpolateStringLists */
797
798
799
char **PHYSFS_enumerateFiles(const char *path)
{
Jul 6, 2001
Jul 6, 2001
800
801
802
803
804
805
806
807
808
809
810
811
812
813
SearchDirInfo *i;
char **retval = NULL;
LinkedStringList *rc;
LinkedStringList *finalList = NULL;
for (i = searchPath; i != NULL; i = i->next)
{
assert(i->reader->funcs->enumerateFiles != NULL);
rc = i->reader->funcs->enumerateFiles(path);
interpolateStringLists(&finalList, rc);
} /* for */
retval = convertStringListToPhysFSList(finalList);
return(retval);
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
} /* PHYSFS_enumerateFiles */
/**
* Open a file for writing, in platform-independent notation and in relation
* to the write path as the root of the writable filesystem. The specified
* file is created if it doesn't exist. If it does exist, it is truncated to
* zero bytes, and the writing offset is set to the start.
*
* @param filename File to open.
* @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
PHYSFS_file *PHYSFS_openWrite(const char *filename)
{
} /* PHYSFS_openWrite */
/**
* Open a file for writing, in platform-independent notation and in relation
* to the write path as the root of the writable filesystem. The specified
* file is created if it doesn't exist. If it does exist, the writing offset
* is set to the end of the file, so the first write will be the byte after
* the end.
*
* @param filename File to open.
* @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
PHYSFS_file *PHYSFS_openAppend(const char *filename)
{
} /* PHYSFS_openAppend */
/**
* Open a file for reading, in platform-independent notation. The search path
* is checked one at a time until a matching file is found, in which case an
* abstract filehandle is associated with it, and reading may be done.
* The reading offset is set to the first byte of the file.
*
* @param filename File to open.
* @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
PHYSFS_file *PHYSFS_openRead(const char *filename)
{
} /* PHYSFS_openRead */
/**
* Close a PhysicsFS filehandle. This call is capable of failing if the
* operating system was buffering writes to this file, and (now forced to
* write those changes to physical media) can not store the data for any
* reason. In such a case, the filehandle stays open. A well-written program
* should ALWAYS check the return value from the close call in addition to
* every writing call!
*
* @param handle handle returned from PHYSFS_open*().
* @return nonzero on success, zero on error. Specifics of the error can be
* gleaned from PHYSFS_getLastError().
*/
int PHYSFS_close(PHYSFS_file *handle)
{
Jul 6, 2001
Jul 6, 2001
877
FileHandle *h = (FileHandle *) handle->opaque;
Jul 6, 2001
Jul 6, 2001
878
879
880
881
FileHandleList *i;
FileHandleList **lists[] = { &openWriteList, &openReadList, NULL };
int rc;
Jul 6, 2001
Jul 6, 2001
882
assert(h != NULL);
Jul 6, 2001
Jul 6, 2001
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
assert(h->funcs != NULL);
assert(h->funcs->close != NULL);
while (lists != NULL)
{
for (i = *(*lists); i != NULL; i = i->next)
{
if (i->handle == h)
{
rc = h->close(h);
if (!rc)
return(0);
if (prev == NULL)
*lists = i->next;
else
prev->next = i->next;
free(i);
free(handle);
return(1);
} /* if */
} /* for */
lists++;
} /* while */
assert(0); /* shouldn't EVER hit this. */
909
910
911
912
913
914
} /* PHYSFS_close */
int PHYSFS_read(PHYSFS_file *handle, void *buffer,
unsigned int objSize, unsigned int objCount)
{
Jul 6, 2001
Jul 6, 2001
915
916
FileHandle *h = (FileHandle *) handle->opaque;
assert(h != NULL);
Jul 6, 2001
Jul 6, 2001
917
918
919
assert(h->funcs != NULL);
BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
return(h->funcs->read(h, buffer, objSize, objCount));
920
921
922
923
924
925
} /* PHYSFS_read */
int PHYSFS_write(PHYSFS_file *handle, void *buffer,
unsigned int objSize, unsigned int objCount)
{
Jul 6, 2001
Jul 6, 2001
926
927
FileHandle *h = (FileHandle *) handle->opaque;
assert(h != NULL);
Jul 6, 2001
Jul 6, 2001
928
929
930
assert(h->funcs != NULL);
BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
return(h->funcs->write(h, buffer, objSize, objCount));
931
932
933
934
935
} /* PHYSFS_write */
int PHYSFS_eof(PHYSFS_file *handle)
{
Jul 6, 2001
Jul 6, 2001
936
937
FileHandle *h = (FileHandle *) handle->opaque;
assert(h != NULL);
Jul 6, 2001
Jul 6, 2001
938
939
940
assert(h->funcs != NULL);
BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
return(h->funcs->eof(h));
941
942
943
944
945
} /* PHYSFS_eof */
int PHYSFS_tell(PHYSFS_file *handle)
{
Jul 6, 2001
Jul 6, 2001
946
947
FileHandle *h = (FileHandle *) handle->opaque;
assert(h != NULL);
Jul 6, 2001
Jul 6, 2001
948
949
950
assert(h->funcs != NULL);
BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
return(h->funcs->tell(h));
951
952
953
954
955
} /* PHYSFS_tell */
int PHYSFS_seek(PHYSFS_file *handle, int pos)
{
Jul 6, 2001
Jul 6, 2001
956
957
FileHandle *h = (FileHandle *) handle->opaque;
assert(h != NULL);
Jul 6, 2001
Jul 6, 2001
958
959
960
assert(h->funcs != NULL);
BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
return(h->funcs->seek(h, pos));
961
962
} /* PHYSFS_seek */
Jul 6, 2001
Jul 6, 2001
963
/* end of physfs.c ... */