~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/string_array.c

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2012-03-30 12:40:01 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120330124001-ze0lhxm5uwq2e3mo
Tags: 3.4.2-2
Added patch to handle a missing CFLAGS variable in Python's sysconfig
report (Closes: #661658).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
5
*/
 
6
 
 
7
#include "xmalloc.h"
 
8
 
 
9
#include <assert.h>
 
10
#include <stddef.h>
 
11
#include <string.h>
 
12
 
 
13
/* Format:
 
14
   <NULL terminated array of char *>
 
15
   <size_t size of entire memory block>
 
16
   <data>
 
17
*/
 
18
 
 
19
#define DEFAULT_SIZE  (sizeof(char *) + sizeof(size_t))
 
20
 
 
21
char **string_array_new (void)
 
22
{
 
23
        char **data = (char **) xxrealloc(NULL, DEFAULT_SIZE);
 
24
        *data = NULL;
 
25
        size_t *length = (size_t *) data+1;
 
26
        *length = DEFAULT_SIZE;
 
27
        return data;
 
28
}
 
29
 
 
30
char **string_array_append (char **oarray, char *str)
 
31
{
 
32
        char **narray, **tmp;
 
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 */
 
42
        strcpy(*tmp, str);
 
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 */
 
46
        tmp++;
 
47
        *((size_t *) tmp) = nlength; /* set the new length */
 
48
        return narray;
 
49
}