mxml-string.c 10.1 KB
Newer Older
1
/*
Michael Sweet's avatar
Michael Sweet committed
2
 * String functions for Mini-XML, a small XML file parsing library.
3
 *
Michael Sweet's avatar
Michael Sweet committed
4
 * Copyright 2003-2017 by Michael R Sweet.
5
 *
6 7 8 9 10
 * These coded instructions, statements, and computer programs are the
 * property of Michael R Sweet and are protected by Federal copyright
 * law.  Distribution and use rights are outlined in the file "COPYING"
 * which should have been included with this file.  If this file is
 * missing or damaged, see the license at:
11
 *
Michael Sweet's avatar
Michael Sweet committed
12
 *     https://michaelrsweet.github.io/mxml
13 14 15 16 17 18 19 20 21
 */

/*
 * Include necessary headers...
 */

#include "config.h"


22 23 24 25 26 27 28 29 30
/*
 * The va_copy macro is part of C99, but many compilers don't implement it.
 * Provide a "direct assignment" implmentation when va_copy isn't defined...
 */

#ifndef va_copy
#  ifdef __va_copy
#    define va_copy(dst,src) __va_copy(dst,src)
#  else
31
#    define va_copy(dst,src) memcpy(&dst, src, sizeof(va_list))
32 33 34 35
#  endif /* __va_copy */
#endif /* va_copy */


Michael R Sweet's avatar
Michael R Sweet committed
36
#ifndef HAVE_SNPRINTF
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * '_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);
}
Michael R Sweet's avatar
Michael R Sweet committed
57
#endif /* !HAVE_SNPRINTF */
58 59


60
/*
Michael R Sweet's avatar
Michael R Sweet committed
61
 * '_mxml_strdup()' - Duplicate a string.
62 63 64
 */

#ifndef HAVE_STRDUP
65
char *					/* O - New string pointer */
Michael R Sweet's avatar
Michael R Sweet committed
66
_mxml_strdup(const char *s)		/* I - String to duplicate */
67
{
68
  char	*t;				/* New string pointer */
69 70 71 72 73 74 75 76 77 78 79 80 81 82


  if (s == NULL)
    return (NULL);

  if ((t = malloc(strlen(s) + 1)) == NULL)
    return (NULL);

  return (strcpy(t, s));
}
#endif /* !HAVE_STRDUP */


/*
Michael R Sweet's avatar
Michael R Sweet committed
83
 * '_mxml_strdupf()' - Format and duplicate a string.
84 85 86
 */

char *					/* O - New string pointer */
Michael R Sweet's avatar
Michael R Sweet committed
87 88
_mxml_strdupf(const char *format,	/* I - Printf-style format string */
              ...)			/* I - Additional arguments as needed */
89 90 91 92 93 94 95 96 97 98 99
{
  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);
100 101 102
#ifdef HAVE_VASPRINTF
  vasprintf(&s, format, ap);
#else
Michael R Sweet's avatar
Michael R Sweet committed
103
  s = _mxml_vstrdupf(format, ap);
104
#endif /* HAVE_VASPRINTF */
105 106 107 108 109 110
  va_end(ap);

  return (s);
}


111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#ifndef HAVE_STRLCPY
/*
 * '_mxml_strlcpy()' - Safely copy a string.
 */

size_t					/* O - Number of bytes copied */
_mxml_strlcpy(char       *dst,		/* I - Destination buffer */
              const char *src,		/* I - Source string */
              size_t     dstsize)	/* I - Size of destinatipon buffer */
{
  size_t        srclen;                 /* Length of source string */


 /*
  * Figure out how much room is needed...
  */

  dstsize --;

  srclen = strlen(src);

 /*
  * Copy the appropriate amount...
  */

  if (srclen > dstsize)
    srclen = dstsize;

  memmove(dst, src, srclen);
  dst[srclen] = '\0';

  return (srclen);
}
#endif /* !HAVE_STRLCPY */


147 148
#ifndef HAVE_VSNPRINTF
/*
Michael R Sweet's avatar
Michael R Sweet committed
149
 * '_mxml_vsnprintf()' - Format a string into a fixed size buffer.
150 151 152
 */

int					/* O - Number of bytes formatted */
Michael R Sweet's avatar
Michael R Sweet committed
153 154 155 156
_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 */
157 158 159 160 161 162 163 164 165
{
  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() */
166
		*tptr,			/* Pointer into temporary format */
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		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 == '%')
    {
185 186
      tptr = tformat;
      *tptr++ = *format++;
187 188 189

      if (*format == '%')
      {
Michael R Sweet's avatar
Michael R Sweet committed
190 191
        if (bufptr && bufptr < bufend)
          *bufptr++ = *format;
192 193
        bytes ++;
        format ++;
194 195 196
	continue;
      }
      else if (strchr(" -+#\'", *format))
197 198
      {
        *tptr++ = *format;
199
        sign = *format++;
200
      }
201 202 203
      else
        sign = 0;

204 205
      if (*format == '*')
      {
Michael R Sweet's avatar
Michael R Sweet committed
206 207 208 209
       /*
        * Get width from argument...
	*/

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	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';
	}
      }
228 229 230

      if (*format == '.')
      {
231 232 233
	if (tptr < (tformat + sizeof(tformat) - 1))
	  *tptr++ = *format;

234 235
        format ++;

236 237
        if (*format == '*')
	{
Michael R Sweet's avatar
Michael R Sweet committed
238 239 240 241
         /*
	  * Get precision from argument...
	  */

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	  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';
	  }
	}
260 261 262 263 264 265 266
      }
      else
        prec = -1;

      if (*format == 'l' && format[1] == 'l')
      {
        size = 'L';
267 268 269 270 271 272 273

	if (tptr < (tformat + sizeof(tformat) - 2))
	{
	  *tptr++ = 'l';
	  *tptr++ = 'l';
	}

274 275 276
	format += 2;
      }
      else if (*format == 'h' || *format == 'l' || *format == 'L')
277 278 279 280
      {
	if (tptr < (tformat + sizeof(tformat) - 1))
	  *tptr++ = *format;

281
        size = *format++;
282
      }
283 284 285 286

      if (!*format)
        break;

287 288 289 290 291
      if (tptr < (tformat + sizeof(tformat) - 1))
        *tptr++ = *format;

      type  = *format++;
      *tptr = '\0';
292 293 294 295 296 297 298 299

      switch (type)
      {
	case 'E' : /* Floating point formats */
	case 'G' :
	case 'e' :
	case 'f' :
	case 'g' :
300
	    if ((width + 2) > sizeof(temp))
301 302 303 304 305 306 307 308 309 310
	      break;

	    sprintf(temp, tformat, va_arg(ap, double));

            bytes += strlen(temp);

            if (bufptr)
	    {
	      if ((bufptr + strlen(temp)) > bufend)
	      {
311
		strncpy(bufptr, temp, (size_t)(bufend - bufptr));
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
		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' :
330
	    if ((width + 2) > sizeof(temp))
331 332
	      break;

333 334 335 336 337
#ifdef HAVE_LONG_LONG
	    if (size == 'L')
	      sprintf(temp, tformat, va_arg(ap, long long));
	    else
#endif /* HAVE_LONG_LONG */
338 339 340 341 342 343 344 345
	    sprintf(temp, tformat, va_arg(ap, int));

            bytes += strlen(temp);

	    if (bufptr)
	    {
	      if ((bufptr + strlen(temp)) > bufend)
	      {
346
		strncpy(bufptr, temp, (size_t)(bufend - bufptr));
347 348 349 350 351 352 353 354 355
		bufptr = bufend;
	      }
	      else
	      {
		strcpy(bufptr, temp);
		bufptr += strlen(temp);
	      }
	    }
	    break;
356

357
	case 'p' : /* Pointer value */
358
	    if ((width + 2) > sizeof(temp))
359 360 361 362 363 364 365 366 367 368
	      break;

	    sprintf(temp, tformat, va_arg(ap, void *));

            bytes += strlen(temp);

	    if (bufptr)
	    {
	      if ((bufptr + strlen(temp)) > bufend)
	      {
369
		strncpy(bufptr, temp, (size_t)(bufend - bufptr));
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
		bufptr = bufend;
	      }
	      else
	      {
		strcpy(bufptr, temp);
		bufptr += strlen(temp);
	      }
	    }
	    break;

        case 'c' : /* Character or character array */
	    bytes += width;

	    if (bufptr)
	    {
	      if (width <= 1)
386
	        *bufptr++ = va_arg(ap, int);
387 388 389
	      else
	      {
		if ((bufptr + width) > bufend)
390
		  width = bufend - bufptr;
391

392
		memcpy(bufptr, va_arg(ap, char *), (size_t)width);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
		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)
411
	        width = bufend - bufptr;
412 413

              if (slen > width)
414
	        slen = width;
415 416 417

	      if (sign == '-')
	      {
418 419
		strncpy(bufptr, s, (size_t)slen);
		memset(bufptr + slen, ' ', (size_t)(width - slen));
420 421 422
	      }
	      else
	      {
423 424
		memset(bufptr, ' ', (size_t)(width - slen));
		strncpy(bufptr + width - slen, s, (size_t)slen);
425 426 427 428 429 430 431
	      }

	      bufptr += width;
	    }
	    break;

	case 'n' : /* Output number of chars so far */
432
	    *(va_arg(ap, int *)) = bytes;
433 434 435 436 437 438 439 440
	    break;
      }
    }
    else
    {
      bytes ++;

      if (bufptr && bufptr < bufend)
441
        *bufptr++ = *format;
442 443

      format ++;
444 445 446 447 448 449 450 451 452 453 454 455 456 457
    }
  }

 /*
  * Nul-terminate the string and return the number of characters needed.
  */

  *bufptr = '\0';

  return (bytes);
}
#endif /* !HAVE_VSNPRINTF */


458 459 460 461 462 463 464 465
/*
 * '_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 */
{
466 467 468 469 470 471 472 473
#ifdef HAVE_VASPRINTF
  char		*s;			/* String */

  vasprintf(&s, format, ap);

  return (s);

#else
474 475 476 477
  int		bytes;			/* Number of bytes required */
  char		*buffer,		/* String buffer */
		temp[256];		/* Small buffer for first vsnprintf */
  va_list	apcopy;			/* Copy of argument list */
478 479 480 481 482 483 484


 /*
  * First format with a tiny buffer; this will tell us how many bytes are
  * needed...
  */

485 486
  va_copy(apcopy, ap);
  bytes = vsnprintf(temp, sizeof(temp), format, apcopy);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

  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);
510
#endif /* HAVE_VASPRINTF */
511
}