physfsrwops.c 6.5 KB

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