Mercurial > trustbridge
comparison common/strhelp.c @ 91:80ab2168760f
Also add output size handling to str_append_str
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 21 Mar 2014 09:47:05 +0000 |
parents | 355800cdefcc |
children | c602d8cfa619 |
comparison
equal
deleted
inserted
replaced
90:899fcddb92d0 | 91:80ab2168760f |
---|---|
66 | 66 |
67 (*pArray)[old_len] = xstrndup(string, len); | 67 (*pArray)[old_len] = xstrndup(string, len); |
68 (*pArray)[old_len + 1] = NULL; | 68 (*pArray)[old_len + 1] = NULL; |
69 } | 69 } |
70 | 70 |
71 void str_append_str(char **pDst, const char *appendage, const size_t len) | 71 void |
72 str_append_str(char **pDst, size_t *dst_len, const char *appendage, const size_t len) | |
72 { | 73 { |
73 if (!appendage) | 74 if (!appendage) |
74 return; | 75 return; |
75 | 76 |
76 if (!*pDst) { | 77 if (!(*pDst)) { |
77 *pDst = xstrndup(appendage, len); | 78 *pDst = xstrndup(appendage, len); |
79 *dst_len = len; | |
78 } else { | 80 } else { |
79 size_t old_len = strlen(*pDst); | 81 size_t new_size = (*dst_len) + len + 1; |
80 size_t new_len = old_len + len + 1; | 82 char *p_old = *pDst; |
81 *pDst = (char *)xrealloc(*pDst, sizeof(char) * (new_len + 1)); | 83 *pDst = xmalloc(new_size); |
82 strncpy(*pDst + old_len, appendage, len); | 84 strncpy(*pDst, p_old, *dst_len); |
83 (*pDst)[new_len] = '\0'; | 85 strncpy(*pDst + *dst_len, appendage, len); |
86 *dst_len = new_size - 1; | |
87 (*pDst)[*dst_len] = '\0'; | |
88 free (p_old); | |
84 } | 89 } |
85 } | 90 } |
86 | 91 |
87 void | 92 void |
88 strfreev (char **str_array) | 93 strfreev (char **str_array) |