2003-07-20 13:41:17 +00:00
|
|
|
/*
|
2005-01-29 07:21:44 +00:00
|
|
|
* "$Id$"
|
2003-07-20 13:41:17 +00:00
|
|
|
*
|
2004-05-02 16:04:40 +00:00
|
|
|
* String functions for Mini-XML, a small XML-like file parsing library.
|
2003-07-20 13:41:17 +00:00
|
|
|
*
|
2007-04-18 13:14:10 +00:00
|
|
|
* Copyright 2003-2007 by Michael Sweet.
|
2003-07-20 13:41:17 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* Contents:
|
|
|
|
*
|
2007-04-18 13:14:10 +00:00
|
|
|
* _mxml_snprintf() - Format a string.
|
2005-10-13 18:27:00 +00:00
|
|
|
* _mxml_strdup() - Duplicate a string.
|
|
|
|
* _mxml_strdupf() - Format and duplicate a string.
|
|
|
|
* _mxml_vsnprintf() - Format a string into a fixed size buffer.
|
2007-04-18 13:14:10 +00:00
|
|
|
* _mxml_vstrdupf() - Format and duplicate a string.
|
2003-07-20 13:41:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Include necessary headers...
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
2007-04-18 13:14:10 +00:00
|
|
|
#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 */
|
|
|
|
|
|
|
|
|
2003-07-20 13:41:17 +00:00
|
|
|
/*
|
2005-10-13 18:27:00 +00:00
|
|
|
* '_mxml_strdup()' - Duplicate a string.
|
2003-07-20 13:41:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HAVE_STRDUP
|
2003-09-28 12:44:39 +00:00
|
|
|
char * /* O - New string pointer */
|
2005-10-13 18:27:00 +00:00
|
|
|
_mxml_strdup(const char *s) /* I - String to duplicate */
|
2003-07-20 13:41:17 +00:00
|
|
|
{
|
2003-09-28 12:44:39 +00:00
|
|
|
char *t; /* New string pointer */
|
2003-07-20 13:41:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if ((t = malloc(strlen(s) + 1)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
return (strcpy(t, s));
|
|
|
|
}
|
|
|
|
#endif /* !HAVE_STRDUP */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-10-13 18:27:00 +00:00
|
|
|
* '_mxml_strdupf()' - Format and duplicate a string.
|
2003-09-28 12:44:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
char * /* O - New string pointer */
|
2005-10-13 18:27:00 +00:00
|
|
|
_mxml_strdupf(const char *format, /* I - Printf-style format string */
|
|
|
|
...) /* I - Additional arguments as needed */
|
2005-08-05 14:17:19 +00:00
|
|
|
{
|
|
|
|
va_list ap; /* Pointer to additional arguments */
|
|
|
|
char *s; /* Pointer to formatted string */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a pointer to the additional arguments, format the string,
|
|
|
|
* and return it...
|
|
|
|
*/
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2005-10-13 18:27:00 +00:00
|
|
|
s = _mxml_vstrdupf(format, ap);
|
2005-08-05 14:17:19 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-28 12:44:39 +00:00
|
|
|
#ifndef HAVE_VSNPRINTF
|
|
|
|
/*
|
2005-10-13 18:27:00 +00:00
|
|
|
* '_mxml_vsnprintf()' - Format a string into a fixed size buffer.
|
2003-09-28 12:44:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int /* O - Number of bytes formatted */
|
2005-10-13 18:27:00 +00:00
|
|
|
_mxml_vsnprintf(char *buffer, /* O - Output buffer */
|
|
|
|
size_t bufsize, /* O - Size of output buffer */
|
|
|
|
const char *format, /* I - Printf-style format string */
|
|
|
|
va_list ap) /* I - Pointer to additional arguments */
|
2003-09-28 12:44:39 +00:00
|
|
|
{
|
|
|
|
char *bufptr, /* Pointer to position in buffer */
|
|
|
|
*bufend, /* Pointer to end of buffer */
|
|
|
|
sign, /* Sign of format width */
|
|
|
|
size, /* Size character (h, l, L) */
|
|
|
|
type; /* Format type character */
|
|
|
|
int width, /* Width of field */
|
|
|
|
prec; /* Number of characters of precision */
|
|
|
|
char tformat[100], /* Temporary format string for sprintf() */
|
2005-08-11 14:28:49 +00:00
|
|
|
*tptr, /* Pointer into temporary format */
|
2003-09-28 12:44:39 +00:00
|
|
|
temp[1024]; /* Buffer for formatted numbers */
|
|
|
|
char *s; /* Pointer to string */
|
|
|
|
int slen; /* Length of string */
|
|
|
|
int bytes; /* Total number of bytes needed */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through the format string, formatting as needed...
|
|
|
|
*/
|
|
|
|
|
|
|
|
bufptr = buffer;
|
|
|
|
bufend = buffer + bufsize - 1;
|
|
|
|
bytes = 0;
|
|
|
|
|
|
|
|
while (*format)
|
|
|
|
{
|
|
|
|
if (*format == '%')
|
|
|
|
{
|
2005-08-11 14:28:49 +00:00
|
|
|
tptr = tformat;
|
|
|
|
*tptr++ = *format++;
|
2003-09-28 12:44:39 +00:00
|
|
|
|
|
|
|
if (*format == '%')
|
|
|
|
{
|
2005-08-11 14:28:49 +00:00
|
|
|
if (bufptr && bufptr < bufend) *bufptr++ = *format;
|
|
|
|
bytes ++;
|
|
|
|
format ++;
|
2003-09-28 12:44:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (strchr(" -+#\'", *format))
|
2005-08-11 14:28:49 +00:00
|
|
|
{
|
|
|
|
*tptr++ = *format;
|
2003-09-28 12:44:39 +00:00
|
|
|
sign = *format++;
|
2005-08-11 14:28:49 +00:00
|
|
|
}
|
2003-09-28 12:44:39 +00:00
|
|
|
else
|
|
|
|
sign = 0;
|
|
|
|
|
2005-08-11 14:28:49 +00:00
|
|
|
if (*format == '*')
|
|
|
|
{
|
2005-10-13 18:27:00 +00:00
|
|
|
/*
|
|
|
|
* Get width from argument...
|
|
|
|
*/
|
|
|
|
|
2005-08-11 14:28:49 +00:00
|
|
|
format ++;
|
|
|
|
width = va_arg(ap, int);
|
|
|
|
|
|
|
|
snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", width);
|
|
|
|
tptr += strlen(tptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = 0;
|
|
|
|
|
|
|
|
while (isdigit(*format & 255))
|
|
|
|
{
|
|
|
|
if (tptr < (tformat + sizeof(tformat) - 1))
|
|
|
|
*tptr++ = *format;
|
|
|
|
|
|
|
|
width = width * 10 + *format++ - '0';
|
|
|
|
}
|
|
|
|
}
|
2003-09-28 12:44:39 +00:00
|
|
|
|
|
|
|
if (*format == '.')
|
|
|
|
{
|
2005-08-11 14:28:49 +00:00
|
|
|
if (tptr < (tformat + sizeof(tformat) - 1))
|
|
|
|
*tptr++ = *format;
|
|
|
|
|
2003-09-28 12:44:39 +00:00
|
|
|
format ++;
|
|
|
|
|
2005-08-11 14:28:49 +00:00
|
|
|
if (*format == '*')
|
|
|
|
{
|
2005-10-13 18:27:00 +00:00
|
|
|
/*
|
|
|
|
* Get precision from argument...
|
|
|
|
*/
|
|
|
|
|
2005-08-11 14:28:49 +00:00
|
|
|
format ++;
|
|
|
|
prec = va_arg(ap, int);
|
|
|
|
|
|
|
|
snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", prec);
|
|
|
|
tptr += strlen(tptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prec = 0;
|
|
|
|
|
|
|
|
while (isdigit(*format & 255))
|
|
|
|
{
|
|
|
|
if (tptr < (tformat + sizeof(tformat) - 1))
|
|
|
|
*tptr++ = *format;
|
|
|
|
|
|
|
|
prec = prec * 10 + *format++ - '0';
|
|
|
|
}
|
|
|
|
}
|
2003-09-28 12:44:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
prec = -1;
|
|
|
|
|
|
|
|
if (*format == 'l' && format[1] == 'l')
|
|
|
|
{
|
|
|
|
size = 'L';
|
2005-08-11 14:28:49 +00:00
|
|
|
|
|
|
|
if (tptr < (tformat + sizeof(tformat) - 2))
|
|
|
|
{
|
|
|
|
*tptr++ = 'l';
|
|
|
|
*tptr++ = 'l';
|
|
|
|
}
|
|
|
|
|
2003-09-28 12:44:39 +00:00
|
|
|
format += 2;
|
|
|
|
}
|
|
|
|
else if (*format == 'h' || *format == 'l' || *format == 'L')
|
2005-08-11 14:28:49 +00:00
|
|
|
{
|
|
|
|
if (tptr < (tformat + sizeof(tformat) - 1))
|
|
|
|
*tptr++ = *format;
|
|
|
|
|
2003-09-28 12:44:39 +00:00
|
|
|
size = *format++;
|
2005-08-11 14:28:49 +00:00
|
|
|
}
|
2003-09-28 12:44:39 +00:00
|
|
|
|
|
|
|
if (!*format)
|
|
|
|
break;
|
|
|
|
|
2005-08-11 14:28:49 +00:00
|
|
|
if (tptr < (tformat + sizeof(tformat) - 1))
|
|
|
|
*tptr++ = *format;
|
|
|
|
|
|
|
|
type = *format++;
|
|
|
|
*tptr = '\0';
|
2003-09-28 12:44:39 +00:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case 'E' : /* Floating point formats */
|
|
|
|
case 'G' :
|
|
|
|
case 'e' :
|
|
|
|
case 'f' :
|
|
|
|
case 'g' :
|
2005-08-11 14:28:49 +00:00
|
|
|
if ((width + 2) > sizeof(temp))
|
2003-09-28 12:44:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
sprintf(temp, tformat, va_arg(ap, double));
|
|
|
|
|
|
|
|
bytes += strlen(temp);
|
|
|
|
|
|
|
|
if (bufptr)
|
|
|
|
{
|
|
|
|
if ((bufptr + strlen(temp)) > bufend)
|
|
|
|
{
|
2005-08-11 14:28:49 +00:00
|
|
|
strncpy(bufptr, temp, (size_t)(bufend - bufptr));
|
2003-09-28 12:44:39 +00:00
|
|
|
bufptr = bufend;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(bufptr, temp);
|
|
|
|
bufptr += strlen(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B' : /* Integer formats */
|
|
|
|
case 'X' :
|
|
|
|
case 'b' :
|
|
|
|
case 'd' :
|
|
|
|
case 'i' :
|
|
|
|
case 'o' :
|
|
|
|
case 'u' :
|
|
|
|
case 'x' :
|
2005-08-11 14:28:49 +00:00
|
|
|
if ((width + 2) > sizeof(temp))
|
2003-09-28 12:44:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
sprintf(temp, tformat, va_arg(ap, int));
|
|
|
|
|
|
|
|
bytes += strlen(temp);
|
|
|
|
|
|
|
|
if (bufptr)
|
|
|
|
{
|
|
|
|
if ((bufptr + strlen(temp)) > bufend)
|
|
|
|
{
|
2005-08-11 14:28:49 +00:00
|
|
|
strncpy(bufptr, temp, (size_t)(bufend - bufptr));
|
2003-09-28 12:44:39 +00:00
|
|
|
bufptr = bufend;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(bufptr, temp);
|
|
|
|
bufptr += strlen(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-08-11 14:28:49 +00:00
|
|
|
|
2003-09-28 12:44:39 +00:00
|
|
|
case 'p' : /* Pointer value */
|
2005-08-11 14:28:49 +00:00
|
|
|
if ((width + 2) > sizeof(temp))
|
2003-09-28 12:44:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
sprintf(temp, tformat, va_arg(ap, void *));
|
|
|
|
|
|
|
|
bytes += strlen(temp);
|
|
|
|
|
|
|
|
if (bufptr)
|
|
|
|
{
|
|
|
|
if ((bufptr + strlen(temp)) > bufend)
|
|
|
|
{
|
2005-08-11 14:28:49 +00:00
|
|
|
strncpy(bufptr, temp, (size_t)(bufend - bufptr));
|
2003-09-28 12:44:39 +00:00
|
|
|
bufptr = bufend;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(bufptr, temp);
|
|
|
|
bufptr += strlen(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c' : /* Character or character array */
|
|
|
|
bytes += width;
|
|
|
|
|
|
|
|
if (bufptr)
|
|
|
|
{
|
|
|
|
if (width <= 1)
|
2005-08-11 14:28:49 +00:00
|
|
|
*bufptr++ = va_arg(ap, int);
|
2003-09-28 12:44:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((bufptr + width) > bufend)
|
2005-08-11 14:28:49 +00:00
|
|
|
width = bufend - bufptr;
|
2003-09-28 12:44:39 +00:00
|
|
|
|
2005-08-11 14:28:49 +00:00
|
|
|
memcpy(bufptr, va_arg(ap, char *), (size_t)width);
|
2003-09-28 12:44:39 +00:00
|
|
|
bufptr += width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's' : /* String */
|
|
|
|
if ((s = va_arg(ap, char *)) == NULL)
|
|
|
|
s = "(null)";
|
|
|
|
|
|
|
|
slen = strlen(s);
|
|
|
|
if (slen > width && prec != width)
|
|
|
|
width = slen;
|
|
|
|
|
|
|
|
bytes += width;
|
|
|
|
|
|
|
|
if (bufptr)
|
|
|
|
{
|
|
|
|
if ((bufptr + width) > bufend)
|
2005-08-11 14:28:49 +00:00
|
|
|
width = bufend - bufptr;
|
2003-09-28 12:44:39 +00:00
|
|
|
|
|
|
|
if (slen > width)
|
2005-08-11 14:28:49 +00:00
|
|
|
slen = width;
|
2003-09-28 12:44:39 +00:00
|
|
|
|
|
|
|
if (sign == '-')
|
|
|
|
{
|
2005-08-11 14:28:49 +00:00
|
|
|
strncpy(bufptr, s, (size_t)slen);
|
|
|
|
memset(bufptr + slen, ' ', (size_t)(width - slen));
|
2003-09-28 12:44:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-08-11 14:28:49 +00:00
|
|
|
memset(bufptr, ' ', (size_t)(width - slen));
|
|
|
|
strncpy(bufptr + width - slen, s, (size_t)slen);
|
2003-09-28 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bufptr += width;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n' : /* Output number of chars so far */
|
2005-08-11 14:28:49 +00:00
|
|
|
*(va_arg(ap, int *)) = bytes;
|
2003-09-28 12:44:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes ++;
|
|
|
|
|
|
|
|
if (bufptr && bufptr < bufend)
|
2005-08-11 14:28:49 +00:00
|
|
|
*bufptr++ = *format;
|
2005-08-10 18:51:31 +00:00
|
|
|
|
|
|
|
format ++;
|
2003-09-28 12:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nul-terminate the string and return the number of characters needed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*bufptr = '\0';
|
|
|
|
|
|
|
|
return (bytes);
|
|
|
|
}
|
|
|
|
#endif /* !HAVE_VSNPRINTF */
|
|
|
|
|
|
|
|
|
2007-04-18 13:14:10 +00:00
|
|
|
/*
|
|
|
|
* '_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-28 12:44:39 +00:00
|
|
|
/*
|
2005-01-29 07:21:44 +00:00
|
|
|
* End of "$Id$".
|
2003-07-20 13:41:17 +00:00
|
|
|
*/
|