From 177ed2c603097c3b74cdf9345866ecd177f3ebf6 Mon Sep 17 00:00:00 2001 From: Andrey Akhmichin <15944199+nekonomicon@users.noreply.github.com> Date: Sun, 4 Dec 2022 20:08:28 +0500 Subject: [PATCH] engine: common: simplify strings operations. --- engine/common/con_utils.c | 2 +- engine/common/crashhandler.c | 4 ++-- engine/common/host.c | 2 +- engine/common/net_ws.c | 30 ++++++++++++++++++++---------- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/engine/common/con_utils.c b/engine/common/con_utils.c index aad0b289..34f13460 100644 --- a/engine/common/con_utils.c +++ b/engine/common/con_utils.c @@ -368,7 +368,7 @@ qboolean Cmd_GetSavesList( const char *s, char *completedname, int length ) string matchbuf; int i, numsaves; - t = FS_Search( va( "%s%s*.sav", DEFAULT_SAVE_DIRECTORY, s ), true, true ); // lookup only in gamedir + t = FS_Search( va( DEFAULT_SAVE_DIRECTORY "%s*.sav", s ), true, true ); // lookup only in gamedir if( !t ) return false; COM_FileBase( t->filenames[0], matchbuf ); diff --git a/engine/common/crashhandler.c b/engine/common/crashhandler.c index fac7474a..3c638b47 100644 --- a/engine/common/crashhandler.c +++ b/engine/common/crashhandler.c @@ -358,8 +358,8 @@ static void Sys_Crash( int signal, siginfo_t *si, void *context) #endif // safe actions first, stack and memory may be corrupted - len = Q_snprintf( message, sizeof( message ), "Ver: %s %s (build %i-%s, %s-%s)\n", - XASH_ENGINE_NAME, XASH_VERSION, Q_buildnum(), Q_buildcommit(), Q_buildos(), Q_buildarch() ); + len = Q_snprintf( message, sizeof( message ), "Ver: " XASH_ENGINE_NAME " " XASH_VERSION " (build %i-%s, %s-%s)\n", + Q_buildnum(), Q_buildcommit(), Q_buildos(), Q_buildarch() ); #if !XASH_BSD len += Q_snprintf( message + len, sizeof( message ) - len, "Crash: signal %d errno %d with code %d at %p %p\n", signal, si->si_errno, si->si_code, si->si_addr, si->si_ptr ); diff --git a/engine/common/host.c b/engine/common/host.c index ddba3bd0..7a8ff51a 100644 --- a/engine/common/host.c +++ b/engine/common/host.c @@ -1137,7 +1137,7 @@ int EXPORT Host_Main( int argc, char **argv, const char *progname, int bChangeGa build = Cvar_Get( "buildnum", va( "%i", Q_buildnum_compat()), FCVAR_READ_ONLY, "returns a current build number" ); ver = Cvar_Get( "ver", va( "%i/%s (hw build %i)", PROTOCOL_VERSION, XASH_COMPAT_VERSION, Q_buildnum_compat()), FCVAR_READ_ONLY, "shows an engine version" ); - Cvar_Get( "host_ver", va( "%i %s %s %s %s", Q_buildnum(), XASH_VERSION, Q_buildos(), Q_buildarch(), Q_buildcommit() ), FCVAR_READ_ONLY, "detailed info about this build" ); + Cvar_Get( "host_ver", va( "%i " XASH_VERSION " %s %s %s", Q_buildnum(), Q_buildos(), Q_buildarch(), Q_buildcommit() ), FCVAR_READ_ONLY, "detailed info about this build" ); Cvar_Get( "host_lowmemorymode", va( "%i", XASH_LOW_MEMORY ), FCVAR_READ_ONLY, "indicates if engine compiled for low RAM consumption (0 - normal, 1 - low engine limits, 2 - low protocol limits)" ); Mod_Init(); diff --git a/engine/common/net_ws.c b/engine/common/net_ws.c index 7579251b..3e5cc4a7 100644 --- a/engine/common/net_ws.c +++ b/engine/common/net_ws.c @@ -700,20 +700,23 @@ NET_AdrToString */ const char *NET_AdrToString( const netadr_t a ) { + static char s[64]; + if( a.type == NA_LOOPBACK ) return "loopback"; if( a.type6 == NA_IP6 ) { - char s[64]; uint8_t ip6[16]; NET_NetadrToIP6Bytes( ip6, &a ); IPv6AddrToString( s, ip6, ntohs( a.port ), 0 ); - return va( "%s", s ); + return s; } - return va( "%i.%i.%i.%i:%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3], ntohs( a.port )); + Q_sprintf( s, "%i.%i.%i.%i:%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3], ntohs( a.port )); + + return s; } /* @@ -723,19 +726,23 @@ NET_BaseAdrToString */ const char *NET_BaseAdrToString( const netadr_t a ) { + static char s[64]; + if( a.type == NA_LOOPBACK ) return "loopback"; if( a.type6 == NA_IP6 ) { - char s[64]; uint8_t ip6[16]; NET_NetadrToIP6Bytes( ip6, &a ); IPv6IPToString( s, ip6 ); - return va( "%s", s ); + return s; } - return va( "%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3] ); + + Q_sprintf( s, "%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3] ); + + return s; } /* @@ -1868,6 +1875,7 @@ void NET_GetLocalAddress( void ) char buff[512]; struct sockaddr_storage address; WSAsize_t namelen; + const char *net_addr_string; memset( &net_local, 0, sizeof( netadr_t )); memset( &net6_local, 0, sizeof( netadr_t )); @@ -1895,8 +1903,9 @@ void NET_GetLocalAddress( void ) if( !NET_IsSocketError( getsockname( net.ip_sockets[NS_SERVER], (struct sockaddr *)&address, &namelen ))) { net_local.port = ((struct sockaddr_in *)&address)->sin_port; - Con_Printf( "Server IPv4 address %s\n", NET_AdrToString( net_local )); - Cvar_FullSet( "net_address", va( "%s", NET_AdrToString( net_local )), net_address->flags ); + net_addr_string = NET_AdrToString( net_local ); + Con_Printf( "Server IPv4 address %s\n", net_addr_string ); + Cvar_FullSet( "net_address", net_addr_string, net_address->flags ); } else Con_DPrintf( S_ERROR "Could not get TCP/IPv4 address. Reason: %s\n", NET_ErrorString( )); } @@ -1917,8 +1926,9 @@ void NET_GetLocalAddress( void ) if( !NET_IsSocketError( getsockname( net.ip6_sockets[NS_SERVER], (struct sockaddr *)&address, &namelen ))) { net6_local.port = ((struct sockaddr_in6 *)&address)->sin6_port; - Con_Printf( "Server IPv6 address %s\n", NET_AdrToString( net6_local )); - Cvar_FullSet( "net6_address", va( "%s", NET_AdrToString( net6_local )), net6_address->flags ); + net_addr_string = NET_AdrToString( net6_local ); + Con_Printf( "Server IPv6 address %s\n", net_addr_string ); + Cvar_FullSet( "net6_address", net_addr_string, net6_address->flags ); } else Con_DPrintf( S_ERROR "Could not get TCP/IPv6 address. Reason: %s\n", NET_ErrorString( )); }