physfs.h 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /**
  2. * PhysicsFS; a portable, flexible file i/o abstraction.
  3. *
  4. * This API gives you access to a system file system in ways superior to the
  5. * stdio or system i/o calls. The brief benefits:
  6. *
  7. * - It's portable.
  8. * - It's safe. No file access is permitted outside the specified dirs.
  9. * - It's flexible. Archives (.ZIP files) can be used transparently as
  10. * directory structures.
  11. *
  12. * This system is largely inspired by Quake 3's PK3 files and the related
  13. * fs_* cvars. If you've ever tinkered with these, then this API will be
  14. * familiar to you.
  15. *
  16. * With PhysicsFS, you have a single writing directory and multiple
  17. * directories (the "search path") for reading. You can think of this as a
  18. * filesystem within a filesystem. If (on Windows) you were to set the
  19. * writing directory to "C:\MyGame\MyWritingDirectory", then no PHYSFS calls
  20. * could touch anything above this directory, including the "C:\MyGame" and
  21. * "C:\" directories. This prevents an application's internal scripting
  22. * language from piddling over c:\config.sys, for example. If you'd rather
  23. * give PHYSFS full access to the system's REAL file system, set the writing
  24. * dir to "C:\", but that's generally A Bad Thing for several reasons.
  25. *
  26. * Drive letters are hidden in PhysicsFS once you set up your initial paths.
  27. * The search path creates a single, hierarchical directory structure.
  28. * Not only does this lend itself well to general abstraction with archives,
  29. * it also gives better support to operating systems like MacOS and Unix.
  30. * Generally speaking, you shouldn't ever hardcode a drive letter; not only
  31. * does this hurt portability to non-Microsoft OSes, but it limits your win32
  32. * users to a single drive, too. Use the PhysicsFS abstraction functions and
  33. * allow user-defined configuration options, too. When opening a file, you
  34. * specify it like it was on a Unix filesystem: if you want to write to
  35. * "C:\MyGame\MyConfigFiles\game.cfg", then you might set the write dir to
  36. * "C:\MyGame" and then open "MyConfigFiles/game.cfg". This gives an
  37. * abstraction across all platforms. Specifying a file in this way is termed
  38. * "platform-independent notation" in this documentation. Specifying a
  39. * a filename in a form such as "C:\mydir\myfile" or
  40. * "MacOS hard drive:My Directory:My File" is termed "platform-dependent
  41. * notation". The only time you use platform-dependent notation is when
  42. * setting up your write directory and search path; after that, all file
  43. * access into those directories are done with platform-independent notation.
  44. *
  45. * All files opened for writing are opened in relation to the write directory,
  46. * which is the root of the writable filesystem. When opening a file for
  47. * reading, PhysicsFS goes through the search path. This is NOT the
  48. * same thing as the PATH environment variable. An application using
  49. * PhysicsFS specifies directories to be searched which may be actual
  50. * directories, or archive files that contain files and subdirectories of
  51. * their own. See the end of these docs for currently supported archive
  52. * formats.
  53. *
  54. * Once the search path is defined, you may open files for reading. If you've
  55. * got the following search path defined (to use a win32 example again):
  56. *
  57. * C:\mygame
  58. * C:\mygame\myuserfiles
  59. * D:\mygamescdromdatafiles
  60. * C:\mygame\installeddatafiles.zip
  61. *
  62. * Then a call to PHYSFS_openRead("textfiles/myfile.txt") (note the directory
  63. * separator, lack of drive letter, and lack of dir separator at the start of
  64. * the string; this is platform-independent notation) will check for
  65. * C:\mygame\textfiles\myfile.txt, then
  66. * C:\mygame\myuserfiles\textfiles\myfile.txt, then
  67. * D:\mygamescdromdatafiles\textfiles\myfile.txt, then, finally, for
  68. * textfiles\myfile.txt inside of C:\mygame\installeddatafiles.zip. Remember
  69. * that most archive types and platform filesystems store their filenames in
  70. * a case-sensitive manner, so you should be careful to specify it correctly.
  71. *
  72. * Files opened through PhysicsFS may NOT contain "." or ".." or ":" as dir
  73. * elements. Not only are these meaningless on MacOS and/or Unix, they are a
  74. * security hole. Also, symbolic links (which can be found in some archive
  75. * types and directly in the filesystem on Unix platforms) are NOT followed
  76. * until you call PHYSFS_permitSymbolicLinks(). That's left to your own
  77. * discretion, as following a symlink can allow for access outside the write
  78. * dir and search paths. There is no mechanism for creating new symlinks in
  79. * PhysicsFS.
  80. *
  81. * The write dir is not included in the search path unless you specifically
  82. * add it. While you CAN change the write dir as many times as you like,
  83. * you should probably set it once and stick to it. Remember that your
  84. * program will not have permission to write in every directory on Unix and
  85. * NT systems.
  86. *
  87. * All files are opened in binary mode; there is no endline conversion for
  88. * textfiles. Other than that, PhysicsFS has some convenience functions for
  89. * platform-independence. There is a function to tell you the current
  90. * platform's dir separator ("\\" on windows, "/" on Unix, ":" on MacOS),
  91. * which is needed only to set up your search/write paths. There is a
  92. * function to tell you what CD-ROM drives contain accessible discs, and a
  93. * function to recommend a good search path, etc.
  94. *
  95. * A recommended order for the search path is the write dir, then the base dir,
  96. * then the cdrom dir, then any archives discovered. Quake 3 does something
  97. * like this, but moves the archives to the start of the search path. Build
  98. * Engine games, like Duke Nukem 3D and Blood, place the archives last, and
  99. * use the base dir for both searching and writing. There is a helper
  100. * function (PHYSFS_setSaneConfig()) that puts together a basic configuration
  101. * for you, based on a few parameters. Also see the comments on
  102. * PHYSFS_getBaseDir(), and PHYSFS_getUserDir() for info on what those
  103. * are and how they can help you determine an optimal search path.
  104. *
  105. * PhysicsFS is mostly thread safe. The error messages returned by
  106. * PHYSFS_getLastError are unique by thread, and library-state-setting
  107. * functions are mutex'd. For efficiency, individual file accesses are
  108. * not locked, so you can not safely read/write/seek/close/etc the same
  109. * file from two threads at the same time. Other race conditions are bugs
  110. * that should be reported/patched.
  111. *
  112. * While you CAN use stdio/syscall file access in a program that has PHYSFS_*
  113. * calls, doing so is not recommended, and you can not use system
  114. * filehandles with PhysicsFS and vice versa.
  115. *
  116. * Note that archives need not be named as such: if you have a ZIP file and
  117. * rename it with a .PKG extension, the file will still be recognized as a
  118. * ZIP archive by PhysicsFS; the file's contents are used to determine its
  119. * type.
  120. *
  121. * Currently supported archive types:
  122. * - .ZIP (pkZip/WinZip/Info-ZIP compatible)
  123. *
  124. * Please see the file LICENSE in the source's root directory.
  125. *
  126. * This file written by Ryan C. Gordon.
  127. */
  128. #ifndef _INCLUDE_PHYSFS_H_
  129. #define _INCLUDE_PHYSFS_H_
  130. #ifdef _WIN32
  131. #include <windows.h>
  132. #endif
  133. #ifdef __cplusplus
  134. extern "C" {
  135. #endif
  136. #if (defined _MSC_VER)
  137. #define __EXPORT__ __declspec(dllexport)
  138. #else
  139. #define __EXPORT__
  140. #endif
  141. /* !!! FIXME: This is not universal. */
  142. typedef unsigned char PHYSFS_uint8;
  143. typedef signed char PHYSFS_sint8;
  144. typedef unsigned short PHYSFS_uint16;
  145. typedef signed short PHYSFS_sint16;
  146. typedef unsigned int PHYSFS_uint32;
  147. typedef signed int PHYSFS_sint32;
  148. #ifdef PHYSFS_NO_64BIT_SUPPORT /* oh well. */
  149. typedef PHYSFS_uint32 PHYSFS_uint64;
  150. typedef PHYSFS_sint32 PHYSFS_sint64;
  151. #elif _WIN32
  152. /*!!! No 64-bit unsigned in Win32???? */
  153. typedef LONGLONG PHYSFS_sint64;
  154. typedef LONGLONG PHYSFS_uint64;
  155. #else
  156. typedef unsigned long long PHYSFS_uint64;
  157. typedef signed long long PHYSFS_sint64;
  158. #endif
  159. /* Make sure the types really have the right sizes */
  160. #define PHYSFS_COMPILE_TIME_ASSERT(name, x) \
  161. typedef int PHYSFS_dummy_ ## name[(x) * 2 - 1]
  162. PHYSFS_COMPILE_TIME_ASSERT(uint8, sizeof(PHYSFS_uint8) == 1);
  163. PHYSFS_COMPILE_TIME_ASSERT(sint8, sizeof(PHYSFS_sint8) == 1);
  164. PHYSFS_COMPILE_TIME_ASSERT(uint16, sizeof(PHYSFS_uint16) == 2);
  165. PHYSFS_COMPILE_TIME_ASSERT(sint16, sizeof(PHYSFS_sint16) == 2);
  166. PHYSFS_COMPILE_TIME_ASSERT(uint32, sizeof(PHYSFS_uint32) == 4);
  167. PHYSFS_COMPILE_TIME_ASSERT(sint32, sizeof(PHYSFS_sint32) == 4);
  168. #ifndef PHYSFS_NO_64BIT_SUPPORT
  169. PHYSFS_COMPILE_TIME_ASSERT(uint64, sizeof(PHYSFS_uint64) == 8);
  170. PHYSFS_COMPILE_TIME_ASSERT(sint64, sizeof(PHYSFS_sint64) == 8);
  171. #endif
  172. #undef PHYSFS_COMPILE_TIME_ASSERT
  173. typedef struct __PHYSFS_FILE__
  174. {
  175. void *opaque;
  176. } PHYSFS_file;
  177. typedef struct __PHYSFS_ARCHIVEINFO__
  178. {
  179. const char *extension;
  180. const char *description;
  181. const char *author;
  182. const char *url;
  183. } PHYSFS_ArchiveInfo;
  184. /* functions... */
  185. typedef struct __PHYSFS_VERSION__
  186. {
  187. PHYSFS_uint8 major;
  188. PHYSFS_uint8 minor;
  189. PHYSFS_uint8 patch;
  190. } PHYSFS_Version;
  191. #define PHYSFS_VER_MAJOR 0
  192. #define PHYSFS_VER_MINOR 1
  193. #define PHYSFS_VER_PATCH 4
  194. #define PHYSFS_VERSION(x) { \
  195. (x)->major = PHYSFS_VER_MAJOR; \
  196. (x)->minor = PHYSFS_VER_MINOR; \
  197. (x)->patch = PHYSFS_VER_PATCH; \
  198. }
  199. /**
  200. * Get the version of PhysicsFS that is linked against your program. If you
  201. * are using a shared library (DLL) version of PhysFS, then it is possible
  202. * that it will be different than the version you compiled against.
  203. *
  204. * This is a real function; the macro PHYSFS_VERSION tells you what version
  205. * of PhysFS you compiled against:
  206. *
  207. * PHYSFS_Version compiled;
  208. * PHYSFS_Version linked;
  209. *
  210. * PHYSFS_VERSION(&compiled);
  211. * PHYSFS_getLinkedVersion(&linked);
  212. * printf("We compiled against PhysFS version %d.%d.%d ...\n",
  213. * compiled.major, compiled.minor, compiled.patch);
  214. * printf("But we linked against PhysFS version %d.%d.%d.\n",
  215. * linked.major, linked.minor, linked.patch);
  216. *
  217. * This function may be called safely at any time, even before PHYSFS_init().
  218. */
  219. __EXPORT__ void PHYSFS_getLinkedVersion(PHYSFS_Version *ver);
  220. /**
  221. * Initialize PhysicsFS. This must be called before any other PhysicsFS
  222. * function.
  223. *
  224. * This should be called prior to any attempts to change your process's
  225. * current working directory.
  226. *
  227. * @param argv0 the argv[0] string passed to your program's mainline.
  228. * @return nonzero on success, zero on error. Specifics of the error can be
  229. * gleaned from PHYSFS_getLastError().
  230. */
  231. __EXPORT__ int PHYSFS_init(const char *argv0);
  232. /**
  233. * Shutdown PhysicsFS. This closes any files opened via PhysicsFS, blanks the
  234. * search/write paths, frees memory, and invalidates all of your handles.
  235. *
  236. * Note that this call can FAIL if there's a file open for writing that
  237. * refuses to close (for example, the underlying operating system was
  238. * buffering writes to network filesystem, and the fileserver has crashed,
  239. * or a hard drive has failed, etc). It is usually best to close all write
  240. * handles yourself before calling this function, so that you can gracefully
  241. * handle a specific failure.
  242. *
  243. * Once successfully deinitialized, PHYSFS_init() can be called again to
  244. * restart the subsystem. All defaults API states are restored at this
  245. * point.
  246. *
  247. * @return nonzero on success, zero on error. Specifics of the error can be
  248. * gleaned from PHYSFS_getLastError(). If failure, state of PhysFS is
  249. * undefined, and probably badly screwed up.
  250. */
  251. __EXPORT__ int PHYSFS_deinit(void);
  252. /**
  253. * Get a list of archive types supported by this implementation of PhysicFS.
  254. * These are the file formats usable for search path entries. This is for
  255. * informational purposes only. Note that the extension listed is merely
  256. * convention: if we list "ZIP", you can open a PkZip-compatible archive
  257. * with an extension of "XYZ", if you like.
  258. *
  259. * The returned value is an array of pointers to PHYSFS_ArchiveInfo structures,
  260. * with a NULL entry to signify the end of the list:
  261. *
  262. * PHYSFS_ArchiveInfo **i;
  263. *
  264. * for (i = PHYSFS_supportedArchiveTypes(); *i != NULL; i++)
  265. * {
  266. * printf("Supported archive: [%s], which is [%s].\n",
  267. * i->extension, i->description);
  268. * }
  269. *
  270. * The return values are pointers to static internal memory, and should
  271. * be considered READ ONLY, and never freed.
  272. *
  273. * @return READ ONLY Null-terminated array of READ ONLY structures.
  274. */
  275. __EXPORT__ const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void);
  276. /**
  277. * Certain PhysicsFS functions return lists of information that are
  278. * dynamically allocated. Use this function to free those resources.
  279. *
  280. * @param list List of information specified as freeable by this function.
  281. */
  282. __EXPORT__ void PHYSFS_freeList(void *listVar);
  283. /**
  284. * Get the last PhysicsFS error message as a null-terminated string.
  285. * This will be NULL if there's been no error since the last call to this
  286. * function. The pointer returned by this call points to an internal buffer.
  287. * Each thread has a unique error state associated with it, but each time
  288. * a new error message is set, it will overwrite the previous one associated
  289. * with that thread. It is safe to call this function at anytime, even
  290. * before PHYSFS_init().
  291. *
  292. * @return READ ONLY string of last error message.
  293. */
  294. __EXPORT__ const char *PHYSFS_getLastError(void);
  295. /**
  296. * Get a platform-dependent dir separator. This is "\\" on win32, "/" on Unix,
  297. * and ":" on MacOS. It may be more than one character, depending on the
  298. * platform, and your code should take that into account. Note that this is
  299. * only useful for setting up the search/write paths, since access into those
  300. * dirs always use '/' (platform-independent notation) to separate
  301. * directories. This is also handy for getting platform-independent access
  302. * when using stdio calls.
  303. *
  304. * @return READ ONLY null-terminated string of platform's dir separator.
  305. */
  306. __EXPORT__ const char *PHYSFS_getDirSeparator(void);
  307. /**
  308. * Enable symbolic links. Some physical filesystems and archives contain
  309. * files that are just pointers to other files. On the physical filesystem,
  310. * opening such a link will (transparently) open the file that is pointed to.
  311. *
  312. * By default, PhysicsFS will check if a file is really a symlink during open
  313. * calls and fail if it is. Otherwise, the link could take you outside the
  314. * write and search paths, and compromise security.
  315. *
  316. * If you want to take that risk, call this function with a non-zero parameter.
  317. * Note that this is more for sandboxing a program's scripting language, in
  318. * case untrusted scripts try to compromise the system. Generally speaking,
  319. * a user could very well have a legitimate reason to set up a symlink, so
  320. * unless you feel there's a specific danger in allowing them, you should
  321. * permit them.
  322. *
  323. * Symlinks are only explicitly checked when dealing with filenames
  324. * in platform-independent notation. That is, when setting up your
  325. * search and write paths, etc, symlinks are never checked for.
  326. *
  327. * Symbolic link permission can be enabled or disabled at any time, and is
  328. * disabled by default.
  329. *
  330. * @param allow nonzero to permit symlinks, zero to deny linking.
  331. */
  332. __EXPORT__ void PHYSFS_permitSymbolicLinks(int allow);
  333. /**
  334. * Get an array of dirs to available CD-ROM drives.
  335. *
  336. * The dirs returned are platform-dependent ("D:\" on Win32, "/cdrom" or
  337. * whatnot on Unix). Dirs are only returned if there is a disc ready and
  338. * accessible in the drive. So if you've got two drives (D: and E:), and only
  339. * E: has a disc in it, then that's all you get. If the user inserts a disc
  340. * in D: and you call this function again, you get both drives. If, on a
  341. * Unix box, the user unmounts a disc and remounts it elsewhere, the next
  342. * call to this function will reflect that change. Fun.
  343. *
  344. * The returned value is an array of strings, with a NULL entry to signify the
  345. * end of the list:
  346. *
  347. * char **cds = PHYSFS_getCdRomDirs();
  348. * char **i;
  349. *
  350. * for (i = cds; *i != NULL; i++)
  351. * printf("cdrom dir [%s] is available.\n", *i);
  352. *
  353. * PHYSFS_freeList(cds);
  354. *
  355. * This call may block while drives spin up. Be forewarned.
  356. *
  357. * When you are done with the returned information, you may dispose of the
  358. * resources by calling PHYSFS_freeList() with the returned pointer.
  359. *
  360. * @return Null-terminated array of null-terminated strings.
  361. */
  362. __EXPORT__ char **PHYSFS_getCdRomDirs(void);
  363. /**
  364. * Helper function.
  365. *
  366. * Get the "base dir". This is the directory where the application was run
  367. * from, which is probably the installation directory, and may or may not
  368. * be the process's current working directory.
  369. *
  370. * You should probably use the base dir in your search path.
  371. *
  372. * @return READ ONLY string of base dir in platform-dependent notation.
  373. */
  374. __EXPORT__ const char *PHYSFS_getBaseDir(void);
  375. /**
  376. * Helper function.
  377. *
  378. * Get the "user dir". This is meant to be a suggestion of where a specific
  379. * user of the system can store files. On Unix, this is her home directory.
  380. * On systems with no concept of multiple home directories (MacOS, win95),
  381. * this will default to something like "C:\mybasedir\users\username"
  382. * where "username" will either be the login name, or "default" if the
  383. * platform doesn't support multiple users, either.
  384. *
  385. * You should probably use the user dir as the basis for your write dir, and
  386. * also put it near the beginning of your search path.
  387. *
  388. * @return READ ONLY string of user dir in platform-dependent notation.
  389. */
  390. __EXPORT__ const char *PHYSFS_getUserDir(void);
  391. /**
  392. * Get the current write dir. The default write dir is NULL.
  393. *
  394. * @return READ ONLY string of write dir in platform-dependent notation,
  395. * OR NULL IF NO WRITE PATH IS CURRENTLY SET.
  396. */
  397. __EXPORT__ const char *PHYSFS_getWriteDir(void);
  398. /**
  399. * Set a new write dir. This will override the previous setting. If the
  400. * directory or a parent directory doesn't exist in the physical filesystem,
  401. * PhysicsFS will attempt to create them as needed.
  402. *
  403. * This call will fail (and fail to change the write dir) if the current
  404. * write dir still has files open in it.
  405. *
  406. * @param newDir The new directory to be the root of the write dir,
  407. * specified in platform-dependent notation. Setting to NULL
  408. * disables the write dir, so no files can be opened for
  409. * writing via PhysicsFS.
  410. * @return non-zero on success, zero on failure. All attempts to open a file
  411. * for writing via PhysicsFS will fail until this call succeeds.
  412. * Specifics of the error can be gleaned from PHYSFS_getLastError().
  413. *
  414. */
  415. __EXPORT__ int PHYSFS_setWriteDir(const char *newDir);
  416. /**
  417. * Add a directory or archive to the search path. If this is a duplicate, the
  418. * entry is not added again, even though the function succeeds.
  419. *
  420. * @param newDir directory or archive to add to the path, in
  421. * platform-dependent notation.
  422. * @param appendToPath nonzero to append to search path, zero to prepend.
  423. * @return nonzero if added to path, zero on failure (bogus archive, dir
  424. * missing, etc). Specifics of the error can be
  425. * gleaned from PHYSFS_getLastError().
  426. */
  427. __EXPORT__ int PHYSFS_addToSearchPath(const char *newDir, int appendToPath);
  428. /**
  429. * Remove a directory or archive from the search path.
  430. *
  431. * This must be a (case-sensitive) match to a dir or archive already in the
  432. * search path, specified in platform-dependent notation.
  433. *
  434. * This call will fail (and fail to remove from the path) if the element still
  435. * has files open in it.
  436. *
  437. * @param oldDir dir/archive to remove.
  438. * @return nonzero on success, zero on failure.
  439. * Specifics of the error can be gleaned from PHYSFS_getLastError().
  440. */
  441. __EXPORT__ int PHYSFS_removeFromSearchPath(const char *oldDir);
  442. /**
  443. * Get the current search path. The default search path is an empty list.
  444. *
  445. * The returned value is an array of strings, with a NULL entry to signify the
  446. * end of the list:
  447. *
  448. * char **i;
  449. *
  450. * for (i = PHYSFS_getSearchPath(); *i != NULL; i++)
  451. * printf("[%s] is in the search path.\n", *i);
  452. *
  453. * When you are done with the returned information, you may dispose of the
  454. * resources by calling PHYSFS_freeList() with the returned pointer.
  455. *
  456. * @return Null-terminated array of null-terminated strings. NULL if there
  457. * was a problem (read: OUT OF MEMORY).
  458. */
  459. __EXPORT__ char **PHYSFS_getSearchPath(void);
  460. /**
  461. * Helper function.
  462. *
  463. * Set up sane, default paths. The write dir will be set to
  464. * "userdir/.organization/appName", which is created if it doesn't exist.
  465. *
  466. * The above is sufficient to make sure your program's configuration directory
  467. * is separated from other clutter, and platform-independent. The period
  468. * before "mygame" even hides the directory on Unix systems.
  469. *
  470. * The search path will be:
  471. *
  472. * - The Write Dir (created if it doesn't exist)
  473. * - The Base Dir (PHYSFS_getBaseDir())
  474. * - All found CD-ROM dirs (optionally)
  475. *
  476. * These directories are then searched for files ending with the extension
  477. * (archiveExt), which, if they are valid and supported archives, will also
  478. * be added to the search path. If you specified "PKG" for (archiveExt), and
  479. * there's a file named data.PKG in the base dir, it'll be checked. Archives
  480. * can either be appended or prepended to the search path in alphabetical
  481. * order, regardless of which directories they were found in.
  482. *
  483. * All of this can be accomplished from the application, but this just does it
  484. * all for you. Feel free to add more to the search path manually, too.
  485. *
  486. * @param organization Name of your company/group/etc to be used as a
  487. * dirname, so keep it small, and no-frills.
  488. *
  489. * @param appName Program-specific name of your program, to separate it
  490. * from other programs using PhysicsFS.
  491. *
  492. * @param archiveExt File extention used by your program to specify an
  493. * archive. For example, Quake 3 uses "pk3", even though
  494. * they are just zipfiles. Specify NULL to not dig out
  495. * archives automatically. Do not specify the '.' char;
  496. * If you want to look for ZIP files, specify "ZIP" and
  497. * not ".ZIP" ... the archive search is case-insensitive.
  498. *
  499. * @param includeCdRoms Non-zero to include CD-ROMs in the search path, and
  500. * (if (archiveExt) != NULL) search them for archives.
  501. * This may cause a significant amount of blocking
  502. * while discs are accessed, and if there are no discs
  503. * in the drive (or even not mounted on Unix systems),
  504. * then they may not be made available anyhow. You may
  505. * want to specify zero and handle the disc setup
  506. * yourself.
  507. *
  508. * @param archivesFirst Non-zero to prepend the archives to the search path.
  509. * Zero to append them. Ignored if !(archiveExt).
  510. *
  511. * @return nonzero on success, zero on error. Specifics of the error can be
  512. * gleaned from PHYSFS_getLastError().
  513. */
  514. __EXPORT__ int PHYSFS_setSaneConfig(const char *organization,
  515. const char *appName,
  516. const char *archiveExt,
  517. int includeCdRoms,
  518. int archivesFirst);
  519. /**
  520. * Create a directory. This is specified in platform-independent notation in
  521. * relation to the write dir. All missing parent directories are also
  522. * created if they don't exist.
  523. *
  524. * So if you've got the write dir set to "C:\mygame\writedir" and call
  525. * PHYSFS_mkdir("downloads/maps") then the directories
  526. * "C:\mygame\writedir\downloads" and "C:\mygame\writedir\downloads\maps"
  527. * will be created if possible. If the creation of "maps" fails after we
  528. * have successfully created "downloads", then the function leaves the
  529. * created directory behind and reports failure.
  530. *
  531. * @param dirname New dir to create.
  532. * @return nonzero on success, zero on error. Specifics of the error can be
  533. * gleaned from PHYSFS_getLastError().
  534. */
  535. __EXPORT__ int PHYSFS_mkdir(const char *dirName);
  536. /**
  537. * Delete a file or directory. This is specified in platform-independent
  538. * notation in relation to the write dir.
  539. *
  540. * A directory must be empty before this call can delete it.
  541. *
  542. * Deleting a symlink will remove the link, not what it points to, regardless
  543. * of whether you "permitSymLinks" or not.
  544. *
  545. * So if you've got the write dir set to "C:\mygame\writedir" and call
  546. * PHYSFS_delete("downloads/maps/level1.map") then the file
  547. * "C:\mygame\writedir\downloads\maps\level1.map" is removed from the
  548. * physical filesystem, if it exists and the operating system permits the
  549. * deletion.
  550. *
  551. * Note that on Unix systems, deleting a file may be successful, but the
  552. * actual file won't be removed until all processes that have an open
  553. * filehandle to it (including your program) close their handles.
  554. *
  555. * Chances are, the bits that make up the file still exist, they are just
  556. * made available to be written over at a later point. Don't consider this
  557. * a security method or anything. :)
  558. *
  559. * @param filename Filename to delete.
  560. * @return nonzero on success, zero on error. Specifics of the error can be
  561. * gleaned from PHYSFS_getLastError().
  562. */
  563. __EXPORT__ int PHYSFS_delete(const char *filename);
  564. /**
  565. * Figure out where in the search path a file resides. The file is specified
  566. * in platform-independent notation. The returned filename will be the
  567. * element of the search path where the file was found, which may be a
  568. * directory, or an archive. Even if there are multiple matches in different
  569. * parts of the search path, only the first one found is used, just like
  570. * when opening a file.
  571. *
  572. * So, if you look for "maps/level1.map", and C:\mygame is in your search
  573. * path and C:\mygame\maps\level1.map exists, then "C:\mygame" is returned.
  574. *
  575. * If a any part of a match is a symbolic link, and you've not explicitly
  576. * permitted symlinks, then it will be ignored, and the search for a match
  577. * will continue.
  578. *
  579. * @param filename file to look for.
  580. * @return READ ONLY string of element of search path containing the
  581. * the file in question. NULL if not found.
  582. */
  583. __EXPORT__ const char *PHYSFS_getRealDir(const char *filename);
  584. /**
  585. * Get a file listing of a search path's directory. Matching directories are
  586. * interpolated. That is, if "C:\mydir" is in the search path and contains a
  587. * directory "savegames" that contains "x.sav", "y.sav", and "z.sav", and
  588. * there is also a "C:\userdir" in the search path that has a "savegames"
  589. * subdirectory with "w.sav", then the following code:
  590. *
  591. * ------------------------------------------------
  592. * char **rc = PHYSFS_enumerateFiles("savegames");
  593. * char **i;
  594. *
  595. * for (i = rc; *i != NULL; i++)
  596. * printf("We've got [%s].\n", *i);
  597. *
  598. * PHYSFS_freeList(rc);
  599. * ------------------------------------------------
  600. *
  601. * ...will print:
  602. *
  603. * ------------------------------------------------
  604. * We've got [x.sav].
  605. * We've got [y.sav].
  606. * We've got [z.sav].
  607. * We've got [w.sav].
  608. * ------------------------------------------------
  609. *
  610. * Feel free to sort the list however you like. We only promise there will
  611. * be no duplicates, but not what order the final list will come back in.
  612. *
  613. * Don't forget to call PHYSFS_freeList() with the return value from this
  614. * function when you are done with it.
  615. *
  616. * @param dir directory in platform-independent notation to enumerate.
  617. * @return Null-terminated array of null-terminated strings.
  618. */
  619. __EXPORT__ char **PHYSFS_enumerateFiles(const char *dir);
  620. /**
  621. * Determine if there is an entry anywhere in the search path by the
  622. * name of (fname).
  623. *
  624. * Note that entries that are symlinks are ignored if
  625. * PHYSFS_permitSymbolicLinks(1) hasn't been called, so you
  626. * might end up further down in the search path than expected.
  627. *
  628. * @param fname filename in platform-independent notation.
  629. * @return non-zero if filename exists. zero otherwise.
  630. */
  631. __EXPORT__ int PHYSFS_exists(const char *fname);
  632. /**
  633. * Determine if the first occurence of (fname) in the search path is
  634. * really a directory entry.
  635. *
  636. * Note that entries that are symlinks are ignored if
  637. * PHYSFS_permitSymbolicLinks(1) hasn't been called, so you
  638. * might end up further down in the search path than expected.
  639. *
  640. * @param fname filename in platform-independent notation.
  641. * @return non-zero if filename exists and is a directory. zero otherwise.
  642. */
  643. __EXPORT__ int PHYSFS_isDirectory(const char *fname);
  644. /**
  645. * Determine if the first occurence of (fname) in the search path is
  646. * really a symbolic link.
  647. *
  648. * Note that entries that are symlinks are ignored if
  649. * PHYSFS_permitSymbolicLinks(1) hasn't been called, and as such,
  650. * this function will always return 0 in that case.
  651. *
  652. * @param fname filename in platform-independent notation.
  653. * @return non-zero if filename exists and is a symlink. zero otherwise.
  654. */
  655. __EXPORT__ int PHYSFS_isSymbolicLink(const char *fname);
  656. /**
  657. * Open a file for writing, in platform-independent notation and in relation
  658. * to the write dir as the root of the writable filesystem. The specified
  659. * file is created if it doesn't exist. If it does exist, it is truncated to
  660. * zero bytes, and the writing offset is set to the start.
  661. *
  662. * Note that entries that are symlinks are ignored if
  663. * PHYSFS_permitSymbolicLinks(1) hasn't been called, and opening a
  664. * symlink with this function will fail in such a case.
  665. *
  666. * @param filename File to open.
  667. * @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
  668. * of the error can be gleaned from PHYSFS_getLastError().
  669. */
  670. __EXPORT__ PHYSFS_file *PHYSFS_openWrite(const char *filename);
  671. /**
  672. * Open a file for writing, in platform-independent notation and in relation
  673. * to the write dir as the root of the writable filesystem. The specified
  674. * file is created if it doesn't exist. If it does exist, the writing offset
  675. * is set to the end of the file, so the first write will be the byte after
  676. * the end.
  677. *
  678. * Note that entries that are symlinks are ignored if
  679. * PHYSFS_permitSymbolicLinks(1) hasn't been called, and opening a
  680. * symlink with this function will fail in such a case.
  681. *
  682. * @param filename File to open.
  683. * @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
  684. * of the error can be gleaned from PHYSFS_getLastError().
  685. */
  686. __EXPORT__ PHYSFS_file *PHYSFS_openAppend(const char *filename);
  687. /**
  688. * Open a file for reading, in platform-independent notation. The search path
  689. * is checked one at a time until a matching file is found, in which case an
  690. * abstract filehandle is associated with it, and reading may be done.
  691. * The reading offset is set to the first byte of the file.
  692. *
  693. * Note that entries that are symlinks are ignored if
  694. * PHYSFS_permitSymbolicLinks(1) hasn't been called, and opening a
  695. * symlink with this function will fail in such a case.
  696. *
  697. * @param filename File to open.
  698. * @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
  699. * of the error can be gleaned from PHYSFS_getLastError().
  700. */
  701. __EXPORT__ PHYSFS_file *PHYSFS_openRead(const char *filename);
  702. /**
  703. * Close a PhysicsFS filehandle. This call is capable of failing if the
  704. * operating system was buffering writes to this file, and (now forced to
  705. * write those changes to physical media) can not store the data for any
  706. * reason. In such a case, the filehandle stays open. A well-written program
  707. * should ALWAYS check the return value from the close call in addition to
  708. * every writing call!
  709. *
  710. * @param handle handle returned from PHYSFS_open*().
  711. * @return nonzero on success, zero on error. Specifics of the error can be
  712. * gleaned from PHYSFS_getLastError().
  713. */
  714. __EXPORT__ int PHYSFS_close(PHYSFS_file *handle);
  715. /**
  716. * Read data from a PhysicsFS filehandle. The file must be opened for reading.
  717. *
  718. * @param handle handle returned from PHYSFS_openRead().
  719. * @param buffer buffer to store read data into.
  720. * @param objSize size in bytes of objects being read from (handle).
  721. * @param objCount number of (objSize) objects to read from (handle).
  722. * @return number of objects read. PHYSFS_getLastError() can shed light on
  723. * the reason this might be < (objCount), as can PHYSFS_eof().
  724. * -1 if complete failure.
  725. */
  726. __EXPORT__ PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle,
  727. void *buffer,
  728. PHYSFS_uint32 objSize,
  729. PHYSFS_uint32 objCount);
  730. /**
  731. * Write data to a PhysicsFS filehandle. The file must be opened for writing.
  732. *
  733. * @param handle retval from PHYSFS_openWrite() or PHYSFS_openAppend().
  734. * @param buffer buffer to store read data into.
  735. * @param objSize size in bytes of objects being read from (handle).
  736. * @param objCount number of (objSize) objects to read from (handle).
  737. * @return number of objects written. PHYSFS_getLastError() can shed light on
  738. * the reason this might be < (objCount). -1 if complete failure.
  739. */
  740. __EXPORT__ PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle,
  741. const void *buffer,
  742. PHYSFS_uint32 objSize,
  743. PHYSFS_uint32 objCount);
  744. /**
  745. * Determine if the end of file has been reached in a PhysicsFS filehandle.
  746. *
  747. * @param handle handle returned from PHYSFS_openRead().
  748. * @return nonzero if EOF, zero if not.
  749. */
  750. __EXPORT__ int PHYSFS_eof(PHYSFS_file *handle);
  751. /**
  752. * Determine current position within a PhysicsFS filehandle.
  753. *
  754. * @param handle handle returned from PHYSFS_open*().
  755. * @return offset in bytes from start of file. -1 if error occurred.
  756. * Specifics of the error can be gleaned from PHYSFS_getLastError().
  757. */
  758. __EXPORT__ PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle);
  759. /**
  760. * Seek to a new position within a PhysicsFS filehandle. The next read or write
  761. * will occur at that place. Seeking past the beginning or end of the file is
  762. * not allowed.
  763. *
  764. * @param handle handle returned from PHYSFS_open*().
  765. * @param pos number of bytes from start of file to seek to.
  766. * @return nonzero on success, zero on error. Specifics of the error can be
  767. * gleaned from PHYSFS_getLastError().
  768. */
  769. __EXPORT__ int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos);
  770. /**
  771. * Get total length of a file in bytes. Note that if the file size can't
  772. * be determined (since the archive is "streamed" or whatnot) than this
  773. * will report (-1). Also note that if another process/thread is writing
  774. * to this file at the same time, then the information this function
  775. * supplies could be incorrect before you get it. Use with caution, or
  776. * better yet, don't use at all.
  777. *
  778. * @param handle handle returned from PHYSFS_open*().
  779. * @return size in bytes of the file. -1 if can't be determined.
  780. */
  781. __EXPORT__ PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle);
  782. /* Byteorder stuff... */
  783. /**
  784. * Take a 16-bit signed value in littleendian format and convert it to
  785. * the platform's native byte order.
  786. *
  787. * @param val value to convert
  788. * @return converted value.
  789. */
  790. __EXPORT__ PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 val);
  791. /**
  792. * Take a 16-bit unsigned value in littleendian format and convert it to
  793. * the platform's native byte order.
  794. *
  795. * @param val value to convert
  796. * @return converted value.
  797. */
  798. __EXPORT__ PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 val);
  799. /**
  800. * Take a 32-bit signed value in littleendian format and convert it to
  801. * the platform's native byte order.
  802. *
  803. * @param val value to convert
  804. * @return converted value.
  805. */
  806. __EXPORT__ PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 val);
  807. /**
  808. * Take a 32-bit unsigned value in littleendian format and convert it to
  809. * the platform's native byte order.
  810. *
  811. * @param val value to convert
  812. * @return converted value.
  813. */
  814. __EXPORT__ PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 val);
  815. /**
  816. * Take a 64-bit signed value in littleendian format and convert it to
  817. * the platform's native byte order.
  818. *
  819. * @param val value to convert
  820. * @return converted value.
  821. */
  822. __EXPORT__ PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 val);
  823. /**
  824. * Take a 64-bit unsigned value in littleendian format and convert it to
  825. * the platform's native byte order.
  826. *
  827. * @param val value to convert
  828. * @return converted value.
  829. */
  830. __EXPORT__ PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 val);
  831. /**
  832. * Take a 16-bit signed value in bigendian format and convert it to
  833. * the platform's native byte order.
  834. *
  835. * @param val value to convert
  836. * @return converted value.
  837. */
  838. __EXPORT__ PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 val);
  839. /**
  840. * Take a 16-bit unsigned value in bigendian format and convert it to
  841. * the platform's native byte order.
  842. *
  843. * @param val value to convert
  844. * @return converted value.
  845. */
  846. __EXPORT__ PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 val);
  847. /**
  848. * Take a 32-bit signed value in bigendian format and convert it to
  849. * the platform's native byte order.
  850. *
  851. * @param val value to convert
  852. * @return converted value.
  853. */
  854. __EXPORT__ PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 val);
  855. /**
  856. * Take a 32-bit unsigned value in bigendian format and convert it to
  857. * the platform's native byte order.
  858. *
  859. * @param val value to convert
  860. * @return converted value.
  861. */
  862. __EXPORT__ PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 val);
  863. /**
  864. * Take a 64-bit signed value in bigendian format and convert it to
  865. * the platform's native byte order.
  866. *
  867. * @param val value to convert
  868. * @return converted value.
  869. */
  870. __EXPORT__ PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 val);
  871. /**
  872. * Take a 64-bit unsigned value in bigendian format and convert it to
  873. * the platform's native byte order.
  874. *
  875. * @param val value to convert
  876. * @return converted value.
  877. */
  878. __EXPORT__ PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 val);
  879. #if 0 /* !!! FIXME: add this? */
  880. #undef __EXPORT__
  881. #endif
  882. #ifdef __cplusplus
  883. }
  884. #endif
  885. #endif /* !defined _INCLUDE_PHYSFS_H_ */
  886. /* end of physfs.h ... */