Repository: lucy-clownfish Updated Branches: refs/heads/master faefec2b5 -> 20eef8fc3
Implement CFCUtil_vsprintf Drop support for Microsoft compilers without _vscprintf. This function is supported at least since Visual Studio .NET 2003 (MSVC 7.1). I think it was only MSVC 6 that didn't support it. Also use va_copy when iterating va_lists twice. Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/65387c4f Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/65387c4f Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/65387c4f Branch: refs/heads/master Commit: 65387c4fe5f0088fe89c90a9d0a1e8829fabb0f3 Parents: 05d3caa Author: Nick Wellnhofer <[email protected]> Authored: Fri Mar 4 17:54:20 2016 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Fri Mar 4 17:56:55 2016 +0100 ---------------------------------------------------------------------- compiler/src/CFCUtil.c | 46 ++++++++++++++++++--------------------------- compiler/src/CFCUtil.h | 4 ++++ 2 files changed, 22 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/65387c4f/compiler/src/CFCUtil.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUtil.c b/compiler/src/CFCUtil.c index 6fe62fa..da546af 100644 --- a/compiler/src/CFCUtil.c +++ b/compiler/src/CFCUtil.c @@ -59,51 +59,41 @@ CFCUtil_strndup(const char *string, size_t len) { return copy; } -#if defined(CHY_HAS_C99_SNPRINTF) || defined(CHY_HAS__SCPRINTF) - char* CFCUtil_sprintf(const char *fmt, ...) { va_list args; va_start(args, fmt); + char *string = CFCUtil_vsprintf(fmt, args); + va_end(args); + + return string; +} + +#if !defined(CHY_HAS_C99_SNPRINTF) && !defined(CHY_HAS__SCPRINTF) + #error "snprintf or replacement not available." +#endif + +char* +CFCUtil_vsprintf(const char *fmt, va_list args) { + va_list args_copy; + + va_copy(args_copy, args); #if defined(CHY_HAS_C99_SNPRINTF) - int size = vsnprintf(NULL, 0, fmt, args); + int size = vsnprintf(NULL, 0, fmt, args_copy); if (size < 0) { CFCUtil_die("snprintf failed"); } #else - int size = _vscprintf(fmt, args); + int size = _vscprintf(fmt, args_copy); if (size < 0) { CFCUtil_die("_scprintf failed"); } #endif - va_end(args); + va_end(args_copy); char *string = (char*)MALLOCATE((size_t)size + 1); - va_start(args, fmt); vsprintf(string, fmt, args); - va_end(args); return string; } -#elif defined(CHY_HAS__SNPRINTF) - -char* -CFCUtil_sprintf(const char *fmt, ...) { - for (size_t size = 32; size * 2 > size; size *= 2) { - char *string = (char*)MALLOCATE(size); - va_list args; - va_start(args, fmt); - int result = _vsnprintf(string, size, fmt, args); - va_end(args); - if (result >= 0 && (size_t)result < size) { return string; } - FREEMEM(string); - } - CFCUtil_die("_snprintf failed"); - return NULL; -} - -#else - #error "snprintf or replacement not available." -#endif - char* CFCUtil_cat(char *string, ...) { va_list args; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/65387c4f/compiler/src/CFCUtil.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUtil.h b/compiler/src/CFCUtil.h index 15f45a9..3e06b03 100644 --- a/compiler/src/CFCUtil.h +++ b/compiler/src/CFCUtil.h @@ -27,6 +27,7 @@ extern "C" { #endif +#include <stdarg.h> #include <stddef.h> /** Create an inner Perl object with a refcount of 1. For use in actual @@ -58,6 +59,9 @@ CFCUtil_strndup(const char *string, size_t len); char* CFCUtil_sprintf(const char *fmt, ...); +char* +CFCUtil_vsprintf(const char *fmt, va_list args); + /** Concatenate a NULL-terminated list of strings onto the first, reallocating * with each argument. */
