varbuf.c 2.6 KB

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