mxml-string.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * "$Id: mxml-string.c 454 2014-01-05 03:25:07Z msweet $"
  3. *
  4. * String functions for Mini-XML, a small XML-like file parsing library.
  5. *
  6. * Copyright 2003-2014 by Michael R Sweet.
  7. *
  8. * These coded instructions, statements, and computer programs are the
  9. * property of Michael R Sweet and are protected by Federal copyright
  10. * law. Distribution and use rights are outlined in the file "COPYING"
  11. * which should have been included with this file. If this file is
  12. * missing or damaged, see the license at:
  13. *
  14. * http://www.msweet.org/projects.php/Mini-XML
  15. */
  16. /*
  17. * Include necessary headers...
  18. */
  19. #include "config.h"
  20. /*
  21. * The va_copy macro is part of C99, but many compilers don't implement it.
  22. * Provide a "direct assignment" implmentation when va_copy isn't defined...
  23. */
  24. #ifndef va_copy
  25. # ifdef __va_copy
  26. # define va_copy(dst,src) __va_copy(dst,src)
  27. # else
  28. # define va_copy(dst,src) memcpy(&dst, src, sizeof(va_list))
  29. # endif /* __va_copy */
  30. #endif /* va_copy */
  31. #ifndef HAVE_SNPRINTF
  32. /*
  33. * '_mxml_snprintf()' - Format a string.
  34. */
  35. int /* O - Number of bytes formatted */
  36. _mxml_snprintf(char *buffer, /* I - Output buffer */
  37. size_t bufsize, /* I - Size of output buffer */
  38. const char *format, /* I - Printf-style format string */
  39. ...) /* I - Additional arguments as needed */
  40. {
  41. va_list ap; /* Argument list */
  42. int bytes; /* Number of bytes formatted */
  43. va_start(ap, format);
  44. bytes = vsnprintf(buffer, bufsize, format, ap);
  45. va_end(ap);
  46. return (bytes);
  47. }
  48. #endif /* !HAVE_SNPRINTF */
  49. /*
  50. * '_mxml_strdup()' - Duplicate a string.
  51. */
  52. #ifndef HAVE_STRDUP
  53. char * /* O - New string pointer */
  54. _mxml_strdup(const char *s) /* I - String to duplicate */
  55. {
  56. char *t; /* New string pointer */
  57. if (s == NULL)
  58. return (NULL);
  59. if ((t = malloc(strlen(s) + 1)) == NULL)
  60. return (NULL);
  61. return (strcpy(t, s));
  62. }
  63. #endif /* !HAVE_STRDUP */
  64. /*
  65. * '_mxml_strdupf()' - Format and duplicate a string.
  66. */
  67. char * /* O - New string pointer */
  68. _mxml_strdupf(const char *format, /* I - Printf-style format string */
  69. ...) /* I - Additional arguments as needed */
  70. {
  71. va_list ap; /* Pointer to additional arguments */
  72. char *s; /* Pointer to formatted string */
  73. /*
  74. * Get a pointer to the additional arguments, format the string,
  75. * and return it...
  76. */
  77. va_start(ap, format);
  78. s = _mxml_vstrdupf(format, ap);
  79. va_end(ap);
  80. return (s);
  81. }
  82. #ifndef HAVE_VSNPRINTF
  83. /*
  84. * '_mxml_vsnprintf()' - Format a string into a fixed size buffer.
  85. */
  86. int /* O - Number of bytes formatted */
  87. _mxml_vsnprintf(char *buffer, /* O - Output buffer */
  88. size_t bufsize, /* O - Size of output buffer */
  89. const char *format, /* I - Printf-style format string */
  90. va_list ap) /* I - Pointer to additional arguments */
  91. {
  92. char *bufptr, /* Pointer to position in buffer */
  93. *bufend, /* Pointer to end of buffer */
  94. sign, /* Sign of format width */
  95. size, /* Size character (h, l, L) */
  96. type; /* Format type character */
  97. int width, /* Width of field */
  98. prec; /* Number of characters of precision */
  99. char tformat[100], /* Temporary format string for sprintf() */
  100. *tptr, /* Pointer into temporary format */
  101. temp[1024]; /* Buffer for formatted numbers */
  102. char *s; /* Pointer to string */
  103. int slen; /* Length of string */
  104. int bytes; /* Total number of bytes needed */
  105. /*
  106. * Loop through the format string, formatting as needed...
  107. */
  108. bufptr = buffer;
  109. bufend = buffer + bufsize - 1;
  110. bytes = 0;
  111. while (*format)
  112. {
  113. if (*format == '%')
  114. {
  115. tptr = tformat;
  116. *tptr++ = *format++;
  117. if (*format == '%')
  118. {
  119. if (bufptr && bufptr < bufend)
  120. *bufptr++ = *format;
  121. bytes ++;
  122. format ++;
  123. continue;
  124. }
  125. else if (strchr(" -+#\'", *format))
  126. {
  127. *tptr++ = *format;
  128. sign = *format++;
  129. }
  130. else
  131. sign = 0;
  132. if (*format == '*')
  133. {
  134. /*
  135. * Get width from argument...
  136. */
  137. format ++;
  138. width = va_arg(ap, int);
  139. snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", width);
  140. tptr += strlen(tptr);
  141. }
  142. else
  143. {
  144. width = 0;
  145. while (isdigit(*format & 255))
  146. {
  147. if (tptr < (tformat + sizeof(tformat) - 1))
  148. *tptr++ = *format;
  149. width = width * 10 + *format++ - '0';
  150. }
  151. }
  152. if (*format == '.')
  153. {
  154. if (tptr < (tformat + sizeof(tformat) - 1))
  155. *tptr++ = *format;
  156. format ++;
  157. if (*format == '*')
  158. {
  159. /*
  160. * Get precision from argument...
  161. */
  162. format ++;
  163. prec = va_arg(ap, int);
  164. snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", prec);
  165. tptr += strlen(tptr);
  166. }
  167. else
  168. {
  169. prec = 0;
  170. while (isdigit(*format & 255))
  171. {
  172. if (tptr < (tformat + sizeof(tformat) - 1))
  173. *tptr++ = *format;
  174. prec = prec * 10 + *format++ - '0';
  175. }
  176. }
  177. }
  178. else
  179. prec = -1;
  180. if (*format == 'l' && format[1] == 'l')
  181. {
  182. size = 'L';
  183. if (tptr < (tformat + sizeof(tformat) - 2))
  184. {
  185. *tptr++ = 'l';
  186. *tptr++ = 'l';
  187. }
  188. format += 2;
  189. }
  190. else if (*format == 'h' || *format == 'l' || *format == 'L')
  191. {
  192. if (tptr < (tformat + sizeof(tformat) - 1))
  193. *tptr++ = *format;
  194. size = *format++;
  195. }
  196. if (!*format)
  197. break;
  198. if (tptr < (tformat + sizeof(tformat) - 1))
  199. *tptr++ = *format;
  200. type = *format++;
  201. *tptr = '\0';
  202. switch (type)
  203. {
  204. case 'E' : /* Floating point formats */
  205. case 'G' :
  206. case 'e' :
  207. case 'f' :
  208. case 'g' :
  209. if ((width + 2) > sizeof(temp))
  210. break;
  211. sprintf(temp, tformat, va_arg(ap, double));
  212. bytes += strlen(temp);
  213. if (bufptr)
  214. {
  215. if ((bufptr + strlen(temp)) > bufend)
  216. {
  217. strncpy(bufptr, temp, (size_t)(bufend - bufptr));
  218. bufptr = bufend;
  219. }
  220. else
  221. {
  222. strcpy(bufptr, temp);
  223. bufptr += strlen(temp);
  224. }
  225. }
  226. break;
  227. case 'B' : /* Integer formats */
  228. case 'X' :
  229. case 'b' :
  230. case 'd' :
  231. case 'i' :
  232. case 'o' :
  233. case 'u' :
  234. case 'x' :
  235. if ((width + 2) > sizeof(temp))
  236. break;
  237. #ifdef HAVE_LONG_LONG
  238. if (size == 'L')
  239. sprintf(temp, tformat, va_arg(ap, long long));
  240. else
  241. #endif /* HAVE_LONG_LONG */
  242. sprintf(temp, tformat, va_arg(ap, int));
  243. bytes += strlen(temp);
  244. if (bufptr)
  245. {
  246. if ((bufptr + strlen(temp)) > bufend)
  247. {
  248. strncpy(bufptr, temp, (size_t)(bufend - bufptr));
  249. bufptr = bufend;
  250. }
  251. else
  252. {
  253. strcpy(bufptr, temp);
  254. bufptr += strlen(temp);
  255. }
  256. }
  257. break;
  258. case 'p' : /* Pointer value */
  259. if ((width + 2) > sizeof(temp))
  260. break;
  261. sprintf(temp, tformat, va_arg(ap, void *));
  262. bytes += strlen(temp);
  263. if (bufptr)
  264. {
  265. if ((bufptr + strlen(temp)) > bufend)
  266. {
  267. strncpy(bufptr, temp, (size_t)(bufend - bufptr));
  268. bufptr = bufend;
  269. }
  270. else
  271. {
  272. strcpy(bufptr, temp);
  273. bufptr += strlen(temp);
  274. }
  275. }
  276. break;
  277. case 'c' : /* Character or character array */
  278. bytes += width;
  279. if (bufptr)
  280. {
  281. if (width <= 1)
  282. *bufptr++ = va_arg(ap, int);
  283. else
  284. {
  285. if ((bufptr + width) > bufend)
  286. width = bufend - bufptr;
  287. memcpy(bufptr, va_arg(ap, char *), (size_t)width);
  288. bufptr += width;
  289. }
  290. }
  291. break;
  292. case 's' : /* String */
  293. if ((s = va_arg(ap, char *)) == NULL)
  294. s = "(null)";
  295. slen = strlen(s);
  296. if (slen > width && prec != width)
  297. width = slen;
  298. bytes += width;
  299. if (bufptr)
  300. {
  301. if ((bufptr + width) > bufend)
  302. width = bufend - bufptr;
  303. if (slen > width)
  304. slen = width;
  305. if (sign == '-')
  306. {
  307. strncpy(bufptr, s, (size_t)slen);
  308. memset(bufptr + slen, ' ', (size_t)(width - slen));
  309. }
  310. else
  311. {
  312. memset(bufptr, ' ', (size_t)(width - slen));
  313. strncpy(bufptr + width - slen, s, (size_t)slen);
  314. }
  315. bufptr += width;
  316. }
  317. break;
  318. case 'n' : /* Output number of chars so far */
  319. *(va_arg(ap, int *)) = bytes;
  320. break;
  321. }
  322. }
  323. else
  324. {
  325. bytes ++;
  326. if (bufptr && bufptr < bufend)
  327. *bufptr++ = *format;
  328. format ++;
  329. }
  330. }
  331. /*
  332. * Nul-terminate the string and return the number of characters needed.
  333. */
  334. *bufptr = '\0';
  335. return (bytes);
  336. }
  337. #endif /* !HAVE_VSNPRINTF */
  338. /*
  339. * '_mxml_vstrdupf()' - Format and duplicate a string.
  340. */
  341. char * /* O - New string pointer */
  342. _mxml_vstrdupf(const char *format, /* I - Printf-style format string */
  343. va_list ap) /* I - Pointer to additional arguments */
  344. {
  345. int bytes; /* Number of bytes required */
  346. char *buffer, /* String buffer */
  347. temp[256]; /* Small buffer for first vsnprintf */
  348. va_list apcopy; /* Copy of argument list */
  349. /*
  350. * First format with a tiny buffer; this will tell us how many bytes are
  351. * needed...
  352. */
  353. va_copy(apcopy, ap);
  354. bytes = vsnprintf(temp, sizeof(temp), format, apcopy);
  355. if (bytes < sizeof(temp))
  356. {
  357. /*
  358. * Hey, the formatted string fits in the tiny buffer, so just dup that...
  359. */
  360. return (strdup(temp));
  361. }
  362. /*
  363. * Allocate memory for the whole thing and reformat to the new, larger
  364. * buffer...
  365. */
  366. if ((buffer = calloc(1, bytes + 1)) != NULL)
  367. vsnprintf(buffer, bytes + 1, format, ap);
  368. /*
  369. * Return the new string...
  370. */
  371. return (buffer);
  372. }
  373. /*
  374. * End of "$Id: mxml-string.c 454 2014-01-05 03:25:07Z msweet $".
  375. */