Add some functions to work with strings and arrays of pointers that will
be useful later.

Signed-off-by: Miroslav Lichvar <mlich...@redhat.com>
---
 util.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 util.h | 57 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 152 insertions(+)

diff --git a/util.c b/util.c
index cb428b1..06c3296 100644
--- a/util.c
+++ b/util.c
@@ -18,6 +18,7 @@
  */
 #include <errno.h>
 #include <signal.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -342,3 +343,97 @@ int is_running(void)
 {
        return running;
 }
+
+char *string_newf(const char *format, ...)
+{
+       va_list ap;
+       char *s;
+
+       va_start(ap, format);
+       if (vasprintf(&s, format, ap) < 0)
+               s = NULL;
+       va_end(ap);
+
+       return s;
+}
+
+void string_append(char **s, const char *str)
+{
+       size_t len1, len2;
+
+       len1 = strlen(*s);
+       len2 = strlen(str);
+       *s = realloc(*s, len1 + len2 + 1);
+       if (*s)
+               memcpy((*s) + len1, str, len2 + 1);
+}
+
+void string_appendf(char **s, const char *format, ...)
+{
+       va_list ap;
+       size_t len1, len2;
+       char *s2;
+
+       len1 = strlen(*s);
+
+       va_start(ap, format);
+       len2 = vasprintf(&s2, format, ap);
+       va_end(ap);
+
+       if (len2 < 0) {
+               *s = NULL;
+               return;
+       }
+
+       *s = realloc(*s, len1 + len2 + 1);
+       if (*s)
+               memcpy((*s) + len1, s2, len2 + 1);
+       free(s2);
+}
+
+void **parray_new(void)
+{
+       void **a = malloc(sizeof(*a));
+
+       if (a)
+               *a = NULL;
+       return a;
+}
+
+void parray_append(void ***a, void *p)
+{
+       parray_extend(a, p, NULL);
+}
+
+void parray_extend(void ***a, ...)
+{
+       va_list ap;
+       int ilen, len, alloced;
+       void *p;
+
+       for (len = 0; (*a)[len]; len++)
+               ;
+       len++;
+
+       va_start(ap, a);
+       for (ilen = 0; va_arg(ap, void *); ilen++)
+               ;
+       va_end(ap);
+
+       /* Reallocate in exponentially increasing sizes. */
+       for (alloced = 1; alloced < len; alloced <<= 1)
+               ;
+       if (alloced < len + ilen) {
+               while (alloced < len + ilen)
+                       alloced *= 2;
+               *a = realloc(*a, alloced * sizeof **a);
+               if (!*a)
+                       return;
+       }
+
+       va_start(ap, a);
+       while ((p = va_arg(ap, void *)))
+               (*a)[len++ - 1] = p;
+       va_end(ap);
+       (*a)[len - 1] = NULL;
+}
diff --git a/util.h b/util.h
index cf05e49..c3900aa 100644
--- a/util.h
+++ b/util.h
@@ -226,4 +226,61 @@ int handle_term_signals(void);
  */
 int is_running(void);
 
+/**
+ * Get an allocated and formatted string. This is a wrapper around asprintf().
+ *
+ * @param format    printf() format string.
+ * @param ...       printf() arguments.
+ * @return          Pointer to the allocated string, NULL on error.
+ */
+#ifdef __GNUC__
+__attribute__ ((format (printf, 1, 2)))
+#endif
+char *string_newf(const char *format, ...);
+
+/**
+ * Reallocate a string and append another string to it.
+ *
+ * @param s         String that should be extended, set to NULL on error.
+ * @param str       String appended to s.
+ */
+void string_append(char **s, const char *str);
+#ifdef __GNUC__
+__attribute__ ((format (printf, 2, 3)))
+#endif
+/**
+ * Reallocate a string and append a formatted string to it.
+ *
+ * @param s         String that should be extended, set to NULL on error.
+ * @param format    printf() format string.
+ * @param ...       printf() arguments.
+ */
+void string_appendf(char **s, const char *format, ...);
+
+/**
+ * Get an empty array of pointers terminated by NULL.
+ *
+ * @return          Pointer to the allocated array, NULL on error.
+ */
+void **parray_new(void);
+
+/**
+ * Append pointer to a NULL-terminated pointer array. The array is reallocated
+ * in exponentially increasing sizes.
+ *
+ * @param a         Pointer to pointer array, set to NULL on error.
+ * @param p         Pointer appended to the array.
+ */
+void parray_append(void ***a, void *p);
+
+
+/**
+ * Append pointers to a NULL-terminated pointer array. The array is reallocated
+ * in exponentially increasing sizes.
+ *
+ * @param a         Pointer to pointer array, set to NULL on error.
+ * @param ...       NULL-terminated list of pointers.
+ */
+void parray_extend(void ***a, ...);
+
 #endif
-- 
1.9.3


------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to