🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
6.8 KiB

  1. /*
  2. * This code provides a glue layer between PhysicsFS and Simple Directmedia
  3. * Layer's (SDL) RWops i/o abstraction.
  4. *
  5. * License: this code is public domain. I make no warranty that it is useful,
  6. * correct, harmless, or environmentally safe.
  7. *
  8. * This particular file may be used however you like, including copying it
  9. * verbatim into a closed-source project, exploiting it commercially, and
  10. * removing any trace of my name from the source (although I hope you won't
  11. * do that). I welcome enhancements and corrections to this file, but I do
  12. * not require you to send me patches if you make changes. This code has
  13. * NO WARRANTY.
  14. *
  15. * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
  16. * Please see LICENSE.txt in the root of the source tree.
  17. *
  18. * SDL 1.2 falls under the LGPL license. SDL 1.3+ is zlib, like PhysicsFS.
  19. * You can get SDL at https://www.libsdl.org/
  20. *
  21. * This file was written by Ryan C. Gordon. (icculus@icculus.org).
  22. */
  23. #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
  24. #include "physfsrwops.h"
  25. /* SDL's RWOPS interface changed a little in SDL 2.0... */
  26. #if defined(SDL_VERSION_ATLEAST)
  27. #if SDL_VERSION_ATLEAST(2, 0, 0)
  28. #define TARGET_SDL2 1
  29. #endif
  30. #endif
  31. #if !TARGET_SDL2
  32. #ifndef RW_SEEK_SET
  33. #define RW_SEEK_SET SEEK_SET
  34. #endif
  35. #ifndef RW_SEEK_CUR
  36. #define RW_SEEK_CUR SEEK_CUR
  37. #endif
  38. #ifndef RW_SEEK_END
  39. #define RW_SEEK_END SEEK_END
  40. #endif
  41. #endif
  42. #if TARGET_SDL2
  43. static Sint64 SDLCALL physfsrwops_size(struct SDL_RWops *rw)
  44. {
  45. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  46. return (Sint64) PHYSFS_fileLength(handle);
  47. } /* physfsrwops_size */
  48. #endif
  49. #if TARGET_SDL2
  50. static Sint64 SDLCALL physfsrwops_seek(struct SDL_RWops *rw, Sint64 offset, int whence)
  51. #else
  52. static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
  53. #endif
  54. {
  55. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  56. PHYSFS_sint64 pos = 0;
  57. if (whence == RW_SEEK_SET)
  58. pos = (PHYSFS_sint64) offset;
  59. else if (whence == RW_SEEK_CUR)
  60. {
  61. const PHYSFS_sint64 current = PHYSFS_tell(handle);
  62. if (current == -1)
  63. {
  64. SDL_SetError("Can't find position in file: %s",
  65. PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  66. return -1;
  67. } /* if */
  68. if (offset == 0) /* this is a "tell" call. We're done. */
  69. {
  70. #if TARGET_SDL2
  71. return (Sint64) current;
  72. #else
  73. return (int) current;
  74. #endif
  75. } /* if */
  76. pos = current + ((PHYSFS_sint64) offset);
  77. } /* else if */
  78. else if (whence == RW_SEEK_END)
  79. {
  80. const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
  81. if (len == -1)
  82. {
  83. SDL_SetError("Can't find end of file: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  84. return -1;
  85. } /* if */
  86. pos = len + ((PHYSFS_sint64) offset);
  87. } /* else if */
  88. else
  89. {
  90. SDL_SetError("Invalid 'whence' parameter.");
  91. return -1;
  92. } /* else */
  93. if ( pos < 0 )
  94. {
  95. SDL_SetError("Attempt to seek past start of file.");
  96. return -1;
  97. } /* if */
  98. if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
  99. {
  100. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  101. return -1;
  102. } /* if */
  103. #if TARGET_SDL2
  104. return (Sint64) pos;
  105. #else
  106. return (int) pos;
  107. #endif
  108. } /* physfsrwops_seek */
  109. #if TARGET_SDL2
  110. static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr,
  111. size_t size, size_t maxnum)
  112. #else
  113. static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
  114. #endif
  115. {
  116. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  117. const PHYSFS_uint64 readlen = (PHYSFS_uint64) (maxnum * size);
  118. const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen);
  119. if (rc != ((PHYSFS_sint64) readlen))
  120. {
  121. if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
  122. {
  123. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  124. #if TARGET_SDL2
  125. return 0;
  126. #else
  127. return -1;
  128. #endif
  129. } /* if */
  130. } /* if */
  131. #if TARGET_SDL2
  132. return (size_t) rc / size;
  133. #else
  134. return (int) rc / size;
  135. #endif
  136. } /* physfsrwops_read */
  137. #if TARGET_SDL2
  138. static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr,
  139. size_t size, size_t num)
  140. #else
  141. static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
  142. #endif
  143. {
  144. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  145. const PHYSFS_uint64 writelen = (PHYSFS_uint64) (num * size);
  146. const PHYSFS_sint64 rc = PHYSFS_writeBytes(handle, ptr, writelen);
  147. if (rc != ((PHYSFS_sint64) writelen))
  148. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  149. #if TARGET_SDL2
  150. return (size_t) rc;
  151. #else
  152. return (int) rc;
  153. #endif
  154. } /* physfsrwops_write */
  155. static int physfsrwops_close(SDL_RWops *rw)
  156. {
  157. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  158. if (!PHYSFS_close(handle))
  159. {
  160. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  161. return -1;
  162. } /* if */
  163. SDL_FreeRW(rw);
  164. return 0;
  165. } /* physfsrwops_close */
  166. static SDL_RWops *create_rwops(PHYSFS_File *handle)
  167. {
  168. SDL_RWops *retval = NULL;
  169. if (handle == NULL)
  170. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  171. else
  172. {
  173. retval = SDL_AllocRW();
  174. if (retval != NULL)
  175. {
  176. #if TARGET_SDL2
  177. retval->size = physfsrwops_size;
  178. #endif
  179. retval->seek = physfsrwops_seek;
  180. retval->read = physfsrwops_read;
  181. retval->write = physfsrwops_write;
  182. retval->close = physfsrwops_close;
  183. retval->hidden.unknown.data1 = handle;
  184. } /* if */
  185. } /* else */
  186. return retval;
  187. } /* create_rwops */
  188. SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
  189. {
  190. SDL_RWops *retval = NULL;
  191. if (handle == NULL)
  192. SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
  193. else
  194. retval = create_rwops(handle);
  195. return retval;
  196. } /* PHYSFSRWOPS_makeRWops */
  197. SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
  198. {
  199. return create_rwops(PHYSFS_openRead(fname));
  200. } /* PHYSFSRWOPS_openRead */
  201. SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
  202. {
  203. return create_rwops(PHYSFS_openWrite(fname));
  204. } /* PHYSFSRWOPS_openWrite */
  205. SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
  206. {
  207. return create_rwops(PHYSFS_openAppend(fname));
  208. } /* PHYSFSRWOPS_openAppend */
  209. /* end of physfsrwops.c ... */