Add CFCUtil_sprintf utility function Works like sprintf but returns a dynamically allocated string similar to asprintf.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/0751cbe2 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/0751cbe2 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/0751cbe2 Branch: refs/heads/cfc-sprintf Commit: 0751cbe25f9362a3887afdf9a6424ad9cb24d9dc Parents: 9436cce Author: Nick Wellnhofer <[email protected]> Authored: Wed Dec 26 19:55:52 2012 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Wed Dec 26 19:55:52 2012 +0100 ---------------------------------------------------------------------- clownfish/compiler/src/CFCUtil.c | 21 +++++++++++++++++++++ clownfish/compiler/src/CFCUtil.h | 6 ++++++ 2 files changed, 27 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/0751cbe2/clownfish/compiler/src/CFCUtil.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCUtil.c b/clownfish/compiler/src/CFCUtil.c index f76c9a8..56fb287 100644 --- a/clownfish/compiler/src/CFCUtil.c +++ b/clownfish/compiler/src/CFCUtil.c @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "charmony.h" + #include <string.h> #include <stdlib.h> #include <ctype.h> @@ -53,6 +55,25 @@ CFCUtil_strndup(const char *string, size_t len) { } char* +CFCUtil_sprintf(const char *fmt, ...) { + va_list args; + va_start(args, fmt); +#if defined(CHY_HAS_C99_SNPRINTF) + size_t size = vsnprintf(NULL, 0, fmt, args); +#elif defined(CHY_HAS__SCPRINTF) + size_t size = _vscprintf(fmt, args); +#else + #error "snprintf or replacement not available." +#endif + va_end(args); + char *string = (char*)MALLOCATE(size + 1); + va_start(args, fmt); + vsprintf(string, fmt, args); + va_end(args); + return string; +} + +char* CFCUtil_cat(char *string, ...) { va_list args; char *appended; http://git-wip-us.apache.org/repos/asf/lucy/blob/0751cbe2/clownfish/compiler/src/CFCUtil.h ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCUtil.h b/clownfish/compiler/src/CFCUtil.h index 0d714c5..df78500 100644 --- a/clownfish/compiler/src/CFCUtil.h +++ b/clownfish/compiler/src/CFCUtil.h @@ -52,6 +52,12 @@ CFCUtil_strdup(const char *string); char* CFCUtil_strndup(const char *string, size_t len); +/** Return a dynamically allocated string with content defined by a printf + * format string and additional arguments. Similar to asprintf(). + */ +char* +CFCUtil_sprintf(const char *fmt, ...); + /** Concatenate a NULL-terminated list of strings onto the first, reallocating * with each argument. */
