From e28fb42ff4d7bd9cbfd437b47a1ec1065f645e5b Mon Sep 17 00:00:00 2001 From: david Date: Thu, 8 Mar 2012 23:56:07 +0000 Subject: [PATCH] Move alloc_sprintf from xml.cc to nbase. --- nbase/nbase.h | 3 +++ nbase/nbase_str.c | 34 ++++++++++++++++++++++++++++++++++ xml.cc | 36 ------------------------------------ 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/nbase/nbase.h b/nbase/nbase.h index 3c2681abb..c8d0683f0 100644 --- a/nbase/nbase.h +++ b/nbase/nbase.h @@ -395,6 +395,9 @@ int Vsnprintf(char *, size_t, const char *, va_list) int Snprintf(char *, size_t, const char *, ...) __attribute__ ((format (printf, 3, 4))); +char *alloc_vsprintf(const char *fmt, va_list va) + __attribute__ ((format (printf, 1, 0))); + /* Trivial function that returns nonzero if all characters in str of length strlength are printable (as defined by isprint()) */ int stringisprintable(const char *str, int strlength); diff --git a/nbase/nbase_str.c b/nbase/nbase_str.c index 09c2e23ad..b201b5789 100644 --- a/nbase/nbase_str.c +++ b/nbase/nbase_str.c @@ -165,6 +165,40 @@ int Snprintf(char *s, size_t n, const char *fmt, ...) return ret; } +/* vsprintf into a dynamically allocated buffer, similar to asprintf in + Glibc. Return the buffer or NULL on error. */ +char *alloc_vsprintf(const char *fmt, va_list va) { + va_list va_tmp; + char *s, *p; + int size = 32; + int n; + + s = NULL; + size = 32; + for (;;) { + p = (char *) safe_realloc(s, size); + if (p == NULL) + return NULL; + s = p; + +#ifdef WIN32 + va_tmp = va; +#else + va_copy(va_tmp, va); +#endif + n = vsnprintf(s, size, fmt, va_tmp); + + if (n >= size) + size = n + 1; + else if (n < 0) + size = size * 2; + else + break; + } + + return s; +} + /* Trivial function that returns nonzero if all characters in str of length strlength are printable (as defined by isprint()) */ int stringisprintable(const char *str, int strlength) { diff --git a/xml.cc b/xml.cc index 749cc3d32..1c5aca67c 100644 --- a/xml.cc +++ b/xml.cc @@ -170,42 +170,6 @@ struct xml_writer { static struct xml_writer xml; -static char *alloc_vsprintf(const char *fmt, va_list va) __attribute__ ((format (printf, 1, 0))); - -/* vsprintf into a dynamically allocated buffer, similar to asprintf in - Glibc. Return the buffer or NULL on error. */ -static char *alloc_vsprintf(const char *fmt, va_list va) { - va_list va_tmp; - char *s, *p; - int size = 32; - int n; - - s = NULL; - size = 32; - for (;;) { - p = (char *) safe_realloc(s, size); - if (p == NULL) - return NULL; - s = p; - -#ifdef WIN32 - va_tmp = va; -#else - va_copy(va_tmp, va); -#endif - n = vsnprintf(s, size, fmt, va_tmp); - - if (n >= size) - size = n + 1; - else if (n < 0) - size = size * 2; - else - break; - } - - return s; -} - /* Escape a string for inclusion in XML. This gets <>&, "' for attribute values, -- for inside comments, and characters with value > 0x7F. It also gets control characters with value < 0x20 to avoid parser