physfsrwops.c 5.1 KB

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