123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- /*
- Copyright (C) 2005 Michael Liebscher <johnnycanuck@users.sourceforge.net>
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- #include "../wolfiphone.h"
- // if true, use mmap instead of alloc and read
- //#define USE_MMAP
- /*
- -----------------------------------------------------------------------------
- Function: FS_GetLoadedFilePointer() -Get file pointer.
-
- Parameters:
- filestream -[in] Target file handle.
- origin -[in] Pointer position
- SEEK_SET -Beginning of file.
- SEEK_CUR -Current position of file pointer.
- SEEK_END -End of file.
-
- Returns: File pointer on success, otherwise NULL.
-
- Notes:
- -----------------------------------------------------------------------------
- */
- PUBLIC void *FS_GetLoadedFilePointer( filehandle_t *fhandle, W32 origin )
- {
- switch( origin )
- {
- case SEEK_SET:
- return( (void *)fhandle->ptrStart );
- case SEEK_END:
- return( (void *)fhandle->ptrEnd );
- case SEEK_CUR:
- return( (void *)fhandle->ptrCurrent );
- }
- return NULL;
- }
- /*
- -----------------------------------------------------------------------------
- Function: FS_GetFileSize() -Get the length of a file.
-
- Parameters: filestream -[in] Target file handle.
-
- Returns: The file length in bytes.
-
- Notes:
- -----------------------------------------------------------------------------
- */
- PUBLIC SW32 FS_GetFileSize( filehandle_t *fhandle )
- {
- return fhandle->filesize;
- }
- /*
- -----------------------------------------------------------------------------
- Function: FS_FileSeek() -Moves the file pointer to a specified location.
-
- Parameters:
- fhandle -[in] Pointer to filehandle_t structure.
- offset -[in] Number of bytes from origin
- origin -[in] Initial position
- SEEK_SET -Beginning of file.
- SEEK_CUR -Current position of file pointer.
- SEEK_END -End of file.
-
- Returns: If successful zero, otherwise a nonzero value.
-
- Notes:
- -----------------------------------------------------------------------------
- */
- PUBLIC W32 FS_FileSeek( filehandle_t *fhandle, SW32 offset, W32 origin )
- {
- switch( origin )
- {
- case SEEK_SET:
- if( offset < 0 ||
- offset > fhandle->filesize )
- {
- return 1;
- }
-
- fhandle->ptrCurrent = fhandle->ptrStart + offset;
- break;
- case SEEK_END:
- if( offset > 0 )
- {
- return 1;
- }
- // offset is negative
- if( (fhandle->filesize + offset) < 0 )
- {
- return 1;
- }
- // offset is negative
- fhandle->ptrCurrent = fhandle->ptrEnd + offset;
- break;
- case SEEK_CUR:
- if( offset < 0 )
- {
- // offset is negative
- if( ((fhandle->ptrCurrent - fhandle->ptrStart) + offset) < 0 )
- {
- return 1;
- }
- }
- if( offset > 0 )
- {
- if( offset > (fhandle->ptrEnd - fhandle->ptrCurrent) )
- {
- return 1;
- }
- }
- fhandle->ptrCurrent += offset;
- break;
- default:
- return 1;
- }
- return 0;
- }
- /*
- -----------------------------------------------------------------------------
- Function: FS_FileTell() -Gets the current position of a file pointer.
-
- Parameters: fhandle -[in] Pointer to filehandle_t structure.
-
- Returns: If successful current file position, otherwise -1.
-
- Notes:
- -----------------------------------------------------------------------------
- */
- PUBLIC SW32 FS_FileTell( filehandle_t *fhandle )
- {
- return( fhandle->ptrCurrent - fhandle->ptrStart );
- }
- /*
- -----------------------------------------------------------------------------
- Function: FS_CloseFile -Close file handle.
-
- Parameters: filestream -[in] Pointer to valid FILE structure.
-
- Returns: Nothing.
-
- Notes: Closes a file stream that was returned by FS_FOpenFile.
- -----------------------------------------------------------------------------
- */
- PUBLIC void FS_CloseFile( filehandle_t *fhandle )
- {
- if( fhandle->filedata )
- {
- #ifdef USE_MMAP
- if ( munmap( fhandle->filedata, fhandle->filesize ) != 0 ) {
- assert( 0 );
- }
- #else
- free( fhandle->filedata );
- #endif
- fhandle->filedata = NULL;
- }
- Z_Free( fhandle );
- }
- /*
- -----------------------------------------------------------------------------
- Function: FS_OpenFile -Open file from the file system.
-
- -----------------------------------------------------------------------------
- */
- PUBLIC filehandle_t *FS_OpenFile( const char *filename, W32 FlagsAndAttributes )
- {
- char netpath[ MAX_OSPATH ];
- filehandle_t *hFile;
- const char *pathBase;
- struct stat s;
- int fd;
-
- //
- // Check for the file in the directory tree
- //
- if ( FlagsAndAttributes & FA_FILE_IPHONE_DOC_DIR ) {
- extern char iphoneDocDirectory[1024];
- pathBase = iphoneDocDirectory;
- my_snprintf( netpath, sizeof( netpath ), "%s/%s", pathBase, filename );
- } else {
- // extern char iphoneAppDirectory[1024];
- // pathBase = iphoneAppDirectory;
- pathBase = FS_Gamedir();
- my_snprintf( netpath, sizeof( netpath ), "%s/%s", pathBase, filename );
- }
- // high performance file mapping path, avoiding stdio
- fd = open( netpath, O_RDONLY );
- if ( fd == -1 ) {
- return NULL;
- }
- fstat( fd, &s );
-
- hFile = Z_Malloc( sizeof( filehandle_t ) );
- memset( hFile, 0, sizeof( filehandle_t ) );
-
- hFile->filesize = s.st_size;
- #ifdef USE_MMAP
- hFile->filedata = mmap( NULL, hFile->filesize, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0 );
- if ( (int)hFile->filedata == -1 ) {
- Com_Printf( "mmap failed: %s\n", strerror( errno ) );
- assert( 0 );
- }
- #else
- hFile->filedata = malloc( hFile->filesize );
- read( fd, hFile->filedata, hFile->filesize );
- #endif
- hFile->ptrStart = hFile->ptrCurrent = (PW8)hFile->filedata;
- hFile->ptrEnd = (PW8)hFile->filedata + hFile->filesize;
- hFile->bLoaded = true;
- // mmap doesn't require the file to stay open
- close( fd );
- return hFile;
- }
- /*
- -----------------------------------------------------------------------------
- Function: FS_ReadFile -Reads data from a stream.
-
- Parameters: buffer -[in/out] Storage location for data.
- size -[in] Item size in bytes.
- count -[in] Maximum number of items to be read.
- fhandle -[in] Pointer to valid filehandle_t structure.
-
- Returns: On success number of full items actually read, otherwise -1.
-
- Notes:
- -----------------------------------------------------------------------------
- */
- PUBLIC SW32 FS_ReadFile( void *buffer, W32 size, W32 count, filehandle_t *fhandle )
- {
- W8 *buf = (PW8)buffer;
- W32 i;
- if( (size * count) > (fhandle->ptrEnd - fhandle->ptrCurrent) )
- {
- SW32 read;
- read = (fhandle->ptrEnd - fhandle->ptrCurrent);
- for( i = 0 ; i < (fhandle->ptrEnd - fhandle->ptrCurrent) ; ++i )
- {
- buf[ i ] = fhandle->ptrCurrent[ i ];
- }
- fhandle->ptrCurrent = fhandle->ptrEnd;
-
- return( read );
- }
- else
- {
- for( i = 0 ; i < (size * count) ; ++i, fhandle->ptrCurrent++ )
- {
- buf[ i ] = *fhandle->ptrCurrent;
- }
-
- return( (size * count) / size );
- }
- /* should never get here */
- return -1;
- }
|