Mercurial > trustbridge
comparison common/strhelp.c @ 60:6acb1dae6185
Use strn functions and improve error handling.
Even if we know the strings are NULL terminated we use
length terminated string functions after a first strlen
this makes it easier to assert that at one point we know
the string is terminated and afterwards use the length
of that.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 18 Mar 2014 11:28:02 +0000 |
parents | 3f6378647371 |
children | 355800cdefcc |
comparison
equal
deleted
inserted
replaced
59:3f6378647371 | 60:6acb1dae6185 |
---|---|
27 out_of_core(); | 27 out_of_core(); |
28 return p; | 28 return p; |
29 } | 29 } |
30 | 30 |
31 char * | 31 char * |
32 xstrdup( const char *string ) | 32 xstrndup( const char *string, const size_t len ) |
33 { | 33 { |
34 void *p = xmalloc( strlen(string)+1 ); | 34 char *p = xmalloc( len + 1 ); |
35 strcpy( p, string ); | 35 memcpy( p, string, len ); |
36 p[len] = '\0'; | |
36 return p; | 37 return p; |
37 } | 38 } |
38 | 39 |
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 | 40 unsigned int |
51 strv_length (char **str_array) | 41 strv_length (char **str_array) |
52 { | 42 { |
53 unsigned int i = 0; | 43 unsigned int i = 0; |
54 | 44 |
59 ++i; | 49 ++i; |
60 | 50 |
61 return i; | 51 return i; |
62 } | 52 } |
63 | 53 |
64 /* @brief append a string to a NULL terminated array of strings. | 54 void array_append_str(char ***pArray, const char *string, const size_t len) |
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 { | 55 { |
71 unsigned int old_len = strv_length(*pArray); | 56 unsigned int old_len = 0; |
57 | |
58 if (!*pArray) { | |
59 *pArray = xmalloc(2 * sizeof(char*)); | |
60 (*pArray)[0] = xstrndup(string, len); | |
61 (*pArray)[1] = NULL; | |
62 return; | |
63 } | |
64 old_len = strv_length(*pArray); | |
72 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2)); | 65 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2)); |
73 | 66 |
74 *pArray[old_len] = xstrdup(string); | 67 (*pArray)[old_len] = xstrndup(string, len); |
75 *pArray[old_len + 1] = NULL; | 68 (*pArray)[old_len + 1] = NULL; |
76 } | 69 } |
77 | 70 |
78 /* @brief append a string to another string. | 71 void str_append_str(char **pDst, const char *appendage, const size_t len) |
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 { | 72 { |
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) | 73 if (!appendage) |
90 return; | 74 return; |
91 | 75 |
92 *pDst = (char *)xrealloc(*pDst, sizeof(char) * (new_len)); | 76 if (!*pDst) { |
93 | 77 *pDst = xstrndup(appendage, len); |
94 strcpy(*pDst + old_len, appendage); | 78 } else { |
95 | 79 size_t old_len = strlen(*pDst); |
96 *pDst[new_len - 1] = '\0'; | 80 size_t new_len = old_len + len + 1; |
81 *pDst = (char *)xrealloc(*pDst, sizeof(char) * (new_len)); | |
82 strncpy(*pDst + old_len, appendage, new_len); | |
83 *pDst[new_len] = '\0'; | |
84 } | |
97 } | 85 } |
98 | 86 |
99 void | 87 void |
100 strfreev (char **str_array) | 88 strfreev (char **str_array) |
101 { | 89 { |