From be25c4901b975cb9ac642cb1eb76f0e226d19432 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Sun, 11 Nov 2007 22:01:05 +0000 Subject: [PATCH] * Modify sasprintf to take this signature instead: "void sasprintf(char**, const char* fmt, ...)" (take in mind though that it is a macro!) * Add some comments to sasprintf * Utilize sasprintf and asprintf in src/version.c to drastically cut back in code size and duplication git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2806 4a71c877-e1ca-e34f-864e-861f7616d084 --- lib/framework/printf_ext.h | 17 +++++++++--- src/version.c | 55 +++++--------------------------------- 2 files changed, 20 insertions(+), 52 deletions(-) diff --git a/lib/framework/printf_ext.h b/lib/framework/printf_ext.h index a6e32e6eb..e16411a93 100644 --- a/lib/framework/printf_ext.h +++ b/lib/framework/printf_ext.h @@ -36,11 +36,20 @@ extern int snprintf(char* str, size_t size, const char* format, ...); #endif // A stack-allocating variant of sprintf -#define sasprintf(var, fmt, ...) \ +#define sasprintf(strp, format, ...) \ do { \ + /* Make sure to evaluate "format" just once */ \ + const char* fmt = format; \ + /* Determine the size of the string we're going to produce */ \ size_t size = snprintf(NULL, 0, fmt, __VA_ARGS__); \ - var = alloca(size + 1); \ - sprintf(var, fmt, __VA_ARGS__); \ -} while(0); + \ + /* Let the compiler perform some static type-checking */ \ + char** var = strp; \ + \ + /* Allocate a buffer large enough to hold our string on the stack*/ \ + *var = (char*)alloca(size + 1); \ + /* Print into our newly created string-buffer */ \ + sprintf(*var, fmt, __VA_ARGS__); \ +} while(0) #endif // __INCLUDE_LIB_FRAMEWORK_PRINTF_EXT_H__ diff --git a/src/version.c b/src/version.c index a672e8b15..488c2218a 100644 --- a/src/version.c +++ b/src/version.c @@ -132,11 +132,6 @@ const char* version_getFormattedVersionString() if (versionString == NULL) { - // TRANSLATORS: This string looks as follows when expanded. - // "Version " - const char* format_string = _("Version %s%s%s%s"); - - // Compose the working copy state string #if (SVN_WC_MODIFIED && SVN_WC_SWITCHED) const char* wc_state = _(" (modified and switched locally)"); @@ -155,57 +150,21 @@ const char* version_getFormattedVersionString() static const char build_type[] = ""; #endif - int str_len; - - char* build_date = NULL; + const char* build_date = NULL; if (strncmp(svn_uri_cstr, "tags/", strlen("tags/")) != 0) { - const char* date_format_string = _(" - Built %s"); - - // Find out how much memory we're going to need for the build-date string - str_len = snprintf(build_date, 0, date_format_string, version_getBuildDate()); - - if (str_len > 0) - { - build_date = malloc(str_len + 1); - if (build_date == NULL) - { - debug(LOG_ERROR, "version_getVersionString: Out of memory!"); - abort(); - return NULL; - } - - snprintf(build_date, str_len + 1, date_format_string, version_getBuildDate()); - } - else - { - build_date = strdup(""); - } + sasprintf((char**)&build_date, _(" - Built %s"), version_getBuildDate()); } else { - build_date = strdup(""); + build_date = ""; } - // Find out how much memory we're going to need for the version string - str_len = snprintf((char*)versionString, 0, format_string, version_getVersionString(), wc_state, build_date, build_type); - - if (str_len > 0) - { - versionString = malloc(str_len + 1); - if (versionString == NULL) - { - free(build_date); - debug(LOG_ERROR, "version_getVersionString: Out of memory!"); - abort(); - return NULL; - } - - snprintf((char*)versionString, str_len + 1, format_string, version_getVersionString(), wc_state, build_date, build_type); - } - - free(build_date); + // Construct the version string + // TRANSLATORS: This string looks as follows when expanded. + // "Version " + asprintf((char**)&versionString, _("Version %s%s%s%s"), version_getVersionString(), wc_state, build_date, build_type); } return versionString;