Просмотр исходного кода

libdpkg: Add new varbuf_detach function

Guillem Jover лет назад: 16
Родитель
Сommit
863943468e
3 измененных файлов с 36 добавлено и 0 удалено
  1. 23 0
      lib/dpkg/test/t-varbuf.c
  2. 12 0
      lib/dpkg/varbuf.c
  3. 1 0
      lib/dpkg/varbuf.h

+ 23 - 0
lib/dpkg/test/t-varbuf.c

@@ -21,6 +21,8 @@
 #include <dpkg/test.h>
 #include <dpkg/varbuf.h>
 
+#include <stdlib.h>
+
 static void
 test_varbuf_init(void)
 {
@@ -221,6 +223,26 @@ test_varbuf_reset(void)
 	varbuffree(&vb);
 }
 
+static void
+test_varbuf_detach(void)
+{
+	struct varbuf vb;
+	char *str;
+
+	varbufinit(&vb, 0);
+
+	varbufaddbuf(&vb, "1234567890", 10);
+
+	str = varbuf_detach(&vb);
+
+	test_mem(str, ==, "1234567890", 10);
+	test_pass(vb.used == 0);
+	test_pass(vb.size == 0);
+	test_pass(vb.buf == NULL);
+
+	free(str);
+}
+
 static void
 test(void)
 {
@@ -233,6 +255,7 @@ test(void)
 	test_varbuf_substc();
 	test_varbuf_printf();
 	test_varbuf_reset();
+	test_varbuf_detach();
 
 	/* FIXME: Complete. */
 }

+ 12 - 0
lib/dpkg/varbuf.c

@@ -129,6 +129,18 @@ varbuf_grow(struct varbuf *v, size_t need_size)
   v->buf = m_realloc(v->buf, v->size);
 }
 
+char *
+varbuf_detach(struct varbuf *v)
+{
+  char *buf = v->buf;
+
+  v->buf = NULL;
+  v->size = 0;
+  v->used = 0;
+
+  return buf;
+}
+
 void varbuffree(struct varbuf *v) {
   free(v->buf); v->buf=NULL; v->size=0; v->used=0;
 }

+ 1 - 0
lib/dpkg/varbuf.h

@@ -66,6 +66,7 @@ struct varbuf {
 
 void varbufinit(struct varbuf *v, size_t size);
 void varbuf_grow(struct varbuf *v, size_t need_size);
+char *varbuf_detach(struct varbuf *v);
 void varbufreset(struct varbuf *v);
 void varbuffree(struct varbuf *v);