physfsrwops.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 falls under the LGPL license. You can get SDL at http://www.libsdl.org/
  19. *
  20. * This file was written by Ryan C. Gordon. (icculus@icculus.org).
  21. */
  22. #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
  23. #include "physfsrwops.h"
  24. static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
  25. {
  26. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  27. int pos = 0;
  28. if (whence == SEEK_SET)
  29. {
  30. pos = offset;
  31. } /* if */
  32. else if (whence == SEEK_CUR)
  33. {
  34. PHYSFS_sint64 current = PHYSFS_tell(handle);
  35. if (current == -1)
  36. {
  37. SDL_SetError("Can't find position in file: %s",
  38. PHYSFS_getLastError());
  39. return(-1);
  40. } /* if */
  41. pos = (int) current;
  42. if ( ((PHYSFS_sint64) pos) != current )
  43. {
  44. SDL_SetError("Can't fit current file position in an int!");
  45. return(-1);
  46. } /* if */
  47. if (offset == 0) /* this is a "tell" call. We're done. */
  48. return(pos);
  49. pos += offset;
  50. } /* else if */
  51. else if (whence == SEEK_END)
  52. {
  53. PHYSFS_sint64 len = PHYSFS_fileLength(handle);
  54. if (len == -1)
  55. {
  56. SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
  57. return(-1);
  58. } /* if */
  59. pos = (int) len;
  60. if ( ((PHYSFS_sint64) pos) != len )
  61. {
  62. SDL_SetError("Can't fit end-of-file position in an int!");
  63. return(-1);
  64. } /* if */
  65. pos += offset;
  66. } /* else if */
  67. else
  68. {
  69. SDL_SetError("Invalid 'whence' parameter.");
  70. return(-1);
  71. } /* else */
  72. if ( pos < 0 )
  73. {
  74. SDL_SetError("Attempt to seek past start of file.");
  75. return(-1);
  76. } /* if */
  77. if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
  78. {
  79. SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
  80. return(-1);
  81. } /* if */
  82. return(pos);
  83. } /* physfsrwops_seek */
  84. static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
  85. {
  86. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  87. PHYSFS_sint64 rc = PHYSFS_read(handle, ptr, size, maxnum);
  88. if (rc != maxnum)
  89. {
  90. if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
  91. SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
  92. } /* if */
  93. return((int) rc);
  94. } /* physfsrwops_read */
  95. static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
  96. {
  97. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  98. PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num);
  99. if (rc != num)
  100. SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
  101. return((int) rc);
  102. } /* physfsrwops_write */
  103. static int physfsrwops_close(SDL_RWops *rw)
  104. {
  105. PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
  106. if (!PHYSFS_close(handle))
  107. {
  108. SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
  109. return(-1);
  110. } /* if */
  111. SDL_FreeRW(rw);
  112. return(0);
  113. } /* physfsrwops_close */
  114. static SDL_RWops *create_rwops(PHYSFS_File *handle)
  115. {
  116. SDL_RWops *retval = NULL;
  117. if (handle == NULL)
  118. SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
  119. else
  120. {
  121. retval = SDL_AllocRW();
  122. if (retval != NULL)
  123. {
  124. retval->seek = physfsrwops_seek;
  125. retval->read = physfsrwops_read;
  126. retval->write = physfsrwops_write;
  127. retval->close = physfsrwops_close;
  128. retval->hidden.unknown.data1 = handle;
  129. } /* if */
  130. } /* else */
  131. return(retval);
  132. } /* create_rwops */
  133. SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
  134. {
  135. SDL_RWops *retval = NULL;
  136. if (handle == NULL)
  137. SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
  138. else
  139. retval = create_rwops(handle);
  140. return(retval);
  141. } /* PHYSFSRWOPS_makeRWops */
  142. SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
  143. {
  144. return(create_rwops(PHYSFS_openRead(fname)));
  145. } /* PHYSFSRWOPS_openRead */
  146. SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
  147. {
  148. return(create_rwops(PHYSFS_openWrite(fname)));
  149. } /* PHYSFSRWOPS_openWrite */
  150. SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
  151. {
  152. return(create_rwops(PHYSFS_openAppend(fname)));
  153. } /* PHYSFSRWOPS_openAppend */
  154. /* end of physfsrwops.c ... */