2 * collection4 - utils_array.c
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
30 #include "utils_array.h"
38 static int sort_callback (const void *v0, const void *v1) /* {{{ */
43 return (strcmp (c0, c1));
44 } /* }}} int sort_callback */
46 str_array_t *array_create (void) /* {{{ */
50 a = malloc (sizeof (*a));
54 memset (a, 0, sizeof (*a));
59 } /* }}} str_array_t *array_create */
61 void array_destroy (str_array_t *a) /* {{{ */
71 } /* }}} void array_destroy */
73 int array_append (str_array_t *a, const char *entry) /* {{{ */
77 if ((entry == NULL) || (a == NULL))
80 ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
84 ptr = a->ptr + a->size;
86 *ptr = strdup (entry);
92 } /* }}} int array_append */
94 int array_append_format (str_array_t *a, const char *format, ...) /* {{{ */
100 va_start (ap, format);
101 status = vsnprintf (buffer, sizeof (buffer), format, ap);
104 if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
107 return (array_append (a, buffer));
108 } /* }}} int array_append_format */
110 int array_prepend (str_array_t *a, const char *entry) /* {{{ */
115 if ((entry == NULL) || (a == NULL))
118 cpy = strdup (entry);
122 ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
130 memmove (a->ptr + 1, a->ptr, sizeof (*a->ptr) * a->size);
135 } /* }}} int array_prepend */
137 int array_prepend_format (str_array_t *a, const char *format, ...) /* {{{ */
143 va_start (ap, format);
144 status = vsnprintf (buffer, sizeof (buffer), format, ap);
147 if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
150 return (array_prepend (a, buffer));
151 } /* }}} int array_prepend_format */
153 int array_sort (str_array_t *a) /* {{{ */
158 qsort (a->ptr, a->size, sizeof (*a->ptr), sort_callback);
161 } /* }}} int array_sort */
163 int array_argc (str_array_t *a) /* {{{ */
168 return ((int) a->size);
169 } /* }}} int array_argc */
171 char **array_argv (str_array_t *a) /* {{{ */
173 if ((a == NULL) || (a->size == 0))
177 } /* }}} char **array_argv */
179 /* vim: set sw=2 sts=2 et fdm=marker : */