src/graph_instance.[ch]: Implement "inst_data_to_json".
[collection4.git] / src / utils_array.c
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  **/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include "utils_array.h"
31
32 struct str_array_s
33 {
34   char **ptr;
35   size_t size;
36 };
37
38 static int sort_callback (const void *v0, const void *v1) /* {{{ */
39 {
40   const char *c0 = v0;
41   const char *c1 = v1;
42
43   return (strcmp (c0, c1));
44 } /* }}} int sort_callback */
45
46 str_array_t *array_create (void) /* {{{ */
47 {
48   str_array_t *a;
49
50   a = malloc (sizeof (*a));
51   if (a == NULL)
52     return (NULL);
53
54   memset (a, 0, sizeof (*a));
55   a->ptr = NULL;
56   a->size = 0;
57
58   return (a);
59 } /* }}} str_array_t *array_create */
60
61 void array_destroy (str_array_t *a) /* {{{ */
62 {
63   if (a == NULL)
64     return;
65
66   free (a->ptr);
67   a->ptr = NULL;
68   a->size = 0;
69
70   free (a);
71 } /* }}} void array_destroy */
72
73 int array_append (str_array_t *a, const char *entry) /* {{{ */
74 {
75   char **ptr;
76
77   if ((entry == NULL) || (a == NULL))
78     return (EINVAL);
79
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;
85
86   *ptr = strdup (entry);
87   if (*ptr == NULL)
88     return (ENOMEM);
89
90   a->size++;
91   return (0);
92 } /* }}} int array_append */
93
94 int array_append_format (str_array_t *a, const char *format, ...) /* {{{ */
95 {
96   char buffer[1024];
97   va_list ap;
98   int status;
99
100   va_start (ap, format);
101   status = vsnprintf (buffer, sizeof (buffer), format, ap);
102   va_end(ap);
103
104   if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
105     return (ENOMEM);
106
107   return (array_append (a, buffer));
108 } /* }}} int array_append_format */
109
110 int array_prepend (str_array_t *a, const char *entry) /* {{{ */
111 {
112   char **ptr;
113   char *cpy;
114
115   if ((entry == NULL) || (a == NULL))
116     return (EINVAL);
117
118   cpy = strdup (entry);
119   if (cpy == NULL)
120     return (ENOMEM);
121
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;
129
130   memmove (a->ptr + 1, a->ptr, sizeof (*a->ptr) * a->size);
131   a->ptr[0] = cpy;
132   a->size++;
133
134   return (0);
135 } /* }}} int array_prepend */
136
137 int array_prepend_format (str_array_t *a, const char *format, ...) /* {{{ */
138 {
139   char buffer[1024];
140   va_list ap;
141   int status;
142
143   va_start (ap, format);
144   status = vsnprintf (buffer, sizeof (buffer), format, ap);
145   va_end(ap);
146
147   if ((status < 0) || (((size_t) status) >= sizeof (buffer)))
148     return (ENOMEM);
149
150   return (array_prepend (a, buffer));
151 } /* }}} int array_prepend_format */
152
153 int array_sort (str_array_t *a) /* {{{ */
154 {
155   if (a == NULL)
156     return (EINVAL);
157
158   qsort (a->ptr, a->size, sizeof (*a->ptr), sort_callback);
159
160   return (0);
161 } /* }}} int array_sort */
162
163 int array_argc (str_array_t *a) /* {{{ */
164 {
165   if (a == NULL)
166     return (-1);
167
168   return ((int) a->size);
169 } /* }}} int array_argc */
170
171 char **array_argv (str_array_t *a) /* {{{ */
172 {
173   if ((a == NULL) || (a->size == 0))
174     return (NULL);
175
176   return (a->ptr);
177 } /* }}} char **array_argv */
178
179 /* vim: set sw=2 sts=2 et fdm=marker : */