1 /**
2 * collection4 - utils_array.c
3 * Copyright (C) 2010 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <ff at octo.it>
22 **/
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <errno.h>
30 #include "utils_array.h"
32 struct str_array_s
33 {
34 char **ptr;
35 size_t size;
36 };
38 static int sort_callback (const void *v0, const void *v1) /* {{{ */
39 {
40 const char *c0 = v0;
41 const char *c1 = v1;
43 return (strcmp (c0, c1));
44 } /* }}} int sort_callback */
46 str_array_t *array_create (void) /* {{{ */
47 {
48 str_array_t *a;
50 a = malloc (sizeof (*a));
51 if (a == NULL)
52 return (NULL);
54 memset (a, 0, sizeof (*a));
55 a->ptr = NULL;
56 a->size = 0;
58 return (a);
59 } /* }}} str_array_t *array_create */
61 void array_destroy (str_array_t *a) /* {{{ */
62 {
63 if (a == NULL)
64 return;
66 free (a->ptr);
67 a->ptr = NULL;
68 a->size = 0;
70 free (a);
71 } /* }}} void array_destroy */
73 int array_append (str_array_t *a, const char *entry) /* {{{ */
74 {
75 char **ptr;
77 if ((entry == NULL) || (a == NULL))
78 return (EINVAL);
80 ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
81 if (ptr == NULL)
82 return (ENOMEM);
83 a->ptr = ptr;
84 ptr = a->ptr + a->size;
86 *ptr = strdup (entry);
87 if (*ptr == NULL)
88 return (ENOMEM);
90 a->size++;
91 return (0);
92 } /* }}} int array_append */
94 int array_append_format (str_array_t *a, const char *format, ...) /* {{{ */
95 {
96 char buffer[1024];
97 va_list ap;
98 int status;
100 va_start (ap, format);
101 status = vsnprintf (buffer, sizeof (buffer), format, ap);
102 va_end(ap);
104 if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
105 return (ENOMEM);
107 return (array_append (a, buffer));
108 } /* }}} int array_append_format */
110 int array_prepend (str_array_t *a, const char *entry) /* {{{ */
111 {
112 char **ptr;
113 char *cpy;
115 if ((entry == NULL) || (a == NULL))
116 return (EINVAL);
118 cpy = strdup (entry);
119 if (cpy == NULL)
120 return (ENOMEM);
122 ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
123 if (ptr == NULL)
124 {
125 free (cpy);
126 return (ENOMEM);
127 }
128 a->ptr = ptr;
130 memmove (a->ptr + 1, a->ptr, sizeof (*a->ptr) * a->size);
131 a->ptr[0] = cpy;
132 a->size++;
134 return (0);
135 } /* }}} int array_prepend */
137 int array_prepend_format (str_array_t *a, const char *format, ...) /* {{{ */
138 {
139 char buffer[1024];
140 va_list ap;
141 int status;
143 va_start (ap, format);
144 status = vsnprintf (buffer, sizeof (buffer), format, ap);
145 va_end(ap);
147 if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
148 return (ENOMEM);
150 return (array_prepend (a, buffer));
151 } /* }}} int array_prepend_format */
153 int array_sort (str_array_t *a) /* {{{ */
154 {
155 if (a == NULL)
156 return (EINVAL);
158 qsort (a->ptr, a->size, sizeof (*a->ptr), sort_callback);
160 return (0);
161 } /* }}} int array_sort */
163 int array_argc (str_array_t *a) /* {{{ */
164 {
165 if (a == NULL)
166 return (-1);
168 return ((int) a->size);
169 } /* }}} int array_argc */
171 char **array_argv (str_array_t *a) /* {{{ */
172 {
173 if ((a == NULL) || (a->size == 0))
174 return (NULL);
176 return (a->ptr);
177 } /* }}} char **array_argv */
179 /* vim: set sw=2 sts=2 et fdm=marker : */