src/utils_array.[ch]: Implement "array_sort".
[collection4.git] / src / utils_array.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <errno.h>
6
7 #include "utils_array.h"
8
9 struct str_array_s
10 {
11   char **ptr;
12   size_t size;
13 };
14
15 static int sort_callback (const void *v0, const void *v1) /* {{{ */
16 {
17   const char *c0 = v0;
18   const char *c1 = v1;
19
20   return (strcmp (c0, c1));
21 } /* }}} int sort_callback */
22
23 str_array_t *array_create (void) /* {{{ */
24 {
25   str_array_t *a;
26
27   a = malloc (sizeof (*a));
28   if (a == NULL)
29     return (NULL);
30
31   memset (a, 0, sizeof (*a));
32   a->ptr = NULL;
33   a->size = 0;
34
35   return (a);
36 } /* }}} str_array_t *array_create */
37
38 void array_destroy (str_array_t *a) /* {{{ */
39 {
40   if (a == NULL)
41     return;
42
43   free (a->ptr);
44   a->ptr = NULL;
45   a->size = 0;
46
47   free (a);
48 } /* }}} void array_destroy */
49
50 int array_append (str_array_t *a, const char *entry) /* {{{ */
51 {
52   char **ptr;
53
54   if ((entry == NULL) || (a == NULL))
55     return (EINVAL);
56
57   ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
58   if (ptr == NULL)
59     return (ENOMEM);
60   a->ptr = ptr;
61   ptr = a->ptr + a->size;
62
63   *ptr = strdup (entry);
64   if (*ptr == NULL)
65     return (ENOMEM);
66
67   a->size++;
68   return (0);
69 } /* }}} int array_append */
70
71 int array_append_format (str_array_t *a, const char *format, ...) /* {{{ */
72 {
73   char buffer[1024];
74   va_list ap;
75   int status;
76
77   va_start (ap, format);
78   status = vsnprintf (buffer, sizeof (buffer), format, ap);
79   va_end(ap);
80
81   if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
82     return (ENOMEM);
83
84   return (array_append (a, buffer));
85 } /* }}} int array_append_format */
86
87 int array_sort (str_array_t *a) /* {{{ */
88 {
89   if (a == NULL)
90     return (EINVAL);
91
92   qsort (a->ptr, a->size, sizeof (*a->ptr), sort_callback);
93
94   return (0);
95 } /* }}} int array_sort */
96
97 int array_argc (str_array_t *a) /* {{{ */
98 {
99   if (a == NULL)
100     return (-1);
101
102   return ((int) a->size);
103 } /* }}} int array_argc */
104
105 char **array_argv (str_array_t *a) /* {{{ */
106 {
107   if ((a == NULL) || (a->size == 0))
108     return (NULL);
109
110   return (a->ptr);
111 } /* }}} char **array_argv */
112
113 /* vim: set sw=2 sts=2 et fdm=marker : */