varbuf.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <https://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 <string.h>
  26. #include <dpkg/macros.h>
  27. DPKG_BEGIN_DECLS
  28. /**
  29. * @defgroup varbuf Variable length buffers
  30. * @ingroup dpkg-public
  31. * @{
  32. */
  33. /**
  34. * varbuf_init must be called exactly once before the use of each varbuf
  35. * (including before any call to varbuf_destroy), or the variable must be
  36. * initialized with VARBUF_INIT.
  37. *
  38. * However, varbufs allocated ‘static’ are properly initialized anyway and
  39. * do not need varbuf_init; multiple consecutive calls to varbuf_init before
  40. * any use are allowed.
  41. *
  42. * varbuf_destroy must be called after a varbuf is finished with, if anything
  43. * other than varbuf_init has been done. After this you are allowed but
  44. * not required to call varbuf_init again if you want to start using the
  45. * varbuf again.
  46. *
  47. * Callers using C++ need not worry about any of this.
  48. */
  49. struct varbuf {
  50. size_t used, size;
  51. char *buf;
  52. #ifdef __cplusplus
  53. varbuf(size_t _size = 0);
  54. ~varbuf();
  55. void init(size_t _size = 0);
  56. void reset();
  57. void destroy();
  58. void operator()(int c);
  59. void operator()(const char *s);
  60. void terminate(void/*to shut 2.6.3 up*/);
  61. const char *string();
  62. #endif
  63. };
  64. #define VARBUF_INIT { 0, 0, NULL }
  65. void varbuf_init(struct varbuf *v, size_t size);
  66. void varbuf_grow(struct varbuf *v, size_t need_size);
  67. void varbuf_trunc(struct varbuf *v, size_t used_size);
  68. char *varbuf_detach(struct varbuf *v);
  69. void varbuf_reset(struct varbuf *v);
  70. void varbuf_destroy(struct varbuf *v);
  71. void varbuf_add_char(struct varbuf *v, int c);
  72. void varbuf_dup_char(struct varbuf *v, int c, size_t n);
  73. void varbuf_map_char(struct varbuf *v, int c_src, int c_dst);
  74. #define varbuf_add_str(v, s) varbuf_add_buf(v, s, strlen(s))
  75. void varbuf_add_buf(struct varbuf *v, const void *s, size_t size);
  76. void varbuf_end_str(struct varbuf *v);
  77. int varbuf_printf(struct varbuf *v, const char *fmt, ...) DPKG_ATTR_PRINTF(2);
  78. int varbuf_vprintf(struct varbuf *v, const char *fmt, va_list va)
  79. DPKG_ATTR_VPRINTF(2);
  80. /** @} */
  81. DPKG_END_DECLS
  82. #ifdef __cplusplus
  83. inline
  84. varbuf::varbuf(size_t _size)
  85. {
  86. varbuf_init(this, _size);
  87. }
  88. inline
  89. varbuf::~varbuf()
  90. {
  91. varbuf_destroy(this);
  92. }
  93. inline void
  94. varbuf::init(size_t _size)
  95. {
  96. varbuf_init(this, _size);
  97. }
  98. inline void
  99. varbuf::reset()
  100. {
  101. used = 0;
  102. }
  103. inline void
  104. varbuf::destroy()
  105. {
  106. varbuf_destroy(this);
  107. }
  108. inline void
  109. varbuf::operator()(int c)
  110. {
  111. varbuf_add_char(this, c);
  112. }
  113. inline void
  114. varbuf::operator()(const char *s)
  115. {
  116. varbuf_add_str(this, s);
  117. }
  118. inline void
  119. varbuf::terminate(void/*to shut 2.6.3 up*/)
  120. {
  121. varbuf_end_str(this);
  122. }
  123. inline const char *
  124. varbuf::string()
  125. {
  126. terminate();
  127. return buf;
  128. }
  129. #endif
  130. #endif /* LIBDPKG_VARBUF_H */