fileio.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. Copyright (C) 2005 Michael Liebscher <johnnycanuck@users.sourceforge.net>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "../wolfiphone.h"
  16. // if true, use mmap instead of alloc and read
  17. //#define USE_MMAP
  18. /*
  19. -----------------------------------------------------------------------------
  20. Function: FS_GetLoadedFilePointer() -Get file pointer.
  21. Parameters:
  22. filestream -[in] Target file handle.
  23. origin -[in] Pointer position
  24. SEEK_SET -Beginning of file.
  25. SEEK_CUR -Current position of file pointer.
  26. SEEK_END -End of file.
  27. Returns: File pointer on success, otherwise NULL.
  28. Notes:
  29. -----------------------------------------------------------------------------
  30. */
  31. PUBLIC void *FS_GetLoadedFilePointer( filehandle_t *fhandle, W32 origin )
  32. {
  33. switch( origin )
  34. {
  35. case SEEK_SET:
  36. return( (void *)fhandle->ptrStart );
  37. case SEEK_END:
  38. return( (void *)fhandle->ptrEnd );
  39. case SEEK_CUR:
  40. return( (void *)fhandle->ptrCurrent );
  41. }
  42. return NULL;
  43. }
  44. /*
  45. -----------------------------------------------------------------------------
  46. Function: FS_GetFileSize() -Get the length of a file.
  47. Parameters: filestream -[in] Target file handle.
  48. Returns: The file length in bytes.
  49. Notes:
  50. -----------------------------------------------------------------------------
  51. */
  52. PUBLIC SW32 FS_GetFileSize( filehandle_t *fhandle )
  53. {
  54. return fhandle->filesize;
  55. }
  56. /*
  57. -----------------------------------------------------------------------------
  58. Function: FS_FileSeek() -Moves the file pointer to a specified location.
  59. Parameters:
  60. fhandle -[in] Pointer to filehandle_t structure.
  61. offset -[in] Number of bytes from origin
  62. origin -[in] Initial position
  63. SEEK_SET -Beginning of file.
  64. SEEK_CUR -Current position of file pointer.
  65. SEEK_END -End of file.
  66. Returns: If successful zero, otherwise a nonzero value.
  67. Notes:
  68. -----------------------------------------------------------------------------
  69. */
  70. PUBLIC W32 FS_FileSeek( filehandle_t *fhandle, SW32 offset, W32 origin )
  71. {
  72. switch( origin )
  73. {
  74. case SEEK_SET:
  75. if( offset < 0 ||
  76. offset > fhandle->filesize )
  77. {
  78. return 1;
  79. }
  80. fhandle->ptrCurrent = fhandle->ptrStart + offset;
  81. break;
  82. case SEEK_END:
  83. if( offset > 0 )
  84. {
  85. return 1;
  86. }
  87. // offset is negative
  88. if( (fhandle->filesize + offset) < 0 )
  89. {
  90. return 1;
  91. }
  92. // offset is negative
  93. fhandle->ptrCurrent = fhandle->ptrEnd + offset;
  94. break;
  95. case SEEK_CUR:
  96. if( offset < 0 )
  97. {
  98. // offset is negative
  99. if( ((fhandle->ptrCurrent - fhandle->ptrStart) + offset) < 0 )
  100. {
  101. return 1;
  102. }
  103. }
  104. if( offset > 0 )
  105. {
  106. if( offset > (fhandle->ptrEnd - fhandle->ptrCurrent) )
  107. {
  108. return 1;
  109. }
  110. }
  111. fhandle->ptrCurrent += offset;
  112. break;
  113. default:
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. /*
  119. -----------------------------------------------------------------------------
  120. Function: FS_FileTell() -Gets the current position of a file pointer.
  121. Parameters: fhandle -[in] Pointer to filehandle_t structure.
  122. Returns: If successful current file position, otherwise -1.
  123. Notes:
  124. -----------------------------------------------------------------------------
  125. */
  126. PUBLIC SW32 FS_FileTell( filehandle_t *fhandle )
  127. {
  128. return( fhandle->ptrCurrent - fhandle->ptrStart );
  129. }
  130. /*
  131. -----------------------------------------------------------------------------
  132. Function: FS_CloseFile -Close file handle.
  133. Parameters: filestream -[in] Pointer to valid FILE structure.
  134. Returns: Nothing.
  135. Notes: Closes a file stream that was returned by FS_FOpenFile.
  136. -----------------------------------------------------------------------------
  137. */
  138. PUBLIC void FS_CloseFile( filehandle_t *fhandle )
  139. {
  140. if( fhandle->filedata )
  141. {
  142. #ifdef USE_MMAP
  143. if ( munmap( fhandle->filedata, fhandle->filesize ) != 0 ) {
  144. assert( 0 );
  145. }
  146. #else
  147. free( fhandle->filedata );
  148. #endif
  149. fhandle->filedata = NULL;
  150. }
  151. Z_Free( fhandle );
  152. }
  153. /*
  154. -----------------------------------------------------------------------------
  155. Function: FS_OpenFile -Open file from the file system.
  156. -----------------------------------------------------------------------------
  157. */
  158. PUBLIC filehandle_t *FS_OpenFile( const char *filename, W32 FlagsAndAttributes )
  159. {
  160. char netpath[ MAX_OSPATH ];
  161. filehandle_t *hFile;
  162. const char *pathBase;
  163. struct stat s;
  164. int fd;
  165. //
  166. // Check for the file in the directory tree
  167. //
  168. if ( FlagsAndAttributes & FA_FILE_IPHONE_DOC_DIR ) {
  169. extern char iphoneDocDirectory[1024];
  170. pathBase = iphoneDocDirectory;
  171. my_snprintf( netpath, sizeof( netpath ), "%s/%s", pathBase, filename );
  172. } else {
  173. // extern char iphoneAppDirectory[1024];
  174. // pathBase = iphoneAppDirectory;
  175. pathBase = FS_Gamedir();
  176. my_snprintf( netpath, sizeof( netpath ), "%s/%s", pathBase, filename );
  177. }
  178. // high performance file mapping path, avoiding stdio
  179. fd = open( netpath, O_RDONLY );
  180. if ( fd == -1 ) {
  181. return NULL;
  182. }
  183. fstat( fd, &s );
  184. hFile = Z_Malloc( sizeof( filehandle_t ) );
  185. memset( hFile, 0, sizeof( filehandle_t ) );
  186. hFile->filesize = s.st_size;
  187. #ifdef USE_MMAP
  188. hFile->filedata = mmap( NULL, hFile->filesize, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0 );
  189. if ( (int)hFile->filedata == -1 ) {
  190. Com_Printf( "mmap failed: %s\n", strerror( errno ) );
  191. assert( 0 );
  192. }
  193. #else
  194. hFile->filedata = malloc( hFile->filesize );
  195. read( fd, hFile->filedata, hFile->filesize );
  196. #endif
  197. hFile->ptrStart = hFile->ptrCurrent = (PW8)hFile->filedata;
  198. hFile->ptrEnd = (PW8)hFile->filedata + hFile->filesize;
  199. hFile->bLoaded = true;
  200. // mmap doesn't require the file to stay open
  201. close( fd );
  202. return hFile;
  203. }
  204. /*
  205. -----------------------------------------------------------------------------
  206. Function: FS_ReadFile -Reads data from a stream.
  207. Parameters: buffer -[in/out] Storage location for data.
  208. size -[in] Item size in bytes.
  209. count -[in] Maximum number of items to be read.
  210. fhandle -[in] Pointer to valid filehandle_t structure.
  211. Returns: On success number of full items actually read, otherwise -1.
  212. Notes:
  213. -----------------------------------------------------------------------------
  214. */
  215. PUBLIC SW32 FS_ReadFile( void *buffer, W32 size, W32 count, filehandle_t *fhandle )
  216. {
  217. W8 *buf = (PW8)buffer;
  218. W32 i;
  219. if( (size * count) > (fhandle->ptrEnd - fhandle->ptrCurrent) )
  220. {
  221. SW32 read;
  222. read = (fhandle->ptrEnd - fhandle->ptrCurrent);
  223. for( i = 0 ; i < (fhandle->ptrEnd - fhandle->ptrCurrent) ; ++i )
  224. {
  225. buf[ i ] = fhandle->ptrCurrent[ i ];
  226. }
  227. fhandle->ptrCurrent = fhandle->ptrEnd;
  228. return( read );
  229. }
  230. else
  231. {
  232. for( i = 0 ; i < (size * count) ; ++i, fhandle->ptrCurrent++ )
  233. {
  234. buf[ i ] = *fhandle->ptrCurrent;
  235. }
  236. return( (size * count) / size );
  237. }
  238. /* should never get here */
  239. return -1;
  240. }