varbuf.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * varbuf.h - variable length expandable buffer handling
  4. *
  5. * Copyright © 1994, 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2008-2011 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef LIBDPKG_VARBUF_H
  22. #define LIBDPKG_VARBUF_H
  23. #include <stddef.h>
  24. #include <stdarg.h>
  25. #include <dpkg/macros.h>
  26. DPKG_BEGIN_DECLS
  27. /*
  28. * varbuf_init must be called exactly once before the use of each varbuf
  29. * (including before any call to varbuf_destroy), or the variable must be
  30. * initialized with VARBUF_INIT.
  31. *
  32. * However, varbufs allocated ‘static’ are properly initialized anyway and
  33. * do not need varbuf_init; multiple consecutive calls to varbuf_init before
  34. * any use are allowed.
  35. *
  36. * varbuf_destroy must be called after a varbuf is finished with, if anything
  37. * other than varbuf_init has been done. After this you are allowed but
  38. * not required to call varbuf_init again if you want to start using the
  39. * varbuf again.
  40. *
  41. * Callers using C++ need not worry about any of this.
  42. */
  43. struct varbuf {
  44. size_t used, size;
  45. char *buf;
  46. #ifdef __cplusplus
  47. varbuf(size_t _size = 0);
  48. ~varbuf();
  49. void init(size_t _size = 0);
  50. void reset();
  51. void destroy();
  52. void operator()(int c);
  53. void operator()(const char *s);
  54. void terminate(void/*to shut 2.6.3 up*/);
  55. const char *string();
  56. #endif
  57. };
  58. #define VARBUF_INIT { 0, 0, NULL }
  59. void varbuf_init(struct varbuf *v, size_t size);
  60. void varbuf_grow(struct varbuf *v, size_t need_size);
  61. void varbuf_trunc(struct varbuf *v, size_t used_size);
  62. char *varbuf_detach(struct varbuf *v);
  63. void varbuf_reset(struct varbuf *v);
  64. void varbuf_destroy(struct varbuf *v);
  65. void varbuf_add_char(struct varbuf *v, int c);
  66. void varbuf_dup_char(struct varbuf *v, int c, size_t n);
  67. void varbuf_map_char(struct varbuf *v, int c_src, int c_dst);
  68. #define varbuf_add_str(v, s) varbuf_add_buf(v, s, strlen(s))
  69. void varbuf_add_buf(struct varbuf *v, const void *s, size_t size);
  70. void varbuf_end_str(struct varbuf *v);
  71. int varbuf_printf(struct varbuf *v, const char *fmt, ...) DPKG_ATTR_PRINTF(2);
  72. int varbuf_vprintf(struct varbuf *v, const char *fmt, va_list va)
  73. DPKG_ATTR_VPRINTF(2);
  74. DPKG_END_DECLS
  75. #ifdef __cplusplus
  76. inline
  77. varbuf::varbuf(size_t _size)
  78. {
  79. varbuf_init(this, _size);
  80. }
  81. inline
  82. varbuf::~varbuf()
  83. {
  84. varbuf_destroy(this);
  85. }
  86. inline void
  87. varbuf::init(size_t _size)
  88. {
  89. varbuf_init(this, _size);
  90. }
  91. inline void
  92. varbuf::reset()
  93. {
  94. used = 0;
  95. }
  96. inline void
  97. varbuf::destroy()
  98. {
  99. varbuf_destroy(this);
  100. }
  101. inline void
  102. varbuf::operator()(int c)
  103. {
  104. varbuf_add_char(this, c);
  105. }
  106. inline void
  107. varbuf::operator()(const char *s)
  108. {
  109. varbuf_add_str(this, s);
  110. }
  111. inline void
  112. varbuf::terminate(void/*to shut 2.6.3 up*/)
  113. {
  114. varbuf_end_str(this);
  115. }
  116. inline const char *
  117. varbuf::string()
  118. {
  119. terminate();
  120. return buf;
  121. }
  122. #endif
  123. #endif /* LIBDPKG_VARBUF_H */