[ccan] [PATCH 5/5] darray: add darray_char sprintf work-alike (and some helpers)
Cody P Schafer
dev at codyps.com
Mon Aug 17 09:22:59 AEST 2015
From: Cody P Schafer <jmesmon at gmail.com>
CC: Joseph Adams <joeyadams3.14159 at gmail.com>
Signed-off-by: Cody P Schafer <dev at codyps.com>
---
ccan/darray/darray.h | 34 +++++++++++++++++++++++++++++++---
1 file changed, 31 insertions(+), 3 deletions(-)
diff --git a/ccan/darray/darray.h b/ccan/darray/darray.h
index f488eec..6a076b3 100644
--- a/ccan/darray/darray.h
+++ b/ccan/darray/darray.h
@@ -119,7 +119,11 @@
#define darray_new() {0,0,0}
#define darray_init(arr) do {(arr).item=0; (arr).size=0; (arr).alloc=0;} while(0)
#define darray_free(arr) do {free((arr).item);} while(0)
-
+#define darray_reset(arr) do { \
+ darray_free(arr); \
+ darray_init(arr); \
+} while (0)
+#define darray_clear(arr) do { (arr).size = 0; } while (0)
/*
* Typedefs for darrays of common types. These are useful
@@ -218,6 +222,23 @@ typedef darray(unsigned long) darray_ulong;
} while(0)
+/*** Insertion for darray_char ***/
+/* How does null termination work?
+ * - we always allocate space for the null terminator
+ * - sprintf will always write it out
+ * - we just don't count the space it takes (in arr.size) unless the user
+ * requests it.
+ */
+#define darray_printf(arr, ...) do { \
+ darray_make_space(arr, snprintf(NULL, 0, __VA_ARGS__) + 1); \
+ (arr).size += sprintf(darray_space(arr), __VA_ARGS__); \
+ } while(0)
+
+#define darray_printf_nullterminate(arr, ...) do { \
+ darray_printf(arr, __VA_ARGS__); \
+ (arr).size += 1; \
+ } while(0)
+
/*** Removal ***/
/* Warning: Do not call darray_pop on an empty darray. */
@@ -274,8 +295,15 @@ typedef darray(unsigned long) darray_ulong;
darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
} while(0)
-#if HAVE_STATEMENT_EXPR==1
-#define darray_make_room(arr, room) ({size_t newAlloc = (arr).size+(room); if ((arr).alloc<newAlloc) darray_realloc(arr, newAlloc); (arr).item+(arr).size; })
+#define darray_make_space(arr, delta) darray_growalloc(arr, (delta) + (arr).size)
+#define darray_space_size(arr) ((arr).alloc - (arr).size)
+#define darray_space(arr) ((arr).item + (arr).size)
+
+#if HAVE_STATEMENT_EXPR
+#define darray_make_room(arr, room) ({ \
+ darray_make_space(arr, room); \
+ darray_space(arr); \
+ })
#endif
static inline size_t darray_next_alloc(size_t alloc, size_t need)
--
2.5.0
More information about the ccan
mailing list