From 9fb36b4a2f42a18919e1a68383b1fb4b6e597530 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Wed, 18 Apr 2007 13:14:10 +0000 Subject: [PATCH] Added snprintf() emulation function for test program (STR #32) --- CHANGES | 4 +- config.h.in | 10 +++- configure | 3 +- configure.in | 2 +- doc/relnotes.html | 3 ++ mxml-string.c | 119 ++++++++++++++++++++++++++++------------------ vcnet/config.h | 8 +++- 7 files changed, 96 insertions(+), 53 deletions(-) diff --git a/CHANGES b/CHANGES index d591f80..b758c1d 100644 --- a/CHANGES +++ b/CHANGES @@ -1,10 +1,12 @@ -CHANGES - 2007-04-17 +CHANGES - 2007-04-18 -------------------- CHANGES IN Mini-XML 2.3 - Added two exceptions to the LGPL to support static linking of applications against Mini-XML. + - Added snprintf() emulation function for test program (STR + #32) - Added the _CRT_SECURE_NO_DEPRECATE definition when building on VC++ 2005 (STR #36) - mxmlLoad*() did not detect missing > characters in diff --git a/config.h.in b/config.h.in index ec7d0ef..d1a060d 100644 --- a/config.h.in +++ b/config.h.in @@ -3,7 +3,7 @@ * * Configuration file for Mini-XML, a small XML-like file parsing library. * - * Copyright 2003-2005 by Michael Sweet. + * Copyright 2003-2007 by Michael Sweet. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -35,9 +35,10 @@ /* - * Do we have the vsnprintf() function? + * Do we have the snprintf() and vsnprintf() functions? */ +#undef HAVE_SNPRINTF #undef HAVE_VSNPRINTF @@ -60,6 +61,11 @@ extern char *_mxml_strdup(const char *); extern char *_mxml_strdupf(const char *, ...); extern char *_mxml_vstrdupf(const char *, va_list); +# ifndef HAVE_SNPRINTF +extern int _mxml_snprintf(char *, size_t, const char *, ...); +# define snprintf _mxml_snprintf +# endif /* !HAVE_SNPRINTF */ + # ifndef HAVE_VSNPRINTF extern int _mxml_vsnprintf(char *, size_t, const char *, va_list); # define vsnprintf _mxml_vsnprintf diff --git a/configure b/configure index ca2e482..6248a4e 100755 --- a/configure +++ b/configure @@ -3199,7 +3199,8 @@ fi if test "x$use_vsnprintf" != xyes; then -for ac_func in vsnprintf + +for ac_func in snprintf vsnprintf do as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` echo "$as_me:$LINENO: checking for $ac_func" >&5 diff --git a/configure.in b/configure.in index 205f231..83c4f48 100644 --- a/configure.in +++ b/configure.in @@ -104,7 +104,7 @@ if test "x$use_ansi" != xyes; then fi if test "x$use_vsnprintf" != xyes; then - AC_CHECK_FUNCS(vsnprintf) + AC_CHECK_FUNCS(snprintf vsnprintf) fi dnl Shared library support... diff --git a/doc/relnotes.html b/doc/relnotes.html index ef40621..d0f646f 100644 --- a/doc/relnotes.html +++ b/doc/relnotes.html @@ -10,6 +10,9 @@
  • Added two exceptions to the LGPL to support static linking of applications against Mini-XML.
  • +
  • Added snprintf() emulation function for test program + (STR #32)
  • +
  • Added the _CRT_SECURE_NO_DEPRECATE definition when building on VC++ 2005 (STR #36)
  • diff --git a/mxml-string.c b/mxml-string.c index 7420427..f43e40e 100644 --- a/mxml-string.c +++ b/mxml-string.c @@ -3,7 +3,7 @@ * * String functions for Mini-XML, a small XML-like file parsing library. * - * Copyright 2003-2005 by Michael Sweet. + * Copyright 2003-2007 by Michael Sweet. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -17,10 +17,11 @@ * * Contents: * + * _mxml_snprintf() - Format a string. * _mxml_strdup() - Duplicate a string. * _mxml_strdupf() - Format and duplicate a string. - * _mxml_vstrdupf() - Format and duplicate a string. * _mxml_vsnprintf() - Format a string into a fixed size buffer. + * _mxml_vstrdupf() - Format and duplicate a string. */ /* @@ -30,6 +31,30 @@ #include "config.h" +#ifdef HAVE_SNPRINTF +/* + * '_mxml_snprintf()' - Format a string. + */ + +int /* O - Number of bytes formatted */ +_mxml_snprintf(char *buffer, /* I - Output buffer */ + size_t bufsize, /* I - Size of output buffer */ + const char *format, /* I - Printf-style format string */ + ...) /* I - Additional arguments as needed */ +{ + va_list ap; /* Argument list */ + int bytes; /* Number of bytes formatted */ + + + va_start(ap, format); + bytes = vsnprintf(buffer, bufsize, format, ap); + va_end(ap); + + return (bytes); +} +#endif /* HAVE_SNPRINTF */ + + /* * '_mxml_strdup()' - Duplicate a string. */ @@ -77,51 +102,6 @@ _mxml_strdupf(const char *format, /* I - Printf-style format string */ } -/* - * '_mxml_vstrdupf()' - Format and duplicate a string. - */ - -char * /* O - New string pointer */ -_mxml_vstrdupf(const char *format, /* I - Printf-style format string */ - va_list ap) /* I - Pointer to additional arguments */ -{ - int bytes; /* Number of bytes required */ - char *buffer, /* String buffer */ - temp[256]; /* Small buffer for first vsnprintf */ - - - /* - * First format with a tiny buffer; this will tell us how many bytes are - * needed... - */ - - bytes = vsnprintf(temp, sizeof(temp), format, ap); - - if (bytes < sizeof(temp)) - { - /* - * Hey, the formatted string fits in the tiny buffer, so just dup that... - */ - - return (strdup(temp)); - } - - /* - * Allocate memory for the whole thing and reformat to the new, larger - * buffer... - */ - - if ((buffer = calloc(1, bytes + 1)) != NULL) - vsnprintf(buffer, bytes + 1, format, ap); - - /* - * Return the new string... - */ - - return (buffer); -} - - #ifndef HAVE_VSNPRINTF /* * '_mxml_vsnprintf()' - Format a string into a fixed size buffer. @@ -427,6 +407,51 @@ _mxml_vsnprintf(char *buffer, /* O - Output buffer */ #endif /* !HAVE_VSNPRINTF */ +/* + * '_mxml_vstrdupf()' - Format and duplicate a string. + */ + +char * /* O - New string pointer */ +_mxml_vstrdupf(const char *format, /* I - Printf-style format string */ + va_list ap) /* I - Pointer to additional arguments */ +{ + int bytes; /* Number of bytes required */ + char *buffer, /* String buffer */ + temp[256]; /* Small buffer for first vsnprintf */ + + + /* + * First format with a tiny buffer; this will tell us how many bytes are + * needed... + */ + + bytes = vsnprintf(temp, sizeof(temp), format, ap); + + if (bytes < sizeof(temp)) + { + /* + * Hey, the formatted string fits in the tiny buffer, so just dup that... + */ + + return (strdup(temp)); + } + + /* + * Allocate memory for the whole thing and reformat to the new, larger + * buffer... + */ + + if ((buffer = calloc(1, bytes + 1)) != NULL) + vsnprintf(buffer, bytes + 1, format, ap); + + /* + * Return the new string... + */ + + return (buffer); +} + + /* * End of "$Id$". */ diff --git a/vcnet/config.h b/vcnet/config.h index 18a0c08..a2524ec 100644 --- a/vcnet/config.h +++ b/vcnet/config.h @@ -46,9 +46,10 @@ /* - * Do we have the vsnprintf() function? + * Do we have the snprintf() and vsnprintf() functions? */ +/*#undef HAVE_SNPRINTF */ /*#undef HAVE_VSNPRINTF */ @@ -70,6 +71,11 @@ extern char *mxml_strdup(const char *); extern char *mxml_strdupf(const char *, va_list); +# ifndef HAVE_SNPRINTF +extern int _mxml_snprintf(char *, size_t, const char *, ...); +# define snprintf _mxml_snprintf +# endif /* !HAVE_SNPRINTF */ + # ifndef HAVE_VSNPRINTF extern int mxml_vsnprintf(char *, size_t, const char *, va_list); # define vsnprintf mxml_vsnprintf