physfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /**
  2. * PhysicsFS; a portable, flexible file i/o abstraction.
  3. *
  4. * Documentation is in physfs.h. It's verbose, honest. :)
  5. *
  6. * Please see the file LICENSE in the source's root directory.
  7. *
  8. * This file written by Ryan C. Gordon.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <assert.h>
  14. #include "physfs.h"
  15. #define __PHYSICSFS_INTERNAL__
  16. #include "physfs_internal.h"
  17. typedef struct __PHYSFS_ERRMSGTYPE__
  18. {
  19. int tid;
  20. int errorAvailable;
  21. char errorString[80];
  22. struct __PHYSFS_ERRMSGTYPE__ *next;
  23. } ErrMsg;
  24. typedef struct __PHYSFS_SEARCHDIRINFO__
  25. {
  26. char *dirName;
  27. DirReader *reader;
  28. struct __PHYSFS_SEARCHDIRINFO__ *next;
  29. } SearchDirInfo;
  30. static int initialized = 0;
  31. static ErrMsg *errorMessages = NULL;
  32. static char *searchPath = NULL;
  33. static char *baseDir = NULL;
  34. static char *writeDir = NULL;
  35. static int allowSymLinks = 0;
  36. #if (defined PHYSFS_SUPPORTS_ZIP)
  37. extern const __PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
  38. extern const __PHYSFS_DirReader __PHYSFS_DirReader_ZIP;
  39. extern const __PHYSFS_FileHandle __PHYSFS_FileHandle_ZIP;
  40. #endif
  41. static const __PHYSFS_ArchiveInfo *supported_types[] =
  42. {
  43. #if (defined PHYSFS_SUPPORTS_ZIP)
  44. &__PHYSFS_ArchiveInfo_ZIP,
  45. #endif
  46. NULL
  47. };
  48. static ErrMsg *findErrorForCurrentThread(void)
  49. {
  50. ErrMsg *i;
  51. int tid;
  52. if (errorMessages != NULL)
  53. {
  54. tid = __PHYSFS_platformGetThreadID();
  55. for (i = errorMessages; i != NULL; i = i->next)
  56. {
  57. if (i->tid == tid)
  58. return(i);
  59. } /* for */
  60. } /* if */
  61. return(NULL); /* no error available. */
  62. } /* findErrorForCurrentThread */
  63. void __PHYSFS_setError(const char *str)
  64. {
  65. ErrMsg *err = findErrorForCurrentThread();
  66. if (err == NULL)
  67. {
  68. err = (ErrMsg *) malloc(sizeof (ErrMsg));
  69. if (err == NULL)
  70. return; /* uhh...? */
  71. err->tid = __PHYSFS_platformGetThreadID();
  72. err->next = errorMessages;
  73. errorMessages = err;
  74. } /* if */
  75. err->errorAvailable = 1;
  76. strncpy(err->errorString, str, sizeof (err->errorString));
  77. err->errorString[sizeof (err->errorString) - 1] = '\0';
  78. } /* __PHYSFS_setError */
  79. const char *PHYSFS_getLastError(void)
  80. {
  81. ErrMsg *err = findErrorForCurrentThread();
  82. if ((err == NULL) || (!err->errorAvailable))
  83. return(NULL);
  84. err->errorAvailable = 0;
  85. return(err->errorString);
  86. } /* PHYSFS_getLastError */
  87. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  88. {
  89. if (ver != NULL)
  90. {
  91. ver->major = PHYSFS_VER_MAJOR;
  92. ver->minor = PHYSFS_VER_MINOR;
  93. ver->patch = PHYSFS_VER_PATCH;
  94. } /* if */
  95. } /* PHYSFS_getLinkedVersion */
  96. int PHYSFS_init(const char *argv0)
  97. {
  98. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  99. BAIL_IF_MACRO(argv0 == NULL, ERR_INVALID_ARGUMENT, 0);
  100. baseDir = calculateBaseDir();
  101. initialized = 1;
  102. return(1);
  103. } /* PHYSFS_init */
  104. static void freeSearchDir(SearchDirInfo *sdi)
  105. {
  106. assert(sdi != NULL);
  107. /* !!! make sure all files in search dir are closed. */
  108. sdi->reader->close(sdi->reader);
  109. free(sdi->dirName);
  110. free(sdi);
  111. } /* freeSearchDir */
  112. static void freeSearchPath(void)
  113. {
  114. SearchDirInfo *i;
  115. SearchDirInfo *next = NULL;
  116. if (searchPath != NULL)
  117. {
  118. for (i = searchPath; i != NULL; i = next)
  119. {
  120. next = i;
  121. freeSearchDir(i);
  122. } /* for */
  123. searchPath = NULL;
  124. } /* if */
  125. } /* freeSearchPath */
  126. static void closeAllFiles(void)
  127. {
  128. } /* closeAllFiles */
  129. void PHYSFS_deinit(void)
  130. {
  131. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  132. closeAllFiles();
  133. PHYSFS_setWriteDir(NULL);
  134. freeSearchPath();
  135. freeErrorMessages();
  136. if (baseDir != NULL)
  137. free(baseDir);
  138. allowSymLinks = 0;
  139. initialized = 0;
  140. return(1);
  141. } /* PHYSFS_deinit */
  142. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  143. {
  144. return(supported_types);
  145. } /* PHYSFS_supportedArchiveTypes */
  146. void PHYSFS_freeList(void *list)
  147. {
  148. void **i;
  149. for (i = (void **) list; *i != NULL; i++)
  150. free(*i);
  151. free(list);
  152. } /* PHYSFS_freeList */
  153. const char *PHYSFS_getDirSeparator(void)
  154. {
  155. return(__PHYSFS_pathSeparator);
  156. } /* PHYSFS_getDirSeparator */
  157. char **PHYSFS_getCdRomDirs(void)
  158. {
  159. return(__PHYSFS_platformDetectAvailableCDs());
  160. } /* PHYSFS_getCdRomDirs */
  161. const char *PHYSFS_getBaseDir(void)
  162. {
  163. return(baseDir); /* this is calculated in PHYSFS_init()... */
  164. } /* PHYSFS_getBaseDir */
  165. const char *PHYSFS_getUserDir(void)
  166. {
  167. static char *retval = NULL;
  168. const char *str = NULL;
  169. if (retval != NULL)
  170. return(retval);
  171. str = __PHYSFS_platformGetUserDir();
  172. if (str != NULL)
  173. retval = str;
  174. else
  175. {
  176. const char *dirsep = PHYSFS_getDirSeparator();
  177. const char *uname;
  178. str = getenv("HOME"); /* try a default. */
  179. if (str != NULL)
  180. retval = str;
  181. else
  182. {
  183. uname = __PHYSFS_platformGetUserName();
  184. str = (uname != NULL) ? uname : "default";
  185. retval = malloc(strlen(baseDir) + strlen(str) +
  186. (strlen(dirsep) * 2) + 6);
  187. if (retval == NULL)
  188. retval = baseDir; /* (*shrug*) */
  189. else
  190. sprintf(retval, "%s%susers%s%s", baseDir, dirsep, dirsep, uname);
  191. if (uname != NULL)
  192. free(uname);
  193. } /* else */
  194. } /* if */
  195. return(baseDir); /* just in case. */
  196. } /* PHYSFS_getUserDir */
  197. const char *PHYSFS_getWriteDir(void)
  198. {
  199. return(writeDir);
  200. } /* PHYSFS_getWriteDir */
  201. int PHYSFS_setWriteDir(const char *newDir)
  202. {
  203. BAIL_IF_MACRO(openWriteCount > 0, ERR_FILES_OPEN_WRITE, 0);
  204. if (writeDir != NULL)
  205. {
  206. free(writeDir);
  207. writeDir = NULL;
  208. } /* if */
  209. if (newDir != NULL)
  210. {
  211. BAIL_IF_MACRO(!createDirs_dependent(newDir), ERR_NO_DIR_CREATE, 0);
  212. writeDir = malloc(strlen(newDir) + 1);
  213. BAIL_IF_MACRO(writeDir == NULL, ERR_OUT_OF_MEMORY, 0);
  214. strcpy(writeDir, newDir);
  215. } /* if */
  216. return(1);
  217. } /* PHYSFS_setWriteDir */
  218. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  219. {
  220. char *str = NULL;
  221. SearchDirInfo *sdi = NULL;
  222. DirReader *dirReader = NULL;
  223. BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
  224. reader = getDirReader(newDir); /* This sets the error message. */
  225. if (reader == NULL)
  226. return(0);
  227. sdi = (SearchDirInfo *) malloc(sizeof (SearchDirInfo));
  228. BAIL_IF_MACRO(sdi == NULL, ERR_OUT_OF_MEMORY, 0);
  229. sdi->dirName = (char *) malloc(strlen(newDir) + 1);
  230. if (sdi->dirName == NULL)
  231. {
  232. free(sdi);
  233. freeReader(reader);
  234. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  235. return(0);
  236. } /* if */
  237. sdi->dirReader = dirReader;
  238. strcpy(sdi->dirName, newDir);
  239. if (appendToPath)
  240. {
  241. sdi->next = searchPath;
  242. searchPath = sdi;
  243. } /* if */
  244. else
  245. {
  246. SearchDirInfo *i = searchPath;
  247. SearchDirInfo *prev = NULL;
  248. sdi->next = NULL;
  249. while (i != NULL)
  250. prev = i;
  251. if (prev == NULL)
  252. searchPath = sdi;
  253. else
  254. prev->next = sdi;
  255. } /* else */
  256. return(1);
  257. } /* PHYSFS_addToSearchPath */
  258. int PHYSFS_removeFromSearchPath(const char *oldDir)
  259. {
  260. SearchDirInfo *i;
  261. SearchDirInfo *prev = NULL;
  262. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  263. for (i = searchPath; i != NULL; i = i->next)
  264. {
  265. if (strcmp(i->dirName, oldDir) == 0)
  266. {
  267. if (prev == NULL)
  268. searchPath = i->next;
  269. else
  270. prev->next = i->next;
  271. /* !!! make sure all files in search dir are closed. */
  272. freeSearchDir(i);
  273. return(1);
  274. } /* if */
  275. prev = i;
  276. } /* for */
  277. __PHYSFS_setError(ERR_NOT_IN_SEARCH_PATH);
  278. return(0);
  279. } /* PHYSFS_removeFromSearchPath */
  280. char **PHYSFS_getSearchPath(void)
  281. {
  282. int count = 1;
  283. int x;
  284. SearchDirInfo *i;
  285. char **retval;
  286. for (i = searchPath; i != NULL; i = i->next)
  287. count++;
  288. retval = (char **) malloc(sizeof (char *) * count);
  289. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  290. count--;
  291. retval[count] = NULL;
  292. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  293. {
  294. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  295. if (retval[x] == NULL) /* this is friggin' ugly. */
  296. {
  297. while (x > 0)
  298. {
  299. x--;
  300. free(retval[x]);
  301. } /* while */
  302. free(retval);
  303. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  304. return(NULL);
  305. } /* if */
  306. strcpy(retval[x], i->dirName);
  307. } /* for */
  308. return(retval);
  309. } /* PHYSFS_getSearchPath */
  310. /**
  311. * Helper function.
  312. *
  313. * Set up sane, default paths. The write path will be set to
  314. * "userpath/.appName", which is created if it doesn't exist.
  315. *
  316. * The above is sufficient to make sure your program's configuration directory
  317. * is separated from other clutter, and platform-independent. The period
  318. * before "mygame" even hides the directory on Unix systems.
  319. *
  320. * The search path will be:
  321. *
  322. * - The Write Dir (created if it doesn't exist)
  323. * - The Write Dir/appName (created if it doesn't exist)
  324. * - The Base Dir (PHYSFS_getBaseDir())
  325. * - The Base Dir/appName (if it exists)
  326. * - All found CD-ROM paths (optionally)
  327. * - All found CD-ROM paths/appName (optionally, and if they exist)
  328. *
  329. * These directories are then searched for files ending with the extension
  330. * (archiveExt), which, if they are valid and supported archives, will also
  331. * be added to the search path. If you specified "PKG" for (archiveExt), and
  332. * there's a file named data.PKG in the base dir, it'll be checked. Archives
  333. * can either be appended or prepended to the search path in alphabetical
  334. * order, regardless of which directories they were found in.
  335. *
  336. * All of this can be accomplished from the application, but this just does it
  337. * all for you. Feel free to add more to the search path manually, too.
  338. *
  339. * @param appName Program-specific name of your program, to separate it
  340. * from other programs using PhysicsFS.
  341. *
  342. * @param archiveExt File extention used by your program to specify an
  343. * archive. For example, Quake 3 uses "pk3", even though
  344. * they are just zipfiles. Specify NULL to not dig out
  345. * archives automatically.
  346. *
  347. * @param includeCdRoms Non-zero to include CD-ROMs in the search path, and
  348. * (if (archiveExt) != NULL) search them for archives.
  349. * This may cause a significant amount of blocking
  350. * while discs are accessed, and if there are no discs
  351. * in the drive (or even not mounted on Unix systems),
  352. * then they may not be made available anyhow. You may
  353. * want to specify zero and handle the disc setup
  354. * yourself.
  355. *
  356. * @param archivesFirst Non-zero to prepend the archives to the search path.
  357. * Zero to append them. Ignored if !(archiveExt).
  358. */
  359. int PHYSFS_setSaneConfig(const char *appName, const char *archiveExt,
  360. int includeCdRoms, int archivesFirst)
  361. {
  362. const char *basedir = PHYSFS_getBaseDir();
  363. const char *userdir = PHYSFS_getUserDir();
  364. const char *dirsep = PHYSFS_getDirSeparator();
  365. char *str;
  366. int rc;
  367. /* set write dir... */
  368. str = malloc(strlen(userdir) + (strlen(appName) * 2) +
  369. (strlen(dirsep) * 2) + 2);
  370. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  371. sprintf(str, "%s%s.%s", userdir, dirsep, appName);
  372. rc = PHYSFS_setWriteDir(str);
  373. if (!rc)
  374. return(0); /* error set by PHYSFS_setWriteDir() ... */
  375. /* Put write dir related dirs on search path... */
  376. PHYSFS_addToSearchPath(str, 1);
  377. PHYSFS_mkdir(appName); /* don't care if this fails. */
  378. strcat(str, dirSep);
  379. strcat(str, appName);
  380. PHYSFS_addToSearchPath(str, 1);
  381. free(str);
  382. /* Put base path stuff on search path... */
  383. PHYSFS_addToSearchPath(basedir, 1);
  384. str = malloc(strlen(basedir) + (strlen(appName) * 2) +
  385. (strlen(dirsep) * 2) + 2);
  386. if (str != NULL)
  387. {
  388. sprintf(str, "%s%s.%s", basedir, dirsep, appName);
  389. PHYSFS_addToSearchPath(str, 1);
  390. free(str);
  391. } /* if */
  392. /* handle CD-ROMs... */
  393. if (includeCdRoms)
  394. {
  395. char **cds = PHYSFS_getCdRomDirs();
  396. char **i;
  397. for (i = cds; *i != NULL; i++)
  398. {
  399. PHYSFS_addToSearchPath(*i);
  400. str = malloc(strlen(*i) + strlen(appName) + strlen(dirsep) + 1);
  401. if (str != NULL)
  402. {
  403. sprintf(str, "%s%s%s", *i, dirsep, appName);
  404. PHYSFS_addToSearchPath(str);
  405. free(str);
  406. } /* if */
  407. } /* for */
  408. PHYSFS_freeList(cds);
  409. } /* if */
  410. /* Root out archives, and add them to search path... */
  411. if (archiveExt != NULL)
  412. {
  413. char **rc = PHYSFS_enumerateFiles("");
  414. char **i;
  415. int extlen = strlen(archiveExt);
  416. char *ext;
  417. for (i = rc; *i != NULL; i++)
  418. {
  419. int l = strlen(*i);
  420. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'));
  421. {
  422. ext = (*i) + (l - extlen);
  423. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  424. {
  425. const char *d = PHYSFS_getRealDir(*i);
  426. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  427. if (str != NULL)
  428. {
  429. sprintf(str, "%s%s%s", d, dirsep, *i);
  430. PHYSFS_addToSearchPath(d, str);
  431. free(str);
  432. } /* if */
  433. } /* if */
  434. } /* if */
  435. } /* for */
  436. PHYSFS_freeList(rc);
  437. } /* if */
  438. return(1);
  439. } /* PHYSFS_setSaneConfig */
  440. /* string manipulation in C makes my ass itch. */
  441. /* be sure to free this crap after you're done with it. */
  442. static char *convertToDependentNotation(const char *prepend,
  443. const char *dirName,
  444. const char *append)
  445. {
  446. const char *dirsep = PHYSFS_getDirSeparator();
  447. int sepsize = strlen(dirsep);
  448. char *str;
  449. char *i1;
  450. char *i2;
  451. size_t allocSize;
  452. allocSize = strlen(dirName) + strlen(writeDir) + sepsize + 1;
  453. if (prepend != NULL)
  454. allocSize += strlen(prepend) + sepsize;
  455. if (append != NULL)
  456. allocSize += strlen(append) + sepsize;
  457. /* make sure there's enough space if the dir separator is bigger. */
  458. if (sepsize > 1)
  459. {
  460. for (str = dirName; *str != '\0'; str++)
  461. {
  462. if (*str == '/')
  463. allocSize += (sepsize - 1);
  464. } /* for */
  465. } /* if */
  466. str = (char *) malloc(allocSize);
  467. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  468. *str = '\0';
  469. if (prepend)
  470. {
  471. strcpy(str, prepend);
  472. strcat(str, dirsep);
  473. } /* if */
  474. for (i1 = dirName, i2 = str + strlen(str); *i1 != '\0'; i1++, i2++)
  475. {
  476. if (*i1 == '/')
  477. {
  478. strcpy(i2, dirsep);
  479. i2 += sepsize;
  480. } /* if */
  481. else
  482. {
  483. *i2 = *i1;
  484. } /* else */
  485. } /* for */
  486. *i2 = '\0';
  487. if (append)
  488. {
  489. strcat(str, dirsep);
  490. strcpy(str, append);
  491. } /* if */
  492. return(str);
  493. } /* convertToDependentNotation */
  494. int PHYSFS_mkdir(const char *dirName)
  495. {
  496. char *str;
  497. int rc;
  498. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, NULL);
  499. str = convertToDependentNotation(writeDir, dirName, NULL);
  500. if (str == NULL) /* __PHYSFS_setError is called in convert call. */
  501. return(0);
  502. rc = createDirs_dependent(str);
  503. free(str);
  504. return(rc);
  505. } /* PHYSFS_mkdir */
  506. int PHYSFS_delete(const char *filename)
  507. {
  508. char *str;
  509. int rc;
  510. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, NULL);
  511. str = convertToDependentNotation(writeDir, fileName, NULL);
  512. if (str == NULL) /* __PHYSFS_setError is called in convert call. */
  513. return(0);
  514. rc = remove(str);
  515. free(str);
  516. rc = (rc == 0);
  517. if (!rc)
  518. __PHYSFS_setError(strerror(errno));
  519. return(rc);
  520. } /* PHYSFS_delete */
  521. void PHYSFS_permitSymbolicLinks(int allow)
  522. {
  523. allowSymLinks = allow;
  524. } /* PHYSFS_permitSymbolicLinks */
  525. /**
  526. * Figure out where in the search path a file resides. The file is specified
  527. * in platform-independent notation. The returned filename will be the
  528. * element of the search path where the file was found, which may be a
  529. * directory, or an archive. Even if there are multiple matches in different
  530. * parts of the search path, only the first one found is used, just like
  531. * when opening a file.
  532. *
  533. * So, if you look for "maps/level1.map", and C:\mygame is in your search
  534. * path and C:\mygame\maps\level1.map exists, then "C:\mygame" is returned.
  535. *
  536. * If a match is a symbolic link, and you've not explicitly permitted symlinks,
  537. * then it will be ignored, and the search for a match will continue.
  538. *
  539. * @param filename file to look for.
  540. * @return READ ONLY string of element of search path containing the
  541. * the file in question. NULL if not found.
  542. */
  543. const char *PHYSFS_getRealDir(const char *filename)
  544. {
  545. } /* PHYSFS_getRealDir */
  546. /**
  547. * Get a file listing of a search path's directory. Matching directories are
  548. * interpolated. That is, if "C:\mypath" is in the search path and contains a
  549. * directory "savegames" that contains "x.sav", "y.sav", and "z.sav", and
  550. * there is also a "C:\userpath" in the search path that has a "savegames"
  551. * subdirectory with "w.sav", then the following code:
  552. *
  553. * ------------------------------------------------
  554. * char **rc = PHYSFS_enumerateFiles("savegames");
  555. * char **i;
  556. *
  557. * for (i = rc; *i != NULL; i++)
  558. * printf("We've got [%s].\n", *i);
  559. *
  560. * PHYSFS_freeList(rc);
  561. * ------------------------------------------------
  562. *
  563. * ...will print:
  564. *
  565. * ------------------------------------------------
  566. * We've got [x.sav].
  567. * We've got [y.sav].
  568. * We've got [z.sav].
  569. * We've got [w.sav].
  570. * ------------------------------------------------
  571. *
  572. * Don't forget to call PHYSFS_freeList() with the return value from this
  573. * function when you are done with it.
  574. *
  575. * @param path directory in platform-independent notation to enumerate.
  576. * @return Null-terminated array of null-terminated strings.
  577. */
  578. char **PHYSFS_enumerateFiles(const char *path)
  579. {
  580. } /* PHYSFS_enumerateFiles */
  581. /**
  582. * Open a file for writing, in platform-independent notation and in relation
  583. * to the write path as the root of the writable filesystem. The specified
  584. * file is created if it doesn't exist. If it does exist, it is truncated to
  585. * zero bytes, and the writing offset is set to the start.
  586. *
  587. * @param filename File to open.
  588. * @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
  589. * of the error can be gleaned from PHYSFS_getLastError().
  590. */
  591. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  592. {
  593. } /* PHYSFS_openWrite */
  594. /**
  595. * Open a file for writing, in platform-independent notation and in relation
  596. * to the write path as the root of the writable filesystem. The specified
  597. * file is created if it doesn't exist. If it does exist, the writing offset
  598. * is set to the end of the file, so the first write will be the byte after
  599. * the end.
  600. *
  601. * @param filename File to open.
  602. * @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
  603. * of the error can be gleaned from PHYSFS_getLastError().
  604. */
  605. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  606. {
  607. } /* PHYSFS_openAppend */
  608. /**
  609. * Open a file for reading, in platform-independent notation. The search path
  610. * is checked one at a time until a matching file is found, in which case an
  611. * abstract filehandle is associated with it, and reading may be done.
  612. * The reading offset is set to the first byte of the file.
  613. *
  614. * @param filename File to open.
  615. * @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
  616. * of the error can be gleaned from PHYSFS_getLastError().
  617. */
  618. PHYSFS_file *PHYSFS_openRead(const char *filename)
  619. {
  620. } /* PHYSFS_openRead */
  621. /**
  622. * Close a PhysicsFS filehandle. This call is capable of failing if the
  623. * operating system was buffering writes to this file, and (now forced to
  624. * write those changes to physical media) can not store the data for any
  625. * reason. In such a case, the filehandle stays open. A well-written program
  626. * should ALWAYS check the return value from the close call in addition to
  627. * every writing call!
  628. *
  629. * @param handle handle returned from PHYSFS_open*().
  630. * @return nonzero on success, zero on error. Specifics of the error can be
  631. * gleaned from PHYSFS_getLastError().
  632. */
  633. int PHYSFS_close(PHYSFS_file *handle)
  634. {
  635. FileHandle *h = (FileHandle *) handle->opaque;
  636. assert(h != NULL);
  637. BAIL_IF_MACRO(h->close == NULL, ERR_NOT_SUPPORTED, -1);
  638. rc = h->close(h);
  639. if (rc)
  640. free(handle);
  641. return(rc);
  642. } /* PHYSFS_close */
  643. int PHYSFS_read(PHYSFS_file *handle, void *buffer,
  644. unsigned int objSize, unsigned int objCount)
  645. {
  646. FileHandle *h = (FileHandle *) handle->opaque;
  647. assert(h != NULL);
  648. BAIL_IF_MACRO(h->read == NULL, ERR_NOT_SUPPORTED, -1);
  649. return(h->read(h, buffer, objSize, objCount));
  650. } /* PHYSFS_read */
  651. int PHYSFS_write(PHYSFS_file *handle, void *buffer,
  652. unsigned int objSize, unsigned int objCount)
  653. {
  654. FileHandle *h = (FileHandle *) handle->opaque;
  655. assert(h != NULL);
  656. BAIL_IF_MACRO(h->write == NULL, ERR_NOT_SUPPORTED, -1);
  657. return(h->write(h, buffer, objSize, objCount));
  658. } /* PHYSFS_write */
  659. int PHYSFS_eof(PHYSFS_file *handle)
  660. {
  661. FileHandle *h = (FileHandle *) handle->opaque;
  662. assert(h != NULL);
  663. BAIL_IF_MACRO(h->eof == NULL, ERR_NOT_SUPPORTED, -1);
  664. return(h->eof(h));
  665. } /* PHYSFS_eof */
  666. int PHYSFS_tell(PHYSFS_file *handle)
  667. {
  668. FileHandle *h = (FileHandle *) handle->opaque;
  669. assert(h != NULL);
  670. BAIL_IF_MACRO(h->tell == NULL, ERR_NOT_SUPPORTED, -1);
  671. return(h->tell(h));
  672. } /* PHYSFS_tell */
  673. int PHYSFS_seek(PHYSFS_file *handle, int pos)
  674. {
  675. FileHandle *h = (FileHandle *) handle->opaque;
  676. assert(h != NULL);
  677. BAIL_IF_MACRO(h->seek == NULL, ERR_NOT_SUPPORTED, 0);
  678. return(h->seek(h, pos));
  679. } /* PHYSFS_seek */
  680. /* end of physfs.c ... */