physfsrwops.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /* This works with SDL1 and SDL2. For SDL3, use physfssdl3.c */
  24. #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
  25. #include "physfsrwops.h"
  26. /* SDL's RWOPS interface changed a little in SDL 2.0... */
  27. #if defined(SDL_VERSION_ATLEAST)
  28. #if SDL_VERSION_ATLEAST(2, 0, 0)
  29. #define TARGET_SDL2 1
  30. #endif
  31. #endif
  32. #if !TARGET_SDL2
  33. #ifndef RW_SEEK_SET
  34. #define RW_SEEK_SET SEEK_SET
  35. #endif
  36. #ifndef RW_SEEK_CUR
  37. #define RW_SEEK_CUR SEEK_CUR
  38. #endif
  39. #ifndef RW_SEEK_END
  40. #define RW_SEEK_END SEEK_END
  41. #endif
  42. #endif
  43. #if TARGET_SDL2
  44. static Sint64 SDLCALL physfsrwops_size(struct SDL_RWops *rw)
  45. {
  46. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  47. return (Sint64) PHYSFS_fileLength(handle);
  48. } /* physfsrwops_size */
  49. #endif
  50. #if TARGET_SDL2
  51. static Sint64 SDLCALL physfsrwops_seek(struct SDL_RWops *rw, Sint64 offset, int whence)
  52. #else
  53. static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
  54. #endif
  55. {
  56. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  57. PHYSFS_sint64 pos = 0;
  58. if (whence == RW_SEEK_SET)
  59. pos = (PHYSFS_sint64) offset;
  60. else if (whence == RW_SEEK_CUR)
  61. {
  62. const PHYSFS_sint64 current = PHYSFS_tell(handle);
  63. if (current == -1)
  64. {
  65. SDL_SetError("Can't find position in file: %s",
  66. PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  67. return -1;
  68. } /* if */
  69. if (offset == 0) /* this is a "tell" call. We're done. */
  70. {
  71. #if TARGET_SDL2
  72. return (Sint64) current;
  73. #else
  74. return (int) current;
  75. #endif
  76. } /* if */
  77. pos = current + ((PHYSFS_sint64) offset);
  78. } /* else if */
  79. else if (whence == RW_SEEK_END)
  80. {
  81. const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
  82. if (len == -1)
  83. {
  84. SDL_SetError("Can't find end of file: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  85. return -1;
  86. } /* if */
  87. pos = len + ((PHYSFS_sint64) offset);
  88. } /* else if */
  89. else
  90. {
  91. SDL_SetError("Invalid 'whence' parameter.");
  92. return -1;
  93. } /* else */
  94. if ( pos < 0 )
  95. {
  96. SDL_SetError("Attempt to seek past start of file.");
  97. return -1;
  98. } /* if */
  99. if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
  100. {
  101. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  102. return -1;
  103. } /* if */
  104. #if TARGET_SDL2
  105. return (Sint64) pos;
  106. #else
  107. return (int) pos;
  108. #endif
  109. } /* physfsrwops_seek */
  110. #if TARGET_SDL2
  111. static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr,
  112. size_t size, size_t maxnum)
  113. #else
  114. static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
  115. #endif
  116. {
  117. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  118. const PHYSFS_uint64 readlen = (PHYSFS_uint64) (maxnum * size);
  119. const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen);
  120. if (rc != ((PHYSFS_sint64) readlen))
  121. {
  122. if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
  123. {
  124. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  125. #if TARGET_SDL2
  126. return 0;
  127. #else
  128. return -1;
  129. #endif
  130. } /* if */
  131. } /* if */
  132. #if TARGET_SDL2
  133. return (size_t) rc / size;
  134. #else
  135. return (int) rc / size;
  136. #endif
  137. } /* physfsrwops_read */
  138. #if TARGET_SDL2
  139. static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr,
  140. size_t size, size_t num)
  141. #else
  142. static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
  143. #endif
  144. {
  145. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  146. const PHYSFS_uint64 writelen = (PHYSFS_uint64) (num * size);
  147. const PHYSFS_sint64 rc = PHYSFS_writeBytes(handle, ptr, writelen);
  148. if (rc != ((PHYSFS_sint64) writelen))
  149. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  150. #if TARGET_SDL2
  151. return (size_t) rc;
  152. #else
  153. return (int) rc;
  154. #endif
  155. } /* physfsrwops_write */
  156. static int physfsrwops_close(SDL_RWops *rw)
  157. {
  158. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  159. if (!PHYSFS_close(handle))
  160. {
  161. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  162. return -1;
  163. } /* if */
  164. SDL_FreeRW(rw);
  165. return 0;
  166. } /* physfsrwops_close */
  167. static SDL_RWops *create_rwops(PHYSFS_File *handle)
  168. {
  169. SDL_RWops *retval = NULL;
  170. if (handle == NULL)
  171. SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  172. else
  173. {
  174. retval = SDL_AllocRW();
  175. if (retval != NULL)
  176. {
  177. #if TARGET_SDL2
  178. retval->size = physfsrwops_size;
  179. #endif
  180. retval->seek = physfsrwops_seek;
  181. retval->read = physfsrwops_read;
  182. retval->write = physfsrwops_write;
  183. retval->close = physfsrwops_close;
  184. retval->hidden.unknown.data1 = handle;
  185. } /* if */
  186. } /* else */
  187. return retval;
  188. } /* create_rwops */
  189. SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
  190. {
  191. SDL_RWops *retval = NULL;
  192. if (handle == NULL)
  193. SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
  194. else
  195. retval = create_rwops(handle);
  196. return retval;
  197. } /* PHYSFSRWOPS_makeRWops */
  198. SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
  199. {
  200. return create_rwops(PHYSFS_openRead(fname));
  201. } /* PHYSFSRWOPS_openRead */
  202. SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
  203. {
  204. return create_rwops(PHYSFS_openWrite(fname));
  205. } /* PHYSFSRWOPS_openWrite */
  206. SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
  207. {
  208. return create_rwops(PHYSFS_openAppend(fname));
  209. } /* PHYSFSRWOPS_openAppend */
  210. /* end of physfsrwops.c ... */