* Add a new statically-sized string macro vssprintf, which allows printing to statically sized strings just like ssprintf, with as difference that this macro function operates similar to vsprintf rather than sprintf

* Replace uses of strncmp, snprintf and vsnprintf with sstrcmp, ssprintf and vssprintf respectively


git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5579 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2008-07-18 22:47:28 +00:00
parent 36a4191818
commit 9d77329842
2 changed files with 13 additions and 10 deletions

View File

@ -322,10 +322,10 @@ void _realObjTrace(int id, const char *function, const char *str, ...)
va_list ap;
va_start(ap, str);
vsnprintf(vaBuffer, MAX_LEN_LOG_LINE, str, ap);
vssprintf(vaBuffer, str, ap);
va_end(ap);
snprintf(outputBuffer, MAX_LEN_LOG_LINE, "[%6d]: [%s] %s", id, function, vaBuffer);
ssprintf(outputBuffer, "[%6d]: [%s] %s", id, function, vaBuffer);
while (curCallback)
{
curCallback->callback(&curCallback->data, outputBuffer);
@ -343,19 +343,20 @@ void _debug( code_part part, const char *function, const char *str, ... )
static unsigned int prev = 0; /* total on last update */
va_start(ap, str);
vsnprintf(outputBuffer, MAX_LEN_LOG_LINE, str, ap);
vssprintf(outputBuffer, str, ap);
va_end(ap);
snprintf(inputBuffer[useInputBuffer1 ? 1 : 0], MAX_LEN_LOG_LINE, "[%s] %s", function, outputBuffer);
ssprintf(inputBuffer[useInputBuffer1 ? 1 : 0], "[%s] %s", function, outputBuffer);
if ( strncmp( inputBuffer[0], inputBuffer[1], MAX_LEN_LOG_LINE - 1 ) == 0 ) {
if (sstrcmp(inputBuffer[0], inputBuffer[1]) == 0)
{
// Received again the same line
repeated++;
if (repeated == next) {
if (repeated > 2) {
snprintf( outputBuffer, sizeof(outputBuffer), "last message repeated %u times (total %u repeats)", repeated - prev, repeated );
ssprintf(outputBuffer, "last message repeated %u times (total %u repeats)", repeated - prev, repeated);
} else {
snprintf( outputBuffer, sizeof(outputBuffer), "last message repeated %u times", repeated - prev );
ssprintf(outputBuffer, "last message repeated %u times", repeated - prev);
}
while (curCallback) {
curCallback->callback( &curCallback->data, outputBuffer );
@ -370,9 +371,9 @@ void _debug( code_part part, const char *function, const char *str, ... )
if (repeated > 0 && repeated != prev && repeated != 1) {
/* just repeat the previous message when only one repeat occurred */
if (repeated > 2) {
snprintf( outputBuffer, sizeof(outputBuffer), "last message repeated %u times (total %u repeats)", repeated - prev, repeated );
ssprintf(outputBuffer, "last message repeated %u times (total %u repeats)", repeated - prev, repeated);
} else {
snprintf( outputBuffer, sizeof(outputBuffer), "last message repeated %u times", repeated - prev );
ssprintf(outputBuffer, "last message repeated %u times", repeated - prev);
}
while (curCallback)
{
@ -389,7 +390,7 @@ void _debug( code_part part, const char *function, const char *str, ... )
if (!repeated)
{
// Assemble the outputBuffer:
snprintf( outputBuffer, MAX_LEN_LOG_LINE, "%-8s|%012u: %s", code_part_names[part], gameTime, useInputBuffer1 ? inputBuffer[1] : inputBuffer[0] );
ssprintf(outputBuffer, "%-8s|%012u: %s", code_part_names[part], gameTime, useInputBuffer1 ? inputBuffer[1] : inputBuffer[0]);
while (curCallback) {
curCallback->callback( &curCallback->data, outputBuffer );

View File

@ -112,11 +112,13 @@ static inline size_t strlcat(char *WZ_DECL_RESTRICT dst, const char *WZ_DECL_RES
#define sstrcpy(dest, src) strlcpy((dest), (src), sizeof(dest))
#define sstrcat(dest, src) strlcat((dest), (src), sizeof(dest))
#define ssprintf(dest, ...) snprintf((dest), sizeof(dest), __VA_ARGS__)
#define vssprintf(dest, format, ap) vsnprintf((dest), sizeof(dest), format, ap)
#define sstrcmp(str1, str2) strncmp((str1), (str2), sizeof(str1) > sizeof(str2) ? sizeof(str2) : sizeof(str1))
#else
#define sstrcpy(dest, src) (WZ_ASSERT_STATIC_STRING(dest), strlcpy((dest), (src), sizeof(dest)))
#define sstrcat(dest, src) (WZ_ASSERT_STATIC_STRING(dest), strlcat((dest), (src), sizeof(dest)))
#define ssprintf(dest, ...) (WZ_ASSERT_STATIC_STRING(dest), snprintf((dest), sizeof(dest), __VA_ARGS__))
#define vssprintf(dest, format, ap) (WZ_ASSERT_STATIC_STRING(dest), vsnprintf((dest), sizeof(dest), format, ap))
#define sstrcmp(str1, str2) (WZ_ASSERT_STATIC_STRING(str1), WZ_ASSERT_STATIC_STRING(str2), strncmp((str1), (str2), sizeof(str1) > sizeof(str2) ? sizeof(str2) : sizeof(str1)))
#endif