X-Git-Url: https://git.octo.it/?p=collection4.git;a=blobdiff_plain;f=src%2Futils_array.c;h=2d6250efd634bc90e542d030a7d6336788fb2f75;hp=5aaabbc5c1d35d38e96ffef8af099775f7a0ed32;hb=2ef734476e3511b6f1c2611875e340b6b8ca2d4c;hpb=2c85f876c1b9d5c161694cfb03cf20773b74c844 diff --git a/src/utils_array.c b/src/utils_array.c index 5aaabbc..2d6250e 100644 --- a/src/utils_array.c +++ b/src/utils_array.c @@ -1,3 +1,26 @@ +/** + * collection4 - utils_array.c + * Copyright (C) 2010 Florian octo Forster + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + #include #include #include @@ -12,6 +35,14 @@ struct str_array_s size_t size; }; +static int sort_callback (const void *v0, const void *v1) /* {{{ */ +{ + const char *c0 = v0; + const char *c1 = v1; + + return (strcmp (c0, c1)); +} /* }}} int sort_callback */ + str_array_t *array_create (void) /* {{{ */ { str_array_t *a; @@ -76,6 +107,59 @@ int array_append_format (str_array_t *a, const char *format, ...) /* {{{ */ return (array_append (a, buffer)); } /* }}} int array_append_format */ +int array_prepend (str_array_t *a, const char *entry) /* {{{ */ +{ + char **ptr; + char *cpy; + + if ((entry == NULL) || (a == NULL)) + return (EINVAL); + + cpy = strdup (entry); + if (cpy == NULL) + return (ENOMEM); + + ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1)); + if (ptr == NULL) + { + free (cpy); + return (ENOMEM); + } + a->ptr = ptr; + + memmove (a->ptr + 1, a->ptr, sizeof (*a->ptr) * a->size); + a->ptr[0] = cpy; + a->size++; + + return (0); +} /* }}} int array_prepend */ + +int array_prepend_format (str_array_t *a, const char *format, ...) /* {{{ */ +{ + char buffer[1024]; + va_list ap; + int status; + + va_start (ap, format); + status = vsnprintf (buffer, sizeof (buffer), format, ap); + va_end(ap); + + if ((status < 0) || (((size_t) status) >= sizeof (buffer))) + return (ENOMEM); + + return (array_prepend (a, buffer)); +} /* }}} int array_prepend_format */ + +int array_sort (str_array_t *a) /* {{{ */ +{ + if (a == NULL) + return (EINVAL); + + qsort (a->ptr, a->size, sizeof (*a->ptr), sort_callback); + + return (0); +} /* }}} int array_sort */ + int array_argc (str_array_t *a) /* {{{ */ { if (a == NULL)