SprintfCat

On the train to Cardiff yesterday, I was creating a fixed function shader generator for the Athena engine. I kept using strcat() with if statements, and in a lot of places I kept using sprintf() then copying that in with strcat().

So I decided to make SprintfCat(), all the wonderful joy of sprintf, with the ability to concatenate with a already existant string like strcat.

//
//
size_t ath::SprintfCat( char * inoutBuffer, const size_t inBufferSize, const char * inFormat, ... )
{
	const size_t offset = StrLen( inoutBuffer );
	size_t ret = 0;
	va_list arg;
 
	//
	va_start( arg, inFormat );
	{
		ret = VSprintf( inoutBuffer + offset, inBufferSize - offset, inFormat, arg );
	}
	va_end( arg );
 
	//
	return offset + ret;
}

I had already created my own wrappers around strlen() and vsprintf(), the reason being the fault of Microsoft. With their implementation of the standard library, they decided to deprecate non-safe versions complain at you until you either used their own _s variants, or defined a certain preprocessor.

//
//
size_t ath::VSprintf( char * outBuffer, const size_t inBufferSize, const char * inFormat, va_list inArgs )
{
#ifndef ATH_PLATFORM_WINDOWS
	return vsnprintf( outBuffer, inBufferSize, inFormat, inArgs );
#else
	return vsprintf_s( outBuffer, inBufferSize, inFormat, inArgs );
#endif
}
 
//
//
size_t ath::StrLen( const char * inString )
{
	return strlen( inString );
}

While I do prefer to have safe versions, their _s variants are not present in the standard and so it makes the code not portable.

I’m still not quite sure about my naming convention for a variable that is an input and an output. While I do like the in or out prefix, I don’t think inout looks right, being slightly too long. I wasn’t a fan of io either. I have however grown to like the truncation of Athena namespace to ath.