physfsrwops.c 5.9 KB

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