7 #include "utils_array.h"
15 static int sort_callback (const void *v0, const void *v1) /* {{{ */
20 return (strcmp (c0, c1));
21 } /* }}} int sort_callback */
23 str_array_t *array_create (void) /* {{{ */
27 a = malloc (sizeof (*a));
31 memset (a, 0, sizeof (*a));
36 } /* }}} str_array_t *array_create */
38 void array_destroy (str_array_t *a) /* {{{ */
48 } /* }}} void array_destroy */
50 int array_append (str_array_t *a, const char *entry) /* {{{ */
54 if ((entry == NULL) || (a == NULL))
57 ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
61 ptr = a->ptr + a->size;
63 *ptr = strdup (entry);
69 } /* }}} int array_append */
71 int array_append_format (str_array_t *a, const char *format, ...) /* {{{ */
77 va_start (ap, format);
78 status = vsnprintf (buffer, sizeof (buffer), format, ap);
81 if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
84 return (array_append (a, buffer));
85 } /* }}} int array_append_format */
87 int array_prepend (str_array_t *a, const char *entry) /* {{{ */
92 if ((entry == NULL) || (a == NULL))
99 ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
107 memmove (a->ptr + 1, a->ptr, sizeof (*a->ptr) * a->size);
112 } /* }}} int array_prepend */
114 int array_prepend_format (str_array_t *a, const char *format, ...) /* {{{ */
120 va_start (ap, format);
121 status = vsnprintf (buffer, sizeof (buffer), format, ap);
124 if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
127 return (array_prepend (a, buffer));
128 } /* }}} int array_prepend_format */
130 int array_sort (str_array_t *a) /* {{{ */
135 qsort (a->ptr, a->size, sizeof (*a->ptr), sort_callback);
138 } /* }}} int array_sort */
140 int array_argc (str_array_t *a) /* {{{ */
145 return ((int) a->size);
146 } /* }}} int array_argc */
148 char **array_argv (str_array_t *a) /* {{{ */
150 if ((a == NULL) || (a->size == 0))
154 } /* }}} char **array_argv */
156 /* vim: set sw=2 sts=2 et fdm=marker : */