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

libdpkg: Add new varbuf_get_str()

Guillem Jover лет назад: 10
Родитель
Сommit
e7a32556af
3 измененных файлов с 43 добавлено и 4 удалено
  1. 32 2
      lib/dpkg/t/t-varbuf.c
  2. 9 1
      lib/dpkg/varbuf.c
  3. 2 1
      lib/dpkg/varbuf.h

+ 32 - 2
lib/dpkg/t/t-varbuf.c

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * t-verbuf.c - test varbuf implementation
  *
- * Copyright © 2009-2011 Guillem Jover <guillem@debian.org>
+ * Copyright © 2009-2011, 2013-2015 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -222,6 +222,35 @@ test_varbuf_end_str(void)
 	varbuf_destroy(&vb);
 }
 
+static void
+test_varbuf_get_str(void)
+{
+	struct varbuf vb;
+	const char *str;
+
+	varbuf_init(&vb, 10);
+
+	varbuf_add_buf(&vb, "1234567890", 10);
+	str = varbuf_get_str(&vb);
+	test_pass(vb.buf == str);
+	test_pass(vb.used == 10);
+	test_pass(vb.buf[vb.used] == '\0');
+	test_pass(str[vb.used] == '\0');
+	test_str(vb.buf, ==, "1234567890");
+	test_str(str, ==, "1234567890");
+
+	varbuf_add_buf(&vb, "abcde", 5);
+	str = varbuf_get_str(&vb);
+	test_pass(vb.buf == str);
+	test_pass(vb.used == 15);
+	test_pass(vb.buf[vb.used] == '\0');
+	test_pass(str[vb.used] == '\0');
+	test_str(vb.buf, ==, "1234567890abcde");
+	test_str(str, ==, "1234567890abcde");
+
+	varbuf_destroy(&vb);
+}
+
 static void
 test_varbuf_printf(void)
 {
@@ -322,7 +351,7 @@ test_varbuf_detach(void)
 static void
 test(void)
 {
-	test_plan(108);
+	test_plan(120);
 
 	test_varbuf_init();
 	test_varbuf_prealloc();
@@ -333,6 +362,7 @@ test(void)
 	test_varbuf_dup_char();
 	test_varbuf_map_char();
 	test_varbuf_end_str();
+	test_varbuf_get_str();
 	test_varbuf_printf();
 	test_varbuf_reset();
 	test_varbuf_snapshot();

+ 9 - 1
lib/dpkg/varbuf.c

@@ -3,7 +3,7 @@
  * varbuf.c - variable length expandable buffer handling
  *
  * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
- * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -107,6 +107,14 @@ varbuf_end_str(struct varbuf *v)
   v->buf[v->used] = '\0';
 }
 
+const char *
+varbuf_get_str(struct varbuf *v)
+{
+  varbuf_end_str(v);
+
+  return v->buf;
+}
+
 void
 varbuf_init(struct varbuf *v, size_t size)
 {

+ 2 - 1
lib/dpkg/varbuf.h

@@ -3,7 +3,7 @@
  * varbuf.h - variable length expandable buffer handling
  *
  * Copyright © 1994, 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
- * Copyright © 2008-2011 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -84,6 +84,7 @@ void varbuf_map_char(struct varbuf *v, int c_src, int c_dst);
 #define varbuf_add_str(v, s) varbuf_add_buf(v, s, strlen(s))
 void varbuf_add_buf(struct varbuf *v, const void *s, size_t size);
 void varbuf_end_str(struct varbuf *v);
+const char *varbuf_get_str(struct varbuf *v);
 
 int varbuf_printf(struct varbuf *v, const char *fmt, ...) DPKG_ATTR_PRINTF(2);
 int varbuf_vprintf(struct varbuf *v, const char *fmt, va_list va)