Mercurial > trustbridge
comparison common/strhelp.c @ 59:3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
arrays and to have a terminating malloc / realloc
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 18 Mar 2014 10:04:30 +0000 |
parents | |
children | 6acb1dae6185 |
comparison
equal
deleted
inserted
replaced
58:ad61489ce593 | 59:3f6378647371 |
---|---|
1 | |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <string.h> | |
5 #include <assert.h> | |
6 | |
7 static void | |
8 out_of_core(void) | |
9 { | |
10 fputs("\nfatal: out of memory\n", stderr); | |
11 exit(2); | |
12 } | |
13 void * | |
14 xmalloc( size_t n ) | |
15 { | |
16 void *p = malloc( n ); | |
17 if( !p ) | |
18 out_of_core(); | |
19 return p; | |
20 } | |
21 | |
22 void * | |
23 xrealloc( void *a, size_t n ) | |
24 { | |
25 void *p = realloc( a, n ); | |
26 if( !p ) | |
27 out_of_core(); | |
28 return p; | |
29 } | |
30 | |
31 char * | |
32 xstrdup( const char *string ) | |
33 { | |
34 void *p = xmalloc( strlen(string)+1 ); | |
35 strcpy( p, string ); | |
36 return p; | |
37 } | |
38 | |
39 | |
40 /** | |
41 * strv_length: | |
42 * @str_array: a %NULL-terminated array of strings | |
43 * | |
44 * Returns the length of the given %NULL-terminated | |
45 * string array @str_array. | |
46 * | |
47 * Return value: length of @str_array. | |
48 * | |
49 */ | |
50 unsigned int | |
51 strv_length (char **str_array) | |
52 { | |
53 unsigned int i = 0; | |
54 | |
55 if (!str_array) | |
56 return 0; | |
57 | |
58 while (str_array[i]) | |
59 ++i; | |
60 | |
61 return i; | |
62 } | |
63 | |
64 /* @brief append a string to a NULL terminated array of strings. | |
65 * | |
66 * @param[inout] array pointer to the NULL terminated list of string pointers. | |
67 * @param[in] string pointer to the string to append to the list. | |
68 * */ | |
69 void array_append_str(char ***pArray, const char *string) | |
70 { | |
71 unsigned int old_len = strv_length(*pArray); | |
72 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2)); | |
73 | |
74 *pArray[old_len] = xstrdup(string); | |
75 *pArray[old_len + 1] = NULL; | |
76 } | |
77 | |
78 /* @brief append a string to another string. | |
79 * | |
80 * @param[inout] pDst pointer to the string to be extended. | |
81 * @param[in] appendage pointer to the string to append. | |
82 * */ | |
83 void str_append_str(char **pDst, const char *appendage) | |
84 { | |
85 size_t old_len = strlen(*pDst), | |
86 added_len = strlen(appendage); | |
87 size_t new_len = old_len + added_len + 1; | |
88 | |
89 if (!appendage) | |
90 return; | |
91 | |
92 *pDst = (char *)xrealloc(*pDst, sizeof(char) * (new_len)); | |
93 | |
94 strcpy(*pDst + old_len, appendage); | |
95 | |
96 *pDst[new_len - 1] = '\0'; | |
97 } | |
98 | |
99 void | |
100 strfreev (char **str_array) | |
101 { | |
102 if (str_array) | |
103 { | |
104 int i; | |
105 | |
106 for (i = 0; str_array[i] != NULL; i++) | |
107 free (str_array[i]); | |
108 | |
109 free (str_array); | |
110 } | |
111 } |