* 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
master
Giel van Schijndel 2007-11-11 22:01:05 +00:00
parent 1b9b69048c
commit be25c4901b
2 changed files with 20 additions and 52 deletions

View File

@ -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__

View File

@ -132,11 +132,6 @@ const char* version_getFormattedVersionString()
if (versionString == NULL)
{
// TRANSLATORS: This string looks as follows when expanded.
// "Version <version name/number> <working copy state><BUILD DATE><BUILD TYPE>"
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());
sasprintf((char**)&build_date, _(" - Built %s"), version_getBuildDate());
}
else
{
build_date = strdup("");
}
}
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 <version name/number> <working copy state><BUILD DATE><BUILD TYPE>"
asprintf((char**)&versionString, _("Version %s%s%s%s"), version_getVersionString(), wc_state, build_date, build_type);
}
return versionString;