varbuf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * varbuf.c - variable length expandable buffer handling
  4. *
  5. * Copyright (C) 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 <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <config.h>
  25. #include <dpkg.h>
  26. #include <dpkg-db.h>
  27. #ifndef HAVE_VA_COPY
  28. #define __va_copy(dest,src) (dest) = (src)
  29. #endif
  30. inline void varbufaddc(struct varbuf *v, int c) {
  31. if (v->used >= v->size) varbufextend(v);
  32. v->buf[v->used++]= c;
  33. }
  34. void varbufdupc(struct varbuf *v, int c, ssize_t n) {
  35. char *b = v->buf + v->used;
  36. v->used += n;
  37. if (v->used >= v->size) varbufextend(v);
  38. while(n) {
  39. *b= c;
  40. b++; n--;
  41. }
  42. }
  43. int varbufprintf(struct varbuf *v, const char *fmt, ...) {
  44. unsigned int ou, r;
  45. va_list al;
  46. ou= v->used;
  47. v->used+= strlen(fmt);
  48. do {
  49. varbufextend(v);
  50. va_start(al,fmt);
  51. r= vsnprintf(v->buf+ou,v->size-ou,fmt,al);
  52. va_end(al);
  53. if (r < 0) r= (v->size-ou+1) * 2;
  54. v->used= ou+r;
  55. } while (r >= v->size-ou-1);
  56. return r;
  57. }
  58. int varbufvprintf(struct varbuf *v, const char *fmt, va_list va) {
  59. unsigned int ou, r;
  60. va_list al;
  61. ou= v->used;
  62. v->used+= strlen(fmt);
  63. do {
  64. varbufextend(v);
  65. __va_copy(al, va);
  66. r= vsnprintf(v->buf+ou,v->size-ou,fmt,al);
  67. if (r < 0) r= (v->size-ou+1) * 2;
  68. v->used= ou+r;
  69. } while (r >= v->size-ou-1);
  70. return r;
  71. }
  72. void varbufaddbuf(struct varbuf *v, const void *s, const int l) {
  73. int ou;
  74. ou= v->used;
  75. v->used += l;
  76. if (v->used >= v->size) varbufextend(v);
  77. memcpy(v->buf + ou, s, l);
  78. }
  79. void varbufinit(struct varbuf *v) {
  80. /* NB: dbmodify.c does its own init to get a big buffer */
  81. v->size= v->used= 0;
  82. v->buf= NULL;
  83. }
  84. void varbufreset(struct varbuf *v) {
  85. v->used= 0;
  86. }
  87. void varbufextend(struct varbuf *v) {
  88. int newsize;
  89. char *newbuf;
  90. newsize= v->size + 80 + v->used;
  91. newbuf= realloc(v->buf,newsize);
  92. if (!newbuf) ohshite(_("failed to realloc for variable buffer"));
  93. v->size= newsize;
  94. v->buf= newbuf;
  95. }
  96. void varbuffree(struct varbuf *v) {
  97. free(v->buf); v->buf=NULL; v->size=0; v->used=0;
  98. }