2
Copyright (C) 2011- The University of Notre Dame
3
This software is distributed under the GNU General Public License.
4
See the file COPYING for details.
14
<NULL terminated array of char *>
15
<size_t size of entire memory block>
19
#define DEFAULT_SIZE (sizeof(char *) + sizeof(size_t))
21
char **string_array_new (void)
23
char **data = (char **) xxrealloc(NULL, DEFAULT_SIZE);
25
size_t *length = (size_t *) data+1;
26
*length = DEFAULT_SIZE;
30
char **string_array_append (char **oarray, char *str)
33
for (tmp = oarray; *tmp; tmp++) ;
34
tmp++; /* advance past NULL pointer */
35
size_t olength = *((size_t *) tmp);
36
size_t nlength = olength + strlen(str)+1 + sizeof(char *);
37
narray = xxrealloc(oarray, nlength);
38
ptrdiff_t offset = ((void *)narray)-((void *)oarray)+sizeof(char *); /* difference including extra pointer */
39
for (tmp = narray; *tmp; tmp++)
40
*tmp = ((void *)*tmp)+offset; /* correct the address */
41
*tmp = (char *) (((void *)narray)+olength+sizeof(char *)); /* set to new string location */
43
tmp++; /* now points to the old data length */
44
memmove(((void *)tmp)+sizeof(char *), tmp, olength-(((void *)tmp)-((void *)narray))); /* careful with pointer arithmetic */
45
*tmp = NULL; /* set NULL terminated final entry */
47
*((size_t *) tmp) = nlength; /* set the new length */