From 37e7f29d384c1eeda8c7bcb850e9c916303ffb8d Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 15 Jun 2010 09:08:19 +0200 Subject: [PATCH] Add forgotten files. --- utils_array.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ utils_array.h | 17 +++++++++ 2 files changed, 112 insertions(+) create mode 100644 utils_array.c create mode 100644 utils_array.h diff --git a/utils_array.c b/utils_array.c new file mode 100644 index 0000000..5aaabbc --- /dev/null +++ b/utils_array.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +#include "utils_array.h" + +struct str_array_s +{ + char **ptr; + size_t size; +}; + +str_array_t *array_create (void) /* {{{ */ +{ + str_array_t *a; + + a = malloc (sizeof (*a)); + if (a == NULL) + return (NULL); + + memset (a, 0, sizeof (*a)); + a->ptr = NULL; + a->size = 0; + + return (a); +} /* }}} str_array_t *array_create */ + +void array_destroy (str_array_t *a) /* {{{ */ +{ + if (a == NULL) + return; + + free (a->ptr); + a->ptr = NULL; + a->size = 0; + + free (a); +} /* }}} void array_destroy */ + +int array_append (str_array_t *a, const char *entry) /* {{{ */ +{ + char **ptr; + + if ((entry == NULL) || (a == NULL)) + return (EINVAL); + + ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1)); + if (ptr == NULL) + return (ENOMEM); + a->ptr = ptr; + ptr = a->ptr + a->size; + + *ptr = strdup (entry); + if (*ptr == NULL) + return (ENOMEM); + + a->size++; + return (0); +} /* }}} int array_append */ + +int array_append_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_append (a, buffer)); +} /* }}} int array_append_format */ + +int array_argc (str_array_t *a) /* {{{ */ +{ + if (a == NULL) + return (-1); + + return ((int) a->size); +} /* }}} int array_argc */ + +char **array_argv (str_array_t *a) /* {{{ */ +{ + if ((a == NULL) || (a->size == 0)) + return (NULL); + + return (a->ptr); +} /* }}} char **array_argv */ + +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/utils_array.h b/utils_array.h new file mode 100644 index 0000000..917bea9 --- /dev/null +++ b/utils_array.h @@ -0,0 +1,17 @@ +#ifndef UTILS_ARRAY_H +#define UTILS_ARRAY_H 1 + +struct str_array_s; +typedef struct str_array_s str_array_t; + +str_array_t *array_create (void); +void array_destroy (str_array_t *a); +int array_append (str_array_t *a, const char *entry); +int array_append_format (str_array_t *a, const char *format, ...) + __attribute__((format(printf,2,3))); + +int array_argc (str_array_t *); +char **array_argv (str_array_t *); + +#endif /* UTILS_ARRAY_H */ +/* vim: set sw=2 sts=2 et fdm=marker : */ -- 2.30.2