vasnprintf.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* vsprintf with automatic memory allocation.
  2. Copyright (C) 2002-2004, 2007-2013 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef _VASNPRINTF_H
  14. #define _VASNPRINTF_H
  15. /* Get va_list. */
  16. #include <stdarg.h>
  17. /* Get size_t. */
  18. #include <stddef.h>
  19. /* The __attribute__ feature is available in gcc versions 2.5 and later.
  20. The __-protected variants of the attributes 'format' and 'printf' are
  21. accepted by gcc versions 2.6.4 (effectively 2.7) and later.
  22. We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
  23. gnulib and libintl do '#define printf __printf__' when they override
  24. the 'printf' function. */
  25. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
  26. # define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
  27. #else
  28. # define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* Write formatted output to a string dynamically allocated with malloc().
  34. You can pass a preallocated buffer for the result in RESULTBUF and its
  35. size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
  36. If successful, return the address of the string (this may be = RESULTBUF
  37. if no dynamic memory allocation was necessary) and set *LENGTHP to the
  38. number of resulting bytes, excluding the trailing NUL. Upon error, set
  39. errno and return NULL.
  40. When dynamic memory allocation occurs, the preallocated buffer is left
  41. alone (with possibly modified contents). This makes it possible to use
  42. a statically allocated or stack-allocated buffer, like this:
  43. char buf[100];
  44. size_t len = sizeof (buf);
  45. char *output = vasnprintf (buf, &len, format, args);
  46. if (output == NULL)
  47. ... error handling ...;
  48. else
  49. {
  50. ... use the output string ...;
  51. if (output != buf)
  52. free (output);
  53. }
  54. */
  55. #if REPLACE_VASNPRINTF
  56. # define asnprintf rpl_asnprintf
  57. # define vasnprintf rpl_vasnprintf
  58. #endif
  59. extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
  60. _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 4));
  61. extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
  62. _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 0));
  63. #ifdef __cplusplus
  64. }
  65. #endif
  66. #endif /* _VASNPRINTF_H */