vasnprintf.h 3.0 KB

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