From 5aef09ce5dc912cadf546a74205fbd8d9a4a65d3 Mon Sep 17 00:00:00 2001 From: Andrey Akhmichin Date: Sun, 29 Nov 2020 04:05:42 +0500 Subject: [PATCH] engine: client: simplified strings operations. --- engine/client/cl_cmds.c | 4 ++-- engine/client/cl_custom.c | 2 +- engine/client/cl_demo.c | 4 ++-- engine/client/cl_gameui.c | 2 +- engine/client/cl_main.c | 4 ++-- engine/client/cl_parse.c | 11 ++++------- engine/client/cl_tent.c | 18 +++++++++--------- engine/client/console.c | 8 ++++---- engine/client/in_touch.c | 8 ++++---- engine/client/keys.c | 22 +++++++++++----------- engine/client/ref_common.c | 2 +- engine/client/s_load.c | 2 +- engine/client/s_main.c | 4 ++-- engine/client/s_vox.c | 9 ++++++--- engine/client/titles.c | 2 +- 15 files changed, 51 insertions(+), 51 deletions(-) diff --git a/engine/client/cl_cmds.c b/engine/client/cl_cmds.c index a3157a9b..e8b43e5c 100644 --- a/engine/client/cl_cmds.c +++ b/engine/client/cl_cmds.c @@ -126,7 +126,7 @@ void CL_PlayCDTrack_f( void ) int i, maxTrack; for( maxTrack = i = 0; i < MAX_CDTRACKS; i++ ) - if( Q_strlen( clgame.cdtracks[i] )) maxTrack++; + if( COM_CheckStringEmpty( clgame.cdtracks[i] ) ) maxTrack++; Con_Printf( "%u tracks\n", maxTrack ); if( track ) @@ -360,7 +360,7 @@ void CL_SaveShot_f( void ) return; } - Q_sprintf( cls.shotname, "%s%s.bmp", DEFAULT_SAVE_DIRECTORY, Cmd_Argv( 1 )); + Q_sprintf( cls.shotname, DEFAULT_SAVE_DIRECTORY "%s.bmp", Cmd_Argv( 1 )); cls.scrshot_action = scrshot_savegame; // build new frame for saveshot } diff --git a/engine/client/cl_custom.c b/engine/client/cl_custom.c index d743fc60..ee96e91e 100644 --- a/engine/client/cl_custom.c +++ b/engine/client/cl_custom.c @@ -39,7 +39,7 @@ qboolean CL_CheckFile( sizebuf_t *msg, resource_t *pResource ) } if( pResource->type == t_sound ) - Q_strncpy( filepath, va( "%s%s", DEFAULT_SOUNDPATH, pResource->szFileName ), sizeof( filepath )); + Q_snprintf( filepath, sizeof( filepath ), DEFAULT_SOUNDPATH "%s", pResource->szFileName ); else Q_strncpy( filepath, pResource->szFileName, sizeof( filepath )); if( !COM_IsSafeFileToDownload( filepath )) diff --git a/engine/client/cl_demo.c b/engine/client/cl_demo.c index 5985d0ed..84dc9a5b 100644 --- a/engine/client/cl_demo.c +++ b/engine/client/cl_demo.c @@ -1185,7 +1185,7 @@ int GAME_EXPORT CL_GetDemoComment( const char *demoname, char *comment ) demfile = FS_Open( demoname, "rb", false ); if( !demfile ) { - Q_strncpy( comment, "", MAX_STRING ); + comment[0] = '\0'; return false; } @@ -1226,7 +1226,7 @@ int GAME_EXPORT CL_GetDemoComment( const char *demoname, char *comment ) // split comment to sections Q_strncpy( comment, demohdr.mapname, CS_SIZE ); Q_strncpy( comment + CS_SIZE, demohdr.comment, CS_SIZE ); - Q_strncpy( comment + CS_SIZE * 2, va( "%g sec", playtime ), CS_TIME ); + Q_snprintf( comment + CS_SIZE * 2, CS_TIME, "%g sec", playtime ); // all done FS_Close( demfile ); diff --git a/engine/client/cl_gameui.c b/engine/client/cl_gameui.c index b5b70325..b806ce26 100644 --- a/engine/client/cl_gameui.c +++ b/engine/client/cl_gameui.c @@ -403,7 +403,7 @@ static void UI_ConvertGameInfo( GAMEINFO *out, gameinfo_t *in ) Q_strncpy( out->startmap, in->startmap, sizeof( out->startmap )); Q_strncpy( out->trainmap, in->trainmap, sizeof( out->trainmap )); Q_strncpy( out->title, in->title, sizeof( out->title )); - Q_strncpy( out->version, va( "%g", in->version ), sizeof( out->version )); + Q_snprintf( out->version, sizeof( out->version ), "%g", in->version ); Q_strncpy( out->game_url, in->game_url, sizeof( out->game_url )); Q_strncpy( out->update_url, in->update_url, sizeof( out->update_url )); diff --git a/engine/client/cl_main.c b/engine/client/cl_main.c index 490316a5..8c654ea7 100644 --- a/engine/client/cl_main.c +++ b/engine/client/cl_main.c @@ -2397,7 +2397,7 @@ A file has been received via the fragmentation/reassembly layer, put it in the r */ void CL_ProcessFile( qboolean successfully_received, const char *filename ) { - int sound_len = Q_strlen( DEFAULT_SOUNDPATH ); + int sound_len = sizeof( DEFAULT_SOUNDPATH ) - 1; byte rgucMD5_hash[16]; const char *pfilename; resource_t *p; @@ -2666,7 +2666,7 @@ qboolean CL_PrecacheResources( void ) { if( FBitSet( pRes->ucFlags, RES_WASMISSING )) { - Con_Printf( S_ERROR "Could not load sound %s%s\n", DEFAULT_SOUNDPATH, pRes->szFileName ); + Con_Printf( S_ERROR "Could not load sound " DEFAULT_SOUNDPATH "%s\n", pRes->szFileName ); cl.sound_precache[pRes->nIndex][0] = 0; cl.sound_index[pRes->nIndex] = 0; } diff --git a/engine/client/cl_parse.c b/engine/client/cl_parse.c index 367d29a5..dce0d2a7 100644 --- a/engine/client/cl_parse.c +++ b/engine/client/cl_parse.c @@ -501,11 +501,8 @@ void CL_BatchResourceRequest( qboolean initialize ) { if( !FBitSet( p->ucFlags, RES_REQUESTED )) { - string filename; - - Q_snprintf( filename, sizeof( filename ), "!MD5%s", MD5_Print( p->rgucMD5_hash )); MSG_BeginClientCmd( &msg, clc_stringcmd ); - MSG_WriteString( &msg, va( "dlfile %s", filename )); + MSG_WriteString( &msg, va( "dlfile !MD5%s", MD5_Print( p->rgucMD5_hash ) ) ); SetBits( p->ucFlags, RES_REQUESTED ); } break; @@ -560,7 +557,7 @@ int CL_EstimateNeededResources( void ) switch( p->type ) { case t_sound: - if( p->szFileName[0] != '*' && !FS_FileExists( va( "%s%s", DEFAULT_SOUNDPATH, p->szFileName ), false ) ) + if( p->szFileName[0] != '*' && !FS_FileExists( va( DEFAULT_SOUNDPATH "%s", p->szFileName ), false ) ) { SetBits( p->ucFlags, RES_WASMISSING ); nTotalSize += p->nDownloadSize; @@ -1496,7 +1493,7 @@ void CL_SendConsistencyInfo( sizebuf_t *msg ) MSG_WriteUBitLong( msg, pc->orig_index, MAX_MODEL_BITS ); if( pc->issound ) - Q_snprintf( filename, sizeof( filename ), "%s%s", DEFAULT_SOUNDPATH, pc->filename ); + Q_snprintf( filename, sizeof( filename ), DEFAULT_SOUNDPATH "%s", pc->filename ); else Q_strncpy( filename, pc->filename, sizeof( filename )); if( Q_strstr( filename, "models/" )) @@ -2805,7 +2802,7 @@ void CL_LegacyParseResourceList( sizebuf_t *msg ) const char *path; if( reslist.restype[i] == t_sound ) - path = va( "sound/%s", reslist.resnames[i] ); + path = va( DEFAULT_SOUNDPATH "%s", reslist.resnames[i] ); else path = reslist.resnames[i]; if( FS_FileExists( path, false )) diff --git a/engine/client/cl_tent.c b/engine/client/cl_tent.c index 1e3bdc03..4981a545 100644 --- a/engine/client/cl_tent.c +++ b/engine/client/cl_tent.c @@ -163,7 +163,7 @@ void CL_AddClientResources( void ) // then check sounds for( i = 0; i < ARRAYSIZE( cl_player_shell_sounds ); i++ ) { - Q_snprintf( filepath, sizeof( filepath ), "%s%s", DEFAULT_SOUNDPATH, cl_player_shell_sounds[i] ); + Q_snprintf( filepath, sizeof( filepath ), DEFAULT_SOUNDPATH "%s", cl_player_shell_sounds[i] ); if( !FS_FileExists( filepath, false )) CL_AddClientResource( cl_player_shell_sounds[i], t_sound ); @@ -171,7 +171,7 @@ void CL_AddClientResources( void ) for( i = 0; i < ARRAYSIZE( cl_weapon_shell_sounds ); i++ ) { - Q_snprintf( filepath, sizeof( filepath ), "%s%s", DEFAULT_SOUNDPATH, cl_weapon_shell_sounds[i] ); + Q_snprintf( filepath, sizeof( filepath ), DEFAULT_SOUNDPATH "%s", cl_weapon_shell_sounds[i] ); if( !FS_FileExists( filepath, false )) CL_AddClientResource( cl_weapon_shell_sounds[i], t_sound ); @@ -179,7 +179,7 @@ void CL_AddClientResources( void ) for( i = 0; i < ARRAYSIZE( cl_explode_sounds ); i++ ) { - Q_snprintf( filepath, sizeof( filepath ), "%s%s", DEFAULT_SOUNDPATH, cl_explode_sounds[i] ); + Q_snprintf( filepath, sizeof( filepath ), DEFAULT_SOUNDPATH "%s", cl_explode_sounds[i] ); if( !FS_FileExists( filepath, false )) CL_AddClientResource( cl_explode_sounds[i], t_sound ); @@ -188,7 +188,7 @@ void CL_AddClientResources( void ) #if 0 // ric sounds was precached by server-side for( i = 0; i < ARRAYSIZE( cl_ricochet_sounds ); i++ ) { - Q_snprintf( filepath, sizeof( filepath ), "%s%s", DEFAULT_SOUNDPATH, cl_ricochet_sounds[i] ); + Q_snprintf( filepath, sizeof( filepath ), DEFAULT_SOUNDPATH "%s", cl_ricochet_sounds[i] ); if( !FS_FileExists( filepath, false )) CL_AddClientResource( cl_ricochet_sounds[i], t_sound ); @@ -322,15 +322,15 @@ void CL_TempEntPlaySound( TEMPENTITY *pTemp, float damp ) Q_snprintf( soundname, sizeof( soundname ), "debris/wood%i.wav", COM_RandomLong( 1, 4 )); break; case BOUNCE_SHRAP: - Q_snprintf( soundname, sizeof( soundname ), "%s", cl_ricochet_sounds[COM_RandomLong( 0, 4 )] ); + Q_strncpy( soundname, cl_ricochet_sounds[COM_RandomLong( 0, 4 )], sizeof( soundname ) ); break; case BOUNCE_SHOTSHELL: - Q_snprintf( soundname, sizeof( soundname ), "%s", cl_weapon_shell_sounds[COM_RandomLong( 0, 2 )] ); + Q_strncpy( soundname, cl_weapon_shell_sounds[COM_RandomLong( 0, 2 )], sizeof( soundname ) ); isshellcasing = true; // shell casings have different playback parameters fvol = 0.5f; break; case BOUNCE_SHELL: - Q_snprintf( soundname, sizeof( soundname ), "%s", cl_player_shell_sounds[COM_RandomLong( 0, 2 )] ); + Q_strncpy( soundname, cl_player_shell_sounds[COM_RandomLong( 0, 2 )], sizeof( soundname ) ); isshellcasing = true; // shell casings have different playback parameters break; case BOUNCE_CONCRETE: @@ -1522,7 +1522,7 @@ void GAME_EXPORT R_RicochetSound( const vec3_t pos ) char soundpath[32]; sound_t handle; - Q_snprintf( soundpath, sizeof( soundpath ), "%s", cl_ricochet_sounds[COM_RandomLong( 0, 4 )] ); + Q_strncpy( soundpath, cl_ricochet_sounds[COM_RandomLong( 0, 4 )], sizeof( soundpath ) ); handle = S_RegisterSound( soundpath ); S_StartSound( pos, 0, CHAN_AUTO, handle, fvol, ATTN_NORM, iPitch, 0 ); @@ -1672,7 +1672,7 @@ void GAME_EXPORT R_Explosion( vec3_t pos, int model, float scale, float framerat if( !FBitSet( flags, TE_EXPLFLAG_NOSOUND )) { - hSound = S_RegisterSound( va( "%s", cl_explode_sounds[COM_RandomLong( 0, 2 )] )); + hSound = S_RegisterSound( cl_explode_sounds[COM_RandomLong( 0, 2 )] ); S_StartSound( pos, 0, CHAN_STATIC, hSound, VOL_NORM, 0.3f, PITCH_NORM, 0 ); } } diff --git a/engine/client/console.c b/engine/client/console.c index 1d4f3177..d9596d3c 100644 --- a/engine/client/console.c +++ b/engine/client/console.c @@ -2427,13 +2427,13 @@ void Con_VidInit( void ) lmp_t *cb = (lmp_t *)FS_LoadFile( "gfx/conback.lmp", &length, false ); char ver[64]; byte *dest; - int x, y; + int x, y, len; if( cb && cb->width == 320 && cb->height == 200 ) { - Q_snprintf( ver, 64, "%i", Q_buildnum( )); // can store only buildnum - dest = (byte *)(cb + 1) + 320 * 186 + 320 - 11 - 8 * Q_strlen( ver ); - y = Q_strlen( ver ); + len = Q_snprintf( ver, 64, "%i", Q_buildnum( )); // can store only buildnum + dest = (byte *)(cb + 1) + 320 * 186 + 320 - 11 - 8 * len; + y = len; for( x = 0; x < y; x++ ) Con_DrawCharToConback( ver[x], buf, dest + (x << 3)); con.background = ref.dllFuncs.GL_LoadTexture( "#gfx/conback.lmp", (byte *)cb, length, TF_IMAGE ); diff --git a/engine/client/in_touch.c b/engine/client/in_touch.c index c42ea954..2b58cb57 100644 --- a/engine/client/in_touch.c +++ b/engine/client/in_touch.c @@ -447,7 +447,7 @@ touch_button_t *Touch_FindFirst( touchbuttonlist_t *list, const char *name ) touch_button_t *button; for ( button = list->first; button; button = button->next ) - if( ( Q_strstr( name, "*" ) && Q_stricmpext( name, button->name ) ) || !Q_strncmp( name, button->name, 32 ) ) + if( ( Q_strchr( name, '*' ) && Q_stricmpext( name, button->name ) ) || !Q_strncmp( name, button->name, 32 ) ) return button; return NULL; } @@ -534,7 +534,7 @@ void Touch_SetColor( touchbuttonlist_t *list, const char *name, byte *color ) touch_button_t *button; for( button = list->first; button; button = button->next ) { - if( ( Q_strstr( name, "*" ) && Q_stricmpext( name, button->name ) ) || !Q_strncmp( name, button->name, 32 ) ) + if( ( Q_strchr( name, '*' ) && Q_stricmpext( name, button->name ) ) || !Q_strncmp( name, button->name, 32 ) ) MakeRGBA( button->color, color[0], color[1], color[2], color[3] ); } } @@ -570,7 +570,7 @@ void Touch_HideButtons( const char *name, byte hide ) for( button = touch.list_user.first; button; button = button->next) { - if( ( Q_strstr( name, "*" ) && Q_stricmpext( name, button->name ) ) || !Q_strncmp( name, button->name, 32 ) ) + if( ( Q_strchr( name, '*' ) && Q_stricmpext( name, button->name ) ) || !Q_strncmp( name, button->name, 32 ) ) { if( hide ) button->flags |= TOUCH_FL_HIDE; @@ -602,7 +602,7 @@ void Touch_FadeButtons( touchbuttonlist_t *list, const char *name, float speed, touch_button_t *button; for( button = list->first; button; button = button->next) { - if( ( Q_strstr( name, "*" ) && Q_stricmpext( name, button->name ) ) || !Q_strncmp( name, button->name, 32 ) ) + if( ( Q_strchr( name, '*' ) && Q_stricmpext( name, button->name ) ) || !Q_strncmp( name, button->name, 32 ) ) { if( start >= 0 ) button->fade = start; diff --git a/engine/client/keys.c b/engine/client/keys.c index 9172b97f..044cddda 100644 --- a/engine/client/keys.c +++ b/engine/client/keys.c @@ -313,25 +313,25 @@ Key_GetKey */ int Key_GetKey( const char *pBinding ) { - int i; + int i, len; + const char *p; if( !pBinding ) return -1; + len = Q_strlen( pBinding ); + for( i = 0; i < 256; i++ ) { if( !keys[i].binding ) continue; - if( *keys[i].binding == '+' ) - { - if( !Q_strnicmp( keys[i].binding + 1, pBinding, Q_strlen( pBinding ))) - return i; - } - else - { - if( !Q_strnicmp( keys[i].binding, pBinding, Q_strlen( pBinding ))) - return i; - } + p = keys[i].binding; + + if( *p == '+' ) + p++; + + if( !Q_strnicmp( p, pBinding, len ) ) + return i; } return -1; diff --git a/engine/client/ref_common.c b/engine/client/ref_common.c index 4a63b438..5fa479aa 100644 --- a/engine/client/ref_common.c +++ b/engine/client/ref_common.c @@ -508,7 +508,7 @@ void R_Shutdown( void ) static void R_GetRendererName( char *dest, size_t size, const char *opt ) { - if( !Q_strstr( opt, va( ".%s", OS_LIB_EXT ))) + if( !Q_strstr( opt, "." OS_LIB_EXT )) { const char *format; diff --git a/engine/client/s_load.c b/engine/client/s_load.c index cb4573bf..89348ca6 100644 --- a/engine/client/s_load.c +++ b/engine/client/s_load.c @@ -56,7 +56,7 @@ void S_SoundList_f( void ) else Con_Printf( " " ); if( sfx->name[0] == '*' ) Con_Printf( " (%2db) %s : %s\n", sc->width * 8, Q_memprint( sc->size ), sfx->name ); - else Con_Printf( " (%2db) %s : %s%s\n", sc->width * 8, Q_memprint( sc->size ), DEFAULT_SOUNDPATH, sfx->name ); + else Con_Printf( " (%2db) %s : " DEFAULT_SOUNDPATH "%s\n", sc->width * 8, Q_memprint( sc->size ), sfx->name ); totalSfx++; } } diff --git a/engine/client/s_main.c b/engine/client/s_main.c index 1c365a8a..361924af 100644 --- a/engine/client/s_main.c +++ b/engine/client/s_main.c @@ -525,7 +525,7 @@ void S_StartSound( const vec3_t pos, int ent, int chan, sound_t handle, float fv if( !target_chan ) { if( !bIgnore ) - Con_DPrintf( S_ERROR "dropped sound \"%s%s\"\n", DEFAULT_SOUNDPATH, sfx->name ); + Con_DPrintf( S_ERROR "dropped sound \"" DEFAULT_SOUNDPATH "%s\"\n", sfx->name ); return; } @@ -637,7 +637,7 @@ void S_RestoreSound( const vec3_t pos, int ent, int chan, sound_t handle, float if( !target_chan ) { if( !bIgnore ) - Con_DPrintf( S_ERROR "dropped sound \"%s%s\"\n", DEFAULT_SOUNDPATH, sfx->name ); + Con_DPrintf( S_ERROR "dropped sound \"" DEFAULT_SOUNDPATH "%s\"\n", sfx->name ); return; } diff --git a/engine/client/s_vox.c b/engine/client/s_vox.c index 38047960..327b0f33 100644 --- a/engine/client/s_vox.c +++ b/engine/client/s_vox.c @@ -76,8 +76,11 @@ static char *ScanForwardUntil( char *string, const char scan ) static char *VOX_GetDirectory( char *szpath, char *psz ) { char c; - int cb = 0; - char *p = psz + Q_strlen( psz ) - 1; + int cb = 0, len; + char *p; + + len = Q_strlen( psz ); + p = psz + len - 1; // scan backwards until first '/' or start of string c = *p; @@ -94,7 +97,7 @@ static char *VOX_GetDirectory( char *szpath, char *psz ) return psz; } - cb = Q_strlen( psz ) - cb; + cb = len - cb; memcpy( szpath, psz, cb ); szpath[cb] = 0; diff --git a/engine/client/titles.c b/engine/client/titles.c index a2b4ccbc..5b860c18 100644 --- a/engine/client/titles.c +++ b/engine/client/titles.c @@ -275,7 +275,7 @@ void CL_TextMessageParse( byte *pMemFile, int fileSize ) // Save name/text on heap textMessages[messageCount] = gMessageParms; textMessages[messageCount].pName = nameHeap + lastNamePos; - lastNamePos += Q_strlen( currentName ) + 1; + lastNamePos += length + 1; textMessages[messageCount].pMessage = pCurrentText; messageCount++;