varbuf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <dpkg.h>
  27. #include <dpkg-db.h>
  28. void
  29. varbufaddc(struct varbuf *v, int c)
  30. {
  31. if (v->used >= v->size) varbufextend(v);
  32. v->buf[v->used++]= c;
  33. }
  34. void
  35. varbufdupc(struct varbuf *v, int c, size_t n)
  36. {
  37. size_t old_used = v->used;
  38. v->used += n;
  39. if (v->used >= v->size) varbufextend(v);
  40. memset(v->buf + old_used, c, 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. size_t ou;
  60. int r;
  61. va_list al;
  62. ou= v->used;
  63. v->used+= strlen(fmt);
  64. do {
  65. varbufextend(v);
  66. va_copy(al, va);
  67. r= vsnprintf(v->buf+ou,v->size-ou,fmt,al);
  68. va_end(al);
  69. if (r < 0) r= (v->size-ou+1) * 2;
  70. v->used= ou+r;
  71. } while (r >= (int)(v->size - ou - 1));
  72. return r;
  73. }
  74. void
  75. varbufaddbuf(struct varbuf *v, const void *s, size_t size)
  76. {
  77. int ou;
  78. ou= v->used;
  79. v->used += size;
  80. if (v->used >= v->size) varbufextend(v);
  81. memcpy(v->buf + ou, s, size);
  82. }
  83. void
  84. varbufinit(struct varbuf *v, size_t size)
  85. {
  86. v->used = 0;
  87. v->size = size;
  88. if (size)
  89. v->buf = m_malloc(size);
  90. else
  91. v->buf = NULL;
  92. }
  93. void varbufreset(struct varbuf *v) {
  94. v->used= 0;
  95. }
  96. void varbufextend(struct varbuf *v) {
  97. int newsize;
  98. char *newbuf;
  99. newsize= v->size + 80 + v->used;
  100. newbuf = m_realloc(v->buf, newsize);
  101. v->size= newsize;
  102. v->buf= newbuf;
  103. }
  104. void varbuffree(struct varbuf *v) {
  105. free(v->buf); v->buf=NULL; v->size=0; v->used=0;
  106. }