varbuf.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * varbuf.c - variable length expandable buffer handling
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2008, 2009 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. #include <config.h>
  22. #include <compat.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <dpkg/i18n.h>
  27. #include <dpkg/dpkg.h>
  28. #include <dpkg/dpkg-db.h>
  29. void
  30. varbufaddc(struct varbuf *v, int c)
  31. {
  32. varbuf_grow(v, 1);
  33. v->buf[v->used++]= c;
  34. }
  35. void
  36. varbufdupc(struct varbuf *v, int c, size_t n)
  37. {
  38. varbuf_grow(v, n);
  39. memset(v->buf + v->used, c, n);
  40. v->used += n;
  41. }
  42. void
  43. varbufsubstc(struct varbuf *v, int c_src, int c_dst)
  44. {
  45. size_t i;
  46. for (i = 0; i < v->used; i++)
  47. if (v->buf[i] == c_src)
  48. v->buf[i] = c_dst;
  49. }
  50. int varbufprintf(struct varbuf *v, const char *fmt, ...) {
  51. int r;
  52. va_list al;
  53. va_start(al, fmt);
  54. r = varbufvprintf(v, fmt, al);
  55. va_end(al);
  56. return r;
  57. }
  58. int varbufvprintf(struct varbuf *v, const char *fmt, va_list va) {
  59. va_list al;
  60. int needed, r;
  61. va_copy(al, va);
  62. needed = vsnprintf(NULL, 0, fmt, al);
  63. va_end(al);
  64. if (needed < 0)
  65. ohshite(_("error formatting string into varbuf variable"));
  66. varbuf_grow(v, needed + 1);
  67. va_copy(al, va);
  68. r = vsnprintf(v->buf + v->used, needed + 1, fmt, al);
  69. va_end(al);
  70. if (r < 0)
  71. ohshite(_("error formatting string into varbuf variable"));
  72. v->used += r;
  73. return r;
  74. }
  75. void
  76. varbufaddbuf(struct varbuf *v, const void *s, size_t size)
  77. {
  78. varbuf_grow(v, size);
  79. memcpy(v->buf + v->used, s, size);
  80. v->used += size;
  81. }
  82. void
  83. varbufinit(struct varbuf *v, size_t size)
  84. {
  85. v->used = 0;
  86. v->size = size;
  87. if (size)
  88. v->buf = m_malloc(size);
  89. else
  90. v->buf = NULL;
  91. }
  92. void varbufreset(struct varbuf *v) {
  93. v->used= 0;
  94. }
  95. void
  96. varbuf_grow(struct varbuf *v, size_t need_size)
  97. {
  98. /* Make sure the varbuf is in a sane state. */
  99. if (v->size < v->used)
  100. internerr("inconsistent varbuf: size(%d) < used(%d)", v->size, v->used);
  101. /* Check if we already have enough room. */
  102. if ((v->size - v->used) >= need_size)
  103. return;
  104. v->size = (v->size + need_size) * 2;
  105. v->buf = m_realloc(v->buf, v->size);
  106. }
  107. void varbuffree(struct varbuf *v) {
  108. free(v->buf); v->buf=NULL; v->size=0; v->used=0;
  109. }