estream-printf.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* estream-printf.h - Versatile C-99 compliant printf formatting.
  2. * Copyright (C) 2007 g10 Code GmbH
  3. *
  4. * This file is part of Libestream.
  5. *
  6. * Libestream is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License,
  9. * or (at your option) any later version.
  10. *
  11. * Libestream is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Libestream; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ESTREAM_PRINTF_H
  20. #define ESTREAM_PRINTF_H
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. /* To use this file with libraries the following macro is useful:
  24. #define _ESTREAM_EXT_SYM_PREFIX _foo_
  25. This prefixes all external symbols with "_foo_".
  26. For the implementation of the code (estream-printf.c) the following
  27. macros may be used to tune the implementation for certain systems:
  28. #define _ESTREAM_PRINTF_MALLOC foo_malloc
  29. #define _ESTREAM_PRINTF_FREE foo_free
  30. Make estream_asprintf and estream_vasprintf use foo_malloc and
  31. foo_free instead of the standard malloc and free functions to
  32. allocate the memory returned to the caller.
  33. #define _ESTREAM_PRINTF_EXTRA_INCLUDE "foo.h"
  34. This includes the file "foo.h" which may provide prototypes for
  35. the custom memory allocation functions.
  36. */
  37. #ifdef _ESTREAM_EXT_SYM_PREFIX
  38. #ifndef _ESTREAM_PREFIX
  39. #define _ESTREAM_PREFIX1(x,y) x ## y
  40. #define _ESTREAM_PREFIX2(x,y) _ESTREAM_PREFIX1(x,y)
  41. #define _ESTREAM_PREFIX(x) _ESTREAM_PREFIX2(_ESTREAM_EXT_SYM_PREFIX,x)
  42. #endif /*_ESTREAM_PREFIX*/
  43. #define estream_printf_out_t _ESTREAM_PREFIX(estream_printf_out_t)
  44. #define estream_format _ESTREAM_PREFIX(estream_format)
  45. #define estream_printf _ESTREAM_PREFIX(estream_printf)
  46. #define estream_fprintf _ESTREAM_PREFIX(estream_fprintf)
  47. #define estream_vfprintf _ESTREAM_PREFIX(estream_vfprintf)
  48. #define estream_snprintf _ESTREAM_PREFIX(estream_snprintf)
  49. #define estream_vsnprintf _ESTREAM_PREFIX(estream_vsnprintf)
  50. #define estream_asprintf _ESTREAM_PREFIX(estream_asprintf)
  51. #define estream_vasprintf _ESTREAM_PREFIX(estream_vasprintf)
  52. #endif /*_ESTREAM_EXT_SYM_PREFIX*/
  53. #ifndef _ESTREAM_GCC_A_PRINTF
  54. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
  55. # define _ESTREAM_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a)))
  56. #else
  57. # define _ESTREAM_GCC_A_PRINTF( f, a )
  58. #endif
  59. #endif /*_ESTREAM_GCC_A_PRINTF*/
  60. #ifdef __cplusplus
  61. extern "C"
  62. {
  63. #if 0
  64. }
  65. #endif
  66. #endif
  67. typedef int (*estream_printf_out_t)
  68. (void *outfncarg, const char *buf, size_t buflen);
  69. int estream_format (estream_printf_out_t outfnc, void *outfncarg,
  70. const char *format, va_list vaargs)
  71. _ESTREAM_GCC_A_PRINTF(3,0);
  72. int estream_printf (const char *format, ...)
  73. _ESTREAM_GCC_A_PRINTF(1,2);
  74. int estream_fprintf (FILE *fp, const char *format, ... )
  75. _ESTREAM_GCC_A_PRINTF(2,3);
  76. int estream_vfprintf (FILE *fp, const char *format, va_list arg_ptr)
  77. _ESTREAM_GCC_A_PRINTF(2,0);
  78. int estream_snprintf (char *buf, size_t bufsize, const char *format, ...)
  79. _ESTREAM_GCC_A_PRINTF(3,4);
  80. int estream_vsnprintf (char *buf,size_t bufsize,
  81. const char *format, va_list arg_ptr)
  82. _ESTREAM_GCC_A_PRINTF(3,0);
  83. int estream_asprintf (char **bufp, const char *format, ...)
  84. _ESTREAM_GCC_A_PRINTF(2,3);
  85. int estream_vasprintf (char **bufp, const char *format, va_list arg_ptr)
  86. _ESTREAM_GCC_A_PRINTF(2,0);
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90. #endif /*ESTREAM_PRINTF_H*/