vasnprintf.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* vsprintf with automatic memory allocation.
  2. Copyright (C) 2002-2004, 2007-2008 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, 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. #ifndef __attribute__
  21. /* This feature is available in gcc versions 2.5 and later. */
  22. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
  23. # define __attribute__(Spec) /* empty */
  24. # endif
  25. /* The __-protected variants of `format' and `printf' attributes
  26. are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
  27. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
  28. # define __format__ format
  29. # define __printf__ printf
  30. # endif
  31. #endif
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* Write formatted output to a string dynamically allocated with malloc().
  36. You can pass a preallocated buffer for the result in RESULTBUF and its
  37. size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
  38. If successful, return the address of the string (this may be = RESULTBUF
  39. if no dynamic memory allocation was necessary) and set *LENGTHP to the
  40. number of resulting bytes, excluding the trailing NUL. Upon error, set
  41. errno and return NULL.
  42. When dynamic memory allocation occurs, the preallocated buffer is left
  43. alone (with possibly modified contents). This makes it possible to use
  44. a statically allocated or stack-allocated buffer, like this:
  45. char buf[100];
  46. size_t len = sizeof (buf);
  47. char *output = vasnprintf (buf, &len, format, args);
  48. if (output == NULL)
  49. ... error handling ...;
  50. else
  51. {
  52. ... use the output string ...;
  53. if (output != buf)
  54. free (output);
  55. }
  56. */
  57. #if REPLACE_VASNPRINTF
  58. # define asnprintf rpl_asnprintf
  59. # define vasnprintf rpl_vasnprintf
  60. #endif
  61. extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
  62. __attribute__ ((__format__ (__printf__, 3, 4)));
  63. extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
  64. __attribute__ ((__format__ (__printf__, 3, 0)));
  65. #ifdef __cplusplus
  66. }
  67. #endif
  68. #endif /* _VASNPRINTF_H */