diff --git a/filesystem/VFileSystem009.cpp b/filesystem/VFileSystem009.cpp index ef3082ea..4fbde216 100644 --- a/filesystem/VFileSystem009.cpp +++ b/filesystem/VFileSystem009.cpp @@ -175,12 +175,8 @@ public: FileHandle_t Open( const char *path, const char *mode, const char *id ) override { - file_t *fd; - FixupPath( p, path ); - fd = FS_Open( p, mode, IsIdGamedir( id )); - - return fd; + return FS_Open( p, mode, IsIdGamedir( id )); } void Close( FileHandle_t handle ) override @@ -304,19 +300,16 @@ public: const char *FindFirst( const char *pattern, FileFindHandle_t *handle, const char *id ) override { - CSearchState *state; - search_t *search; - if( !handle || !pattern ) return nullptr; FixupPath( p, pattern ); - search = FS_Search( p, true, IsIdGamedir( id )); + search_t *search = FS_Search( p, true, IsIdGamedir( id )); if( !search ) return nullptr; - state = new CSearchState( &searchHead, search ); + CSearchState *state = new CSearchState( &searchHead, search ); if( !state ) { Mem_Free( search ); @@ -378,8 +371,6 @@ public: const char *GetLocalPath( const char *name, char *buf, int size ) override { - const char *fullpath; - if( !name ) return nullptr; @@ -396,7 +387,7 @@ public: return buf; } - fullpath = FS_GetDiskPath( p, false ); + const char *fullpath = FS_GetDiskPath( p, false ); if( !fullpath ) return nullptr; @@ -407,10 +398,8 @@ public: char *ParseFile( char *buf, char *token, bool *quoted ) override { qboolean qquoted; - char *p; - // filesystem_stdio expects 512 byte buffers - p = COM_ParseFileSafe( buf, token, 512, 0, nullptr, &qquoted ); + char *p = COM_ParseFileSafe( buf, token, 512, 0, nullptr, &qquoted ); if( quoted ) *quoted = qquoted; diff --git a/filesystem/android.c b/filesystem/android.c index 8f022867..d15b4ae3 100644 --- a/filesystem/android.c +++ b/filesystem/android.c @@ -55,9 +55,7 @@ struct jni_methods_s static void Android_GetAssetManager( android_assets_t *assets ) { - jobject assetManager; - - assetManager = (*jni.env)->CallObjectMethod( jni.env, jni.activity, jni.getAssets, assets->engine ); + jobject assetManager = (*jni.env)->CallObjectMethod( jni.env, jni.activity, jni.getAssets, assets->engine ); if( assetManager ) assets->asset_manager = AAssetManager_fromJava( jni.env, assetManager ); @@ -68,11 +66,9 @@ static void Android_GetAssetManager( android_assets_t *assets ) static const char *Android_GetPackageName( qboolean engine ) { static string pkg; - jstring resultJNIStr; + jstring resultJNIStr = (*jni.env)->CallObjectMethod( jni.env, jni.activity, engine ? jni.getPackageName : jni.getCallingPackage ); const char *resultCStr; - resultJNIStr = (*jni.env)->CallObjectMethod( jni.env, jni.activity, engine ? jni.getPackageName : jni.getCallingPackage ); - if( !resultJNIStr ) return NULL; @@ -144,7 +140,6 @@ static int FS_FileTime_AndroidAssets( searchpath_t *search, const char *filename if( !time ) { struct tm file_tm; - strptime( g_buildcommit_date, "%Y-%m-%d %H:%M:%S", &file_tm ); time = mktime( &file_tm ); } @@ -181,15 +176,13 @@ static void FS_Search_AndroidAssets( searchpath_t *search, stringlist_t *list, c { string temp; stringlist_t dirlist; - const char *slash, *backslash, *colon, *separator; - int basepathlength, dirlistindex, resultlistindex; + const char *slash = Q_strrchr( pattern, '/' ); + const char *backslash = Q_strrchr( pattern, '\\' ); + const char *colon = Q_strrchr( pattern, ':' ); + const char *separator = Q_max( slash, backslash ); + int basepathlength, dirlistindex; char *basepath; - slash = Q_strrchr( pattern, '/' ); - backslash = Q_strrchr( pattern, '\\' ); - colon = Q_strrchr( pattern, ':' ); - - separator = Q_max( slash, backslash ); separator = Q_max( separator, colon ); basepathlength = separator ? (separator + 1 - pattern) : 0; @@ -209,6 +202,8 @@ static void FS_Search_AndroidAssets( searchpath_t *search, stringlist_t *list, c if( matchpattern( temp, (char *)pattern, true )) { + int resultlistindex; + for( resultlistindex = 0; resultlistindex < list->numstrings; resultlistindex++ ) { if( !Q_strcmp( list->strings[resultlistindex], temp )) @@ -245,11 +240,10 @@ static byte *FS_LoadAndroidAssetsFile( searchpath_t *search, const char *path, i { byte *buf; off_t size; - AAsset *asset; + AAsset *asset = AAssetManager_open( search->assets->asset_manager, path, AASSET_MODE_BUFFER ); if( filesize ) *filesize = 0; - asset = AAssetManager_open( search->assets->asset_manager, path, AASSET_MODE_BUFFER ); if( !asset ) return NULL; diff --git a/filesystem/dir.c b/filesystem/dir.c index b6f3d4a0..6408e48e 100644 --- a/filesystem/dir.c +++ b/filesystem/dir.c @@ -65,9 +65,8 @@ static qboolean Platform_GetDirectoryCaseSensitivity( const char *dir ) return true; #elif XASH_LINUX && defined( FS_IOC_GETFLAGS ) int flags = 0; - int fd; + int fd = open( dir, O_RDONLY | O_NONBLOCK ); - fd = open( dir, O_RDONLY | O_NONBLOCK ); if( fd < 0 ) return true; @@ -96,8 +95,7 @@ static void FS_FreeDirEntries( dir_t *dir ) { if( dir->entries ) { - int i; - for( i = 0; i < dir->numentries; i++ ) + for( int i = 0; i < dir->numentries; i++ ) FS_FreeDirEntries( &dir->entries[i] ); dir->entries = NULL; } @@ -107,12 +105,10 @@ static void FS_FreeDirEntries( dir_t *dir ) static void FS_InitDirEntries( dir_t *dir, const stringlist_t *list ) { - int i; - dir->numentries = list->numstrings; dir->entries = Mem_Malloc( fs_mempool, sizeof( dir_t ) * dir->numentries ); - for( i = 0; i < list->numstrings; i++ ) + for( int i = 0; i < list->numstrings; i++ ) { dir_t *entry = &dir->entries[i]; @@ -158,18 +154,14 @@ static void FS_PopulateDirEntries( dir_t *dir, const char *path ) static int FS_FindDirEntry( dir_t *dir, const char *name ) { - int left, right; - // look for the file (binary search) - left = 0; - right = dir->numentries - 1; + int left = 0; + int right = dir->numentries - 1; while( left <= right ) { - int middle = (left + right) / 2; - int diff; - - diff = Q_stricmp( dir->entries[middle].name, name ); + int middle = (left + right) / 2; + int diff = Q_stricmp( dir->entries[middle].name, name ); // found it if( !diff ) @@ -185,7 +177,6 @@ static int FS_FindDirEntry( dir_t *dir, const char *name ) static void FS_MergeDirEntries( dir_t *dir, const stringlist_t *list ) { - int i; dir_t temp; // glorified realloc for sorted dir entries @@ -194,7 +185,7 @@ static void FS_MergeDirEntries( dir_t *dir, const stringlist_t *list ) FS_InitDirEntries( &temp, list ); - for( i = 0; i < dir->numentries; i++ ) + for( int i = 0; i < dir->numentries; i++ ) { dir_t *oldentry = &dir->entries[i]; dir_t *newentry; @@ -417,15 +408,13 @@ static void FS_Search_DIR( searchpath_t *search, stringlist_t *list, const char { string netpath, temp; stringlist_t dirlist; - const char *slash, *backslash, *colon, *separator; - int basepathlength, dirlistindex, resultlistindex; + const char *slash = Q_strrchr( pattern, '/' ); + const char *backslash = Q_strrchr( pattern, '\\' ); + const char *colon = Q_strrchr( pattern, ':' ); + const char *separator = Q_max( slash, backslash ); + int basepathlength, dirlistindex; char *basepath; - slash = Q_strrchr( pattern, '/' ); - backslash = Q_strrchr( pattern, '\\' ); - colon = Q_strrchr( pattern, ':' ); - - separator = Q_max( slash, backslash ); separator = Q_max( separator, colon ); basepathlength = separator ? (separator + 1 - pattern) : 0; @@ -450,6 +439,8 @@ static void FS_Search_DIR( searchpath_t *search, stringlist_t *list, const char if( matchpattern( temp, (char *)pattern, true ) ) { + int resultlistindex; + for( resultlistindex = 0; resultlistindex < list->numstrings; resultlistindex++ ) { if( !Q_strcmp( list->strings[resultlistindex], temp ) ) @@ -476,8 +467,8 @@ static int FS_FileTime_DIR( searchpath_t *search, const char *filename ) static file_t *FS_OpenFile_DIR( searchpath_t *search, const char *filename, const char *mode, int pack_ind ) { - file_t *f; char path[MAX_SYSPATH]; + file_t *f; Q_snprintf( path, sizeof( path ), "%s%s", search->filename, filename ); f = FS_SysOpen( path, mode ); @@ -515,9 +506,8 @@ void FS_InitDirectorySearchpath( searchpath_t *search, const char *path, int fla searchpath_t *FS_AddDir_Fullpath( const char *path, int flags ) { - searchpath_t *search; + searchpath_t *search = (searchpath_t *)Mem_Calloc( fs_mempool, sizeof( searchpath_t )); - search = (searchpath_t *)Mem_Calloc( fs_mempool, sizeof( searchpath_t )); FS_InitDirectorySearchpath( search, path, flags ); Con_Printf( "Adding directory: %s\n", path ); diff --git a/filesystem/gameinfo.c b/filesystem/gameinfo.c index 0226e3df..a535a3cf 100644 --- a/filesystem/gameinfo.c +++ b/filesystem/gameinfo.c @@ -39,8 +39,8 @@ assume GameInfo is valid */ static qboolean FS_WriteGameInfo( const char *filepath, const gameinfo_t *GameInfo ) { - file_t *f = FS_Open( filepath, "w", false ); // we in binary-mode - int i, write_ambients = false; + file_t *f = FS_Open( filepath, "w", false ); // we in binary-mode + int write_ambients = false; if( !f ) return false; @@ -128,7 +128,7 @@ static qboolean FS_WriteGameInfo( const char *filepath, const gameinfo_t *GameIn if( GameInfo->max_particles > 0 ) FS_Printf( f, "max_particles\t%i\n", GameInfo->max_particles ); - for( i = 0; i < NUM_AMBIENTS; i++ ) + for( int i = 0; i < NUM_AMBIENTS; i++ ) { if( *GameInfo->ambientsound[i] ) { @@ -421,7 +421,7 @@ static void FS_ParseGenericGameInfo( gameinfo_t *GameInfo, const char *buf, cons } else if( !Q_strnicmp( token, "ambient", 7 )) { - int ambientNum = Q_atoi( token + 7 ); + int ambientNum = Q_atoi( token + 7 ); if( ambientNum < 0 || ambientNum >= NUM_AMBIENTS ) ambientNum = 0; @@ -569,10 +569,9 @@ static qboolean FS_CheckForQuakeGameDir( const char *gamedir ) // if directory contain quake.rc or progs.dat it's 100% quake gamedir // quake mods probably always archived, so check pak0.pak too const char *files[] = { "pak0.pak", "PAK0.PAK", "progs.dat", "quake.rc" }; - int i; // search it in the filesystem - for( i = 0; i < sizeof( files ) / sizeof( files[0] ); i++ ) + for( int i = 0; i < sizeof( files ) / sizeof( files[0] ); i++ ) { char buf[MAX_SYSPATH]; @@ -607,9 +606,8 @@ static qboolean FS_CheckForXashGameDir( const char *gamedir ) { // if directory contain gameinfo.txt or liblist.gam it's 100% gamedir const char *files[] = { "gameinfo.txt", "liblist.gam" }; - int i; - for( i = 0; i < sizeof( files ) / sizeof( files[0] ); i++ ) + for( int i = 0; i < sizeof( files ) / sizeof( files[0] ); i++ ) { char buf[MAX_SYSPATH]; @@ -633,8 +631,8 @@ qboolean FS_ParseGameInfo( const char *gamedir, gameinfo_t *GameInfo, qboolean r char liblist_path[MAX_SYSPATH]; char gameinfo_path[MAX_SYSPATH]; char gamedir_path[MAX_SYSPATH]; - time_t liblist_mtime = -1; - time_t gameinfo_mtime = -1; + time_t liblist_mtime; + time_t gameinfo_mtime; if( rodir ) Q_snprintf( gamedir_path, sizeof( gamedir_path ), "%s/%s", fs_rodir, gamedir ); diff --git a/filesystem/io.c b/filesystem/io.c index b5627c37..0d373d10 100644 --- a/filesystem/io.c +++ b/filesystem/io.c @@ -62,11 +62,9 @@ Look for a file in the search paths and open it in read-only mode */ file_t *FS_OpenReadFile( const char *filename, const char *mode, qboolean gamedironly ) { - searchpath_t *search; char netpath[MAX_SYSPATH]; int pack_ind; - - search = FS_FindFile( filename, &pack_ind, netpath, sizeof( netpath ), gamedironly ? FS_GAMEDIRONLY_SEARCH_FLAGS : 0 ); + searchpath_t *search = FS_FindFile( filename, &pack_ind, netpath, sizeof( netpath ), gamedironly ? FS_GAMEDIRONLY_SEARCH_FLAGS : 0 ); // not found? if( search == NULL ) @@ -100,7 +98,7 @@ file_t *FS_Open( const char *filepath, const char *mode, qboolean gamedironly ) // if the file is opened in "write", "append", or "read/write" mode if( mode[0] == 'w' || mode[0] == 'a'|| mode[0] == 'e' || Q_strchr( mode, '+' )) { - char real_path[MAX_SYSPATH]; + char real_path[MAX_SYSPATH]; // open the file on disk directly if( !FS_FixFileCase( fs_writepath->dir, filepath, real_path, sizeof( real_path ), true )) @@ -179,7 +177,7 @@ Write "datasize" bytes into a file */ fs_offset_t FS_Write( file_t *file, const void *data, size_t datasize ) { - fs_offset_t result; + fs_offset_t result; if( !file ) return 0; @@ -211,9 +209,9 @@ Read up to "buffersize" bytes from a file */ fs_offset_t FS_Read( file_t *file, void *buffer, size_t buffersize ) { - fs_offset_t done; - fs_offset_t nb; - fs_offset_t count; + fs_offset_t done; + fs_offset_t nb; + fs_offset_t count; // nothing to copy if( buffersize == 0 ) return 1; @@ -395,8 +393,8 @@ Print a string into a file */ int FS_Printf( file_t *file, const char *format, ... ) { - int result; - va_list args; + int result; + va_list args; va_start( args, format ); result = FS_VPrintf( file, format, args ); @@ -414,9 +412,9 @@ Print a string into a file */ int FS_VPrintf( file_t *file, const char *format, va_list ap ) { - int len; - fs_offset_t buff_size = MAX_SYSPATH; - char *tempbuff; + int len; + fs_offset_t buff_size = MAX_SYSPATH; + char *tempbuff; if( !file ) return 0; @@ -447,7 +445,7 @@ Get the next character of a file */ int FS_Getc( file_t *file ) { - char c; + char c; if( FS_Read( file, &c, 1 ) != 1 ) return EOF; @@ -481,7 +479,7 @@ Same as fgets */ int FS_Gets( file_t *file, char *string, size_t bufsize ) { - int c, end = 0; + int c, end = 0; while( 1 ) { @@ -636,11 +634,11 @@ FS_LoadFileFromArchive */ byte *FS_LoadFileFromArchive( searchpath_t *sp, const char *path, int pack_ind, fs_offset_t *filesizeptr, const qboolean sys_malloc ) { - fs_offset_t filesize; - file_t *file; - byte *buf; void *( *pfnAlloc )( size_t ) = sys_malloc ? malloc : FS_CustomAlloc; void ( *pfnFree )( void * ) = sys_malloc ? free : FS_CustomFree; + fs_offset_t filesize; + file_t *file; + byte *buf; // custom load file function for compressed files if( sp->pfnLoadFile ) @@ -679,9 +677,9 @@ Always appends a 0 byte. */ static byte *FS_LoadFile_( const char *path, fs_offset_t *filesizeptr, const qboolean gamedironly, const qboolean custom_alloc ) { - searchpath_t *search; char netpath[MAX_SYSPATH]; int pack_ind; + searchpath_t *search; // some mappers used leading '/' or '\' in path to models or sounds if( path[0] == '/' || path[0] == '\\' ) @@ -719,18 +717,16 @@ CRC32_File */ qboolean CRC32_File( dword *crcvalue, const char *filename ) { - char buffer[1024]; - int num_bytes; - file_t *f; + char buffer[1024]; + file_t *f = FS_Open( filename, "rb", false ); - f = FS_Open( filename, "rb", false ); if( !f ) return false; CRC32_Init( crcvalue ); while( 1 ) { - num_bytes = FS_Read( f, buffer, sizeof( buffer )); + int num_bytes = FS_Read( f, buffer, sizeof( buffer )); if( num_bytes > 0 ) CRC32_ProcessBuffer( crcvalue, buffer, num_bytes ); @@ -750,10 +746,10 @@ MD5_HashFile */ qboolean MD5_HashFile( byte digest[16], const char *pszFileName, uint seed[4] ) { - file_t *file; - MD5Context_t MD5_Hash = { 0 }; + file_t *file = FS_Open( pszFileName, "rb", false ); + MD5Context_t MD5_Hash = { 0 }; - if(( file = FS_Open( pszFileName, "rb", false )) == NULL ) + if( !file ) return false; MD5Init( &MD5_Hash ); @@ -787,11 +783,9 @@ FS_LoadDirectFile */ byte *FS_LoadDirectFile( const char *path, fs_offset_t *filesizeptr ) { - file_t *file; - byte *buf = NULL; - fs_offset_t filesize = 0; - - file = FS_SysOpen( path, "rb" ); + file_t *file = FS_SysOpen( path, "rb" ); + fs_offset_t filesize; + byte *buf; if( !file ) return NULL; @@ -819,9 +813,7 @@ The filename will be prefixed by the current game directory */ qboolean FS_WriteFile( const char *filename, const void *data, fs_offset_t len ) { - file_t *file; - - file = FS_Open( filename, "wb", false ); + file_t *file = FS_Open( filename, "wb", false ); if( !file ) { @@ -875,10 +867,8 @@ return false for file in pack */ qboolean FS_GetFullDiskPath( char *buffer, size_t size, const char *name, qboolean gamedironly ) { - searchpath_t *search; char temp[MAX_SYSPATH]; - - search = FS_FindFile( name, NULL, temp, sizeof( temp ), gamedironly ? FS_GAMEDIRONLY_SEARCH_FLAGS : 0 ); + searchpath_t *search = FS_FindFile( name, NULL, temp, sizeof( temp ), gamedironly ? FS_GAMEDIRONLY_SEARCH_FLAGS : 0 ); if( search && search->type == SEARCHPATH_PLAIN ) { @@ -898,10 +888,8 @@ return size of file in bytes */ fs_offset_t FS_FileSize( const char *filename, qboolean gamedironly ) { - int length = -1; // in case file was missed - file_t *fp; - - fp = FS_Open( filename, "rb", gamedironly ); + int length = -1; // in case file was missed + file_t *fp = FS_Open( filename, "rb", gamedironly ); if( fp ) { @@ -936,11 +924,10 @@ return time of creation file in seconds */ int FS_FileTime( const char *filename, qboolean gamedironly ) { - searchpath_t *search; char netpath[MAX_SYSPATH]; int pack_ind; + searchpath_t *search = FS_FindFile( filename, &pack_ind, netpath, sizeof( netpath ), gamedironly ? FS_GAMEDIRONLY_SEARCH_FLAGS : 0 ); - search = FS_FindFile( filename, &pack_ind, netpath, sizeof( netpath ), gamedironly ? FS_GAMEDIRONLY_SEARCH_FLAGS : 0 ); if( !search ) return -1; // doesn't exist @@ -1040,9 +1027,9 @@ FS_FileCopy */ qboolean FS_FileCopy( file_t *pOutput, file_t *pInput, int fileSize ) { - char *buf = Mem_Malloc( fs_mempool, FILE_COPY_SIZE ); - int size, readSize; - qboolean done = true; + char *buf = Mem_Malloc( fs_mempool, FILE_COPY_SIZE ); + int size, readSize; + qboolean done = true; while( fileSize > 0 ) { diff --git a/filesystem/pak.c b/filesystem/pak.c index dc525c8c..302c3238 100644 --- a/filesystem/pak.c +++ b/filesystem/pak.c @@ -98,17 +98,15 @@ of the list so they override previous pack files. static pack_t *FS_LoadPackPAK( const char *packfile, int *error ) { dpackheader_t header; - file_t *packhandle; - int numpackfiles; - pack_t *pack; - fs_size_t c; - int i; + int numpackfiles; + pack_t *pack; + fs_size_t c; // TODO: use FS_Open to allow PK3 to be included into other archives // Currently, it doesn't work with rodir due to FS_FindFile logic // when it will use FS_Open, check that FS_CheckForQuakePak correctly // detects Quake gamedirs in RoDir - packhandle = FS_SysOpen( packfile, "rb" ); + file_t *packhandle = FS_SysOpen( packfile, "rb" ); if( packhandle == NULL ) { @@ -170,7 +168,7 @@ static pack_t *FS_LoadPackPAK( const char *packfile, int *error ) return NULL; } - for( i = 0; i < numpackfiles; i++ ) + for( int i = 0; i < numpackfiles; i++ ) { pack->files[i].filepos = LittleLong( pack->files[i].filepos ); pack->files[i].filelen = LittleLong( pack->files[i].filelen ); @@ -203,9 +201,7 @@ Open a packed file using its package file descriptor */ static file_t *FS_OpenFile_PAK( searchpath_t *search, const char *filename, const char *mode, int pack_ind ) { - dpackfile_t *pfile; - - pfile = &search->pack->files[pack_ind]; + dpackfile_t *pfile = &search->pack->files[pack_ind]; return FS_OpenHandle( search, search->pack->handle->handle, pfile->filepos, pfile->filelen ); } @@ -218,17 +214,14 @@ FS_FindFile_PAK */ static int FS_FindFile_PAK( searchpath_t *search, const char *path, char *fixedname, size_t len ) { - int left, right, middle; - // look for the file (binary search) - left = 0; - right = search->pack->numfiles - 1; + int left = 0; + int right = search->pack->numfiles - 1; + while( left <= right ) { - int diff; - - middle = (left + right) / 2; - diff = Q_stricmp( search->pack->files[middle].name, path ); + int middle = (left + right) / 2; + int diff = Q_stricmp( search->pack->files[middle].name, path ); // Found it if( !diff ) @@ -256,16 +249,18 @@ FS_Search_PAK static void FS_Search_PAK( searchpath_t *search, stringlist_t *list, const char *pattern, int caseinsensitive ) { string temp; - const char *slash, *backslash, *colon, *separator; - int j, i; - for( i = 0; i < search->pack->numfiles; i++ ) + for( int i = 0; i < search->pack->numfiles; i++ ) { Q_strncpy( temp, search->pack->files[i].name, sizeof( temp )); while( temp[0] ) { + const char *slash, *backslash, *colon, *separator; + if( matchpattern( temp, pattern, true )) { + int j; + for( j = 0; j < list->numstrings; j++ ) { if( !Q_strcmp( list->strings[j], temp )) @@ -348,10 +343,8 @@ plain directories. searchpath_t *FS_AddPak_Fullpath( const char *pakfile, int flags ) { searchpath_t *search; - pack_t *pak; int errorcode = PAK_LOAD_COULDNT_OPEN; - - pak = FS_LoadPackPAK( pakfile, &errorcode ); + pack_t *pak = FS_LoadPackPAK( pakfile, &errorcode ); if( !pak ) { @@ -389,14 +382,12 @@ and find progs.dat in it qboolean FS_CheckForQuakePak( const char *pakfile, const char *files[], size_t num_files ) { qboolean is_quake = false; - pack_t *pak; - int i; + pack_t *pak = FS_LoadPackPAK( pakfile, NULL ); - pak = FS_LoadPackPAK( pakfile, NULL ); if( !pak ) return false; - for( i = 0; i < num_files; i++ ) + for( int i = 0; i < num_files; i++ ) { int j; diff --git a/filesystem/searchpath.c b/filesystem/searchpath.c index 51d3ff80..28e2e5b2 100644 --- a/filesystem/searchpath.c +++ b/filesystem/searchpath.c @@ -95,10 +95,9 @@ static searchpath_t *FS_AddArchive_Fullpath( const fs_archive_t *archive, const if( !archive ) { - int i; const char *ext = COM_FileExtension( file ); - for( i = 0; i < sizeof( g_archives ) / sizeof( g_archives[0] ); i++ ) + for( int i = 0; i < sizeof( g_archives ) / sizeof( g_archives[0] ); i++ ) { if( !Q_stricmp( g_archives[i].ext, ext )) { @@ -140,7 +139,6 @@ static searchpath_t *FS_AddArchive_Fullpath( const fs_archive_t *archive, const if( archive->load_wads && !FBitSet( flags, FS_SKIP_ARCHIVED_WADS )) { stringlist_t list; - int i; stringlistinit( &list ); search->pfnSearch( search, &list, "*.wad", true ); @@ -150,7 +148,7 @@ static searchpath_t *FS_AddArchive_Fullpath( const fs_archive_t *archive, const ClearBits( flags, FS_EXEC_PATH ); SetBits( flags, FS_NOWRITE_PATH ); - for( i = 0; i < list.numstrings; i++ ) + for( int i = 0; i < list.numstrings; i++ ) { searchpath_t *wad; char fullpath[MAX_SYSPATH]; @@ -192,18 +190,16 @@ void FS_AddGameDirectory( const char *dir, uint flags ) { stringlist_t list; searchpath_t *search; - int j; stringlistinit( &list ); listdirectory( &list, dir, false ); stringlistsort( &list ); - for( j = 0; j < sizeof( g_archives ) / sizeof( g_archives[0] ); j++ ) + for( int j = 0; j < sizeof( g_archives ) / sizeof( g_archives[0] ); j++ ) { char fullpath[MAX_SYSPATH]; - int i; - for( i = 0; i < list.numstrings; i++ ) + for( int i = 0; i < list.numstrings; i++ ) { if( Q_stricmp( COM_FileExtension( list.strings[i] ), g_archives[j].ext )) continue; @@ -234,10 +230,7 @@ FS_ClearSearchPath */ void FS_ClearSearchPath( void ) { - searchpath_t *cur, **prev; - int i; - - prev = &fs_searchpaths; + searchpath_t *cur, **prev = &fs_searchpaths; while( true ) { @@ -258,7 +251,7 @@ void FS_ClearSearchPath( void ) Mem_Free( cur ); } - for( i = 0; i < FI.numgames; i++ ) + for( int i = 0; i < FI.numgames; i++ ) { if( FI.games[i] ) FI.games[i]->added = false; @@ -485,7 +478,7 @@ can be passed null arg */ void FS_LoadGameInfo( uint32_t flags, const char *language ) { - int i; + int i; // lock uplevel of gamedir for read\write FS_AllowDirectPaths( false ); @@ -547,9 +540,9 @@ static qboolean FS_CheckForCrypt( const char *dllname ) static int FS_StripIdiotRelativePath( const char *dllname, const char *gamefolder ) { string idiot_relpath; - int len; + int len = Q_snprintf( idiot_relpath, sizeof( idiot_relpath ), "../%s/", gamefolder ); - if(( len = Q_snprintf( idiot_relpath, sizeof( idiot_relpath ), "../%s/", gamefolder )) >= 4 ) + if( len >= 4 ) { if( !Q_strnicmp( dllname, idiot_relpath, len )) return len; @@ -567,13 +560,12 @@ static int FS_StripIdiotRelativePath( const char *dllname, const char *gamefolde static void FS_ValidateDirectories( const char *path, qboolean *has_base_dir, qboolean *has_game_dir ) { stringlist_t dirs; - int i; stringlistinit( &dirs ); listdirectory( &dirs, path, true ); stringlistsort( &dirs ); - for( i = 0; i < dirs.numstrings; i++ ) + for( int i = 0; i < dirs.numstrings; i++ ) { if( !FS_SysFolderExists( dirs.strings[i] )) continue; @@ -599,7 +591,7 @@ FS_Init qboolean FS_InitStdio( qboolean unused_set_to_true, const char *rootdir, const char *basedir, const char *gamedir, const char *rodir ) { stringlist_t dirs; - int i, rodir_num_games; + int rodir_num_games; char buf[MAX_SYSPATH]; FS_InitMemory(); @@ -663,7 +655,7 @@ qboolean FS_InitStdio( qboolean unused_set_to_true, const char *rootdir, const c listdirectory( &dirs, fs_rodir, true ); stringlistsort( &dirs ); - for( i = 0; i < dirs.numstrings; i++ ) + for( int i = 0; i < dirs.numstrings; i++ ) { Q_snprintf( buf, sizeof( buf ), "%s/%s", fs_rodir, dirs.strings[i] ); if( !FS_SysFolderExists( buf )) @@ -685,7 +677,7 @@ qboolean FS_InitStdio( qboolean unused_set_to_true, const char *rootdir, const c listdirectory( &dirs, "./", true ); stringlistsort( &dirs ); - for( i = 0; i < dirs.numstrings; i++ ) + for( int i = 0; i < dirs.numstrings; i++ ) { int j; @@ -735,10 +727,8 @@ FS_Shutdown */ void FS_ShutdownStdio( void ) { - int i; - // release gamedirs - for( i = 0; i < FI.numgames; i++ ) + for( int i = 0; i < FI.numgames; i++ ) { if( FI.games[i] ) { @@ -761,11 +751,9 @@ debug info */ void FS_Path_f( void ) { - searchpath_t *s; - Con_Printf( "Current search path:\n" ); - for( s = fs_searchpaths; s; s = s->next ) + for( searchpath_t *s = fs_searchpaths; s; s = s->next ) { string fl; string info; @@ -797,7 +785,7 @@ and the file index in the package if relevant */ searchpath_t *FS_FindFile( const char *name, int *index, char *fixedname, size_t len, uint32_t flags ) { - searchpath_t *search; + searchpath_t *search; // search through the path, one element at a time for( search = fs_searchpaths; search; search = search->next ) @@ -873,7 +861,7 @@ search for library, assume index is valid qboolean FS_FindLibrary( const char *dllname, qboolean directpath, fs_dllinfo_t *dllInfo ) { string fixedname; - searchpath_t *search; + searchpath_t *search; int index, start = 0, len; // check for bad exports @@ -970,9 +958,7 @@ Converts full path to the relative path considering current searchpaths */ qboolean FS_FullPathToRelativePath( char *dst, const char *src, size_t size ) { - searchpath_t *sp; - - for( sp = fs_searchpaths; sp; sp = sp->next ) + for( searchpath_t *sp = fs_searchpaths; sp; sp = sp->next ) { size_t splen = Q_strlen( sp->filename ); @@ -997,8 +983,7 @@ Allocate and fill a search structure with information on matching filenames. search_t *FS_Search( const char *pattern, int caseinsensitive, int gamedironly ) { search_t *search = NULL; - searchpath_t *searchpath; - int i, numfiles, numchars; + int numfiles, numchars; stringlist_t resultlist; if( pattern[0] == '.' || pattern[0] == ':' || pattern[0] == '/' || pattern[0] == '\\' ) @@ -1007,7 +992,7 @@ search_t *FS_Search( const char *pattern, int caseinsensitive, int gamedironly ) stringlistinit( &resultlist ); // search through the path, one element at a time - for( searchpath = fs_searchpaths; searchpath; searchpath = searchpath->next ) + for( searchpath_t *searchpath = fs_searchpaths; searchpath; searchpath = searchpath->next ) { if( gamedironly && !FBitSet( searchpath->flags, FS_GAMEDIRONLY_SEARCH_FLAGS )) continue; @@ -1021,7 +1006,7 @@ search_t *FS_Search( const char *pattern, int caseinsensitive, int gamedironly ) numfiles = resultlist.numstrings; numchars = 0; - for( i = 0; i < resultlist.numstrings; i++ ) + for( int i = 0; i < resultlist.numstrings; i++ ) numchars += (int)Q_strlen( resultlist.strings[i]) + 1; search = Mem_Calloc( fs_mempool, sizeof(search_t) + numchars + numfiles * sizeof( char* )); search->filenames = (char **)((char *)search + sizeof( search_t )); @@ -1029,9 +1014,9 @@ search_t *FS_Search( const char *pattern, int caseinsensitive, int gamedironly ) search->numfilenames = (int)numfiles; numfiles = numchars = 0; - for( i = 0; i < resultlist.numstrings; i++ ) + for( int i = 0; i < resultlist.numstrings; i++ ) { - size_t textlen; + size_t textlen; search->filenames[numfiles] = search->filenamesbuffer + numchars; textlen = Q_strlen(resultlist.strings[i]) + 1; @@ -1048,12 +1033,10 @@ search_t *FS_Search( const char *pattern, int caseinsensitive, int gamedironly ) qboolean FS_IsArchiveExtensionSupported( const char *ext, uint flags ) { - int i; - if( ext == NULL ) return false; - for( i = 0; i < sizeof( g_archives ) / sizeof( g_archives[0] ); i++ ) + for( int i = 0; i < sizeof( g_archives ) / sizeof( g_archives[0] ); i++ ) { if( FBitSet( flags, IAES_ONLY_REAL_ARCHIVES ) && !g_archives[i].real_archive ) continue; diff --git a/filesystem/sys.c b/filesystem/sys.c index ded04623..510bf66f 100644 --- a/filesystem/sys.c +++ b/filesystem/sys.c @@ -84,9 +84,7 @@ void stringlistinit( stringlist_t *list ) void stringlistfreecontents( stringlist_t *list ) { - int i; - - for( i = 0; i < list->numstrings; i++ ) + for( int i = 0; i < list->numstrings; i++ ) { if( list->strings[i] ) Mem_Free( list->strings[i] ); @@ -103,7 +101,7 @@ void stringlistfreecontents( stringlist_t *list ) void stringlistappend( stringlist_t *list, const char *text ) { - size_t textlen; + size_t textlen; if( !Q_strcmp( text, "." ) || !Q_strcmp( text, ".." )) return; // ignore the virtual directories @@ -122,17 +120,14 @@ void stringlistappend( stringlist_t *list, const char *text ) void stringlistsort( stringlist_t *list ) { - char *temp; - int i, j; - // this is a selection sort (finds the best entry for each slot) - for( i = 0; i < list->numstrings - 1; i++ ) + for( int i = 0; i < list->numstrings - 1; i++ ) { - for( j = i + 1; j < list->numstrings; j++ ) + for( int j = i + 1; j < list->numstrings; j++ ) { if( Q_strcmp( list->strings[i], list->strings[j] ) > 0 ) { - temp = list->strings[i]; + char *temp = list->strings[i]; list->strings[i] = list->strings[j]; list->strings[j] = temp; } @@ -143,12 +138,9 @@ void stringlistsort( stringlist_t *list ) // convert names to lowercase because dos doesn't care, but pattern matching code often does MAYBE_UNUSED static void listlowercase( stringlist_t *list ) { - char *c; - int i; - - for( i = 0; i < list->numstrings; i++ ) + for( int i = 0; i < list->numstrings; i++ ) { - for( c = list->strings[i]; *c; c++ ) + for( char *c = list->strings[i]; *c; c++ ) *c = Q_tolower( *c ); } } @@ -231,14 +223,12 @@ Only used for FS_Open. */ void FS_CreatePath( char *path ) { - char *ofs, save; - - for( ofs = path + 1; *ofs; ofs++ ) + for( char *ofs = path + 1; *ofs; ofs++ ) { if( *ofs == '/' || *ofs == '\\' ) { // create the directory - save = *ofs; + char save = *ofs; *ofs = 0; #if XASH_WIN32 _mkdir( path ); // use _wmkdir maybe? diff --git a/filesystem/wad.c b/filesystem/wad.c index cefd960a..fc806463 100644 --- a/filesystem/wad.c +++ b/filesystem/wad.c @@ -95,13 +95,12 @@ Extracts file type from extension static signed char W_TypeFromExt( const char *lumpname ) { const char *ext = COM_FileExtension( lumpname ); - int i; // we not known about filetype, so match only by filename if( !Q_strcmp( ext, "*" ) || COM_StringEmpty( ext )) return TYP_ANY; - for( i = 0; i < sizeof( wad_types ) / sizeof( wad_types[0] ); i++ ) + for( int i = 0; i < sizeof( wad_types ) / sizeof( wad_types[0] ); i++ ) { if( !Q_stricmp( ext, wad_types[i].ext )) return wad_types[i].type; @@ -119,13 +118,11 @@ Convert type to extension */ static const char *W_ExtFromType( signed char lumptype ) { - int i; - // we not known aboyt filetype, so match only by filename if( lumptype == TYP_NONE || lumptype == TYP_ANY ) return ""; - for( i = 0; i < sizeof( wad_types ) / sizeof( wad_types[0] ); i++ ) + for( int i = 0; i < sizeof( wad_types ) / sizeof( wad_types[0] ); i++ ) { if( lumptype == wad_types[i].type ) return wad_types[i].ext; @@ -143,7 +140,7 @@ Serach for already existed lump */ static dlumpinfo_t *W_FindLump( wfile_t *wad, const char *name, const signed char matchtype ) { - int left, right; + int left, right; if( !wad || !wad->lumps || matchtype == TYP_NONE ) return NULL; @@ -154,8 +151,8 @@ static dlumpinfo_t *W_FindLump( wfile_t *wad, const char *name, const signed cha while( left <= right ) { - int middle = (left + right) / 2; - int diff = Q_stricmp( wad->lumps[middle].name, name ); + int middle = (left + right) / 2; + int diff = Q_stricmp( wad->lumps[middle].name, name ); if( !diff ) { @@ -186,17 +183,16 @@ and sort LAT in alpha-bethical order */ static dlumpinfo_t *W_AddFileToWad( const char *wadfile, const char *name, wfile_t *wad, dlumpinfo_t *newlump ) { - int left, right; - dlumpinfo_t *plump; + dlumpinfo_t *plump; // look for the slot we should put that file into (binary search) - left = 0; - right = wad->numlumps - 1; + int left = 0; + int right = wad->numlumps - 1; while( left <= right ) { - int middle = ( left + right ) / 2; - int diff = Q_stricmp( wad->lumps[middle].name, name ); + int middle = ( left + right ) / 2; + int diff = Q_stricmp( wad->lumps[middle].name, name ); if( !diff ) { @@ -267,11 +263,11 @@ open the wad for reading & writing */ static wfile_t *W_Open( const char *filename, int *error, uint flags ) { - wfile_t *wad = (wfile_t *)Mem_Calloc( fs_mempool, sizeof( wfile_t )); - int i, lumpcount; - dlumpinfo_t *srclumps; - size_t lat_size; - dwadinfo_t header; + wfile_t *wad = (wfile_t *)Mem_Calloc( fs_mempool, sizeof( wfile_t )); + int lumpcount; + dlumpinfo_t *srclumps; + size_t lat_size; + dwadinfo_t header; if( FBitSet( flags, FS_LOAD_PACKED_WAD )) { @@ -355,7 +351,7 @@ static wfile_t *W_Open( const char *filename, int *error, uint flags ) return NULL; } - for( i = 0; i < lumpcount; i++ ) + for( int i = 0; i < lumpcount; i++ ) { srclumps[i].filepos = LittleLong( srclumps[i].filepos ); srclumps[i].disksize = LittleLong( srclumps[i].disksize ); @@ -367,10 +363,10 @@ static wfile_t *W_Open( const char *filename, int *error, uint flags ) wad->numlumps = 0; // sort lumps for binary search - for( i = 0; i < lumpcount; i++ ) + for( int i = 0; i < lumpcount; i++ ) { - char name[16]; - int k; + char name[16]; + int k; // cleanup lumpname Q_strnlwr( srclumps[i].name, name, sizeof( srclumps[i].name )); @@ -425,11 +421,11 @@ FS_FindFile_WAD */ static int FS_FindFile_WAD( searchpath_t *search, const char *path, char *fixedname, size_t len ) { - dlumpinfo_t *lump; - signed char type = W_TypeFromExt( path ); - qboolean anywadname = true; - string wadname; - string shortname; + dlumpinfo_t *lump; + signed char type = W_TypeFromExt( path ); + qboolean anywadname = true; + string wadname; + string shortname; // quick reject by filetype if( type == TYP_NONE ) @@ -478,12 +474,10 @@ FS_Search_WAD */ static void FS_Search_WAD( searchpath_t *search, stringlist_t *list, const char *pattern, int caseinsensitive ) { - string wadpattern, wadname, temp2; - signed char type = W_TypeFromExt( pattern ); - qboolean anywadname = true; - string wadfolder, temp; - int j, i; - const char *slash, *backslash, *colon, *separator; + string wadpattern, wadname, temp2; + signed char type = W_TypeFromExt( pattern ); + qboolean anywadname = true; + string wadfolder, temp; char buf[MAX_VA_STRING]; // quick reject by filetype @@ -513,7 +507,7 @@ static void FS_Search_WAD( searchpath_t *search, stringlist_t *list, const char if( !anywadname && Q_stricmp( wadname, temp2 )) return; - for( i = 0; i < search->wad->numlumps; i++ ) + for( int i = 0; i < search->wad->numlumps; i++ ) { // if type not matching, we already have no chance ... if( type != TYP_ANY && search->wad->lumps[i].type != type ) @@ -524,8 +518,12 @@ static void FS_Search_WAD( searchpath_t *search, stringlist_t *list, const char while( temp[0] ) { + const char *slash, *backslash, *colon, *separator; + if( matchpattern( temp, wadpattern, true )) { + int j; + for( j = 0; j < list->numstrings; j++ ) { if( !Q_strcmp( list->strings[j], temp )) @@ -571,8 +569,8 @@ static byte *W_ReadLump( searchpath_t *search, const char *path, int pack_ind, f { const wfile_t *wad = search->wad; const dlumpinfo_t *lump = &wad->lumps[pack_ind]; - size_t oldpos, size = 0; - byte *buf; + size_t oldpos, size; + byte *buf; // assume error if( lumpsizeptr ) *lumpsizeptr = 0; @@ -620,10 +618,8 @@ FS_AddWad_Fullpath searchpath_t *FS_AddWad_Fullpath( const char *wadfile, int flags ) { searchpath_t *search; - wfile_t *wad; int errorcode = WAD_LOAD_COULDNT_OPEN; - - wad = W_Open( wadfile, &errorcode, flags ); + wfile_t *wad = W_Open( wadfile, &errorcode, flags ); if( !wad ) { diff --git a/filesystem/zip.c b/filesystem/zip.c index d37f6ac1..0a0d0973 100644 --- a/filesystem/zip.c +++ b/filesystem/zip.c @@ -169,15 +169,15 @@ FS_LoadZip */ static zip_t *FS_LoadZip( const char *zipfile, int *error ) { - int numpackfiles = 0, i; - zip_cdf_header_t header_cdf; + int numpackfiles = 0; + zip_cdf_header_t header_cdf; zip_header_eocd_t header_eocd; - uint32_t signature; - fs_offset_t filepos = 0; - zipfile_t *info = NULL; - char filename_buffer[MAX_SYSPATH]; - zip_t *zip = (zip_t *)Mem_Calloc( fs_mempool, sizeof( *zip )); - fs_size_t c; + uint32_t signature; + fs_offset_t filepos; + zipfile_t *info = NULL; + char filename_buffer[MAX_SYSPATH]; + zip_t *zip = (zip_t *)Mem_Calloc( fs_mempool, sizeof( *zip )); + fs_size_t c; // TODO: use FS_Open to allow PK3 to be included into other archives // Currently, it doesn't work with rodir due to FS_FindFile logic @@ -295,7 +295,7 @@ static zip_t *FS_LoadZip( const char *zipfile, int *error ) zip = (zip_t *)Mem_Realloc( fs_mempool, zip, sizeof( *zip ) + sizeof( *info ) * header_eocd.total_central_directory_record ); info = zip->files; - for( i = 0; i < header_eocd.total_central_directory_record; i++ ) + for( int i = 0; i < header_eocd.total_central_directory_record; i++ ) { c = FS_Read( zip->handle, &header_cdf, sizeof( header_cdf )); @@ -373,7 +373,7 @@ static zip_t *FS_LoadZip( const char *zipfile, int *error ) } // recalculate offsets - for( i = 0; i < numpackfiles; i++ ) + for( int i = 0; i < numpackfiles; i++ ) { zip_header_t header; @@ -472,19 +472,15 @@ FS_LoadZIPFile */ static byte *FS_LoadZIPFile( searchpath_t *search, const char *path, int pack_ind, fs_offset_t *sizeptr, void *( *pfnAlloc )( size_t ), void ( *pfnFree )( void * )) { - zipfile_t *file; - byte *compressed_buffer = NULL, *decompressed_buffer = NULL; - int zlib_result = 0; - z_stream decompress_stream; - size_t c; + zipfile_t *file = &search->zip->files[pack_ind]; + byte *compressed_buffer = NULL, *decompressed_buffer; + size_t c; #ifdef ENABLE_CRC_CHECK - dword test_crc, final_crc; + dword test_crc, final_crc; #endif // ENABLE_CRC_CHECK if( sizeptr ) *sizeptr = 0; - file = &search->zip->files[pack_ind]; - if( FS_Seek( search->zip->handle, file->offset, SEEK_SET ) == -1 ) return NULL; @@ -534,6 +530,9 @@ static byte *FS_LoadZIPFile( searchpath_t *search, const char *path, int pack_in } else if( file->flags == ZIP_COMPRESSION_DEFLATED ) { + z_stream decompress_stream; + int zlib_result; + compressed_buffer = (byte *)Mem_Malloc( fs_mempool, file->compressed_size + 1 ); c = FS_Read( search->zip->handle, compressed_buffer, file->compressed_size ); @@ -636,17 +635,14 @@ FS_FindFile_ZIP */ static int FS_FindFile_ZIP( searchpath_t *search, const char *path, char *fixedname, size_t len ) { - int left, right, middle; - // look for the file (binary search) - left = 0; - right = search->zip->numfiles - 1; + int left = 0; + int right = search->zip->numfiles - 1; + while( left <= right ) { - int diff; - - middle = (left + right) / 2; - diff = Q_stricmp( search->zip->files[middle].name, path ); + int middle = (left + right) / 2; + int diff = Q_stricmp( search->zip->files[middle].name, path ); // Found it if( !diff ) @@ -674,16 +670,18 @@ FS_Search_ZIP static void FS_Search_ZIP( searchpath_t *search, stringlist_t *list, const char *pattern, int caseinsensitive ) { string temp; - const char *slash, *backslash, *colon, *separator; - int j, i; - for( i = 0; i < search->zip->numfiles; i++ ) + for( int i = 0; i < search->zip->numfiles; i++ ) { Q_strncpy( temp, search->zip->files[i].name, sizeof( temp )); while( temp[0] ) { + const char *slash, *backslash, *colon, *separator; + if( matchpattern( temp, pattern, true )) { + int j; + for( j = 0; j < list->numstrings; j++ ) { if( !Q_strcmp( list->strings[j], temp )) @@ -720,10 +718,8 @@ FS_AddZip_Fullpath searchpath_t *FS_AddZip_Fullpath( const char *zipfile, int flags ) { searchpath_t *search; - zip_t *zip; int errorcode = ZIP_LOAD_COULDNT_OPEN; - - zip = FS_LoadZip( zipfile, &errorcode ); + zip_t *zip = FS_LoadZip( zipfile, &errorcode ); if( !zip ) {