comparison common/strhelp.c @ 905:698b6a9bd75e

Fix coding style for C code astyle --style=gnu --indent=spaces=2
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 13 Aug 2014 15:49:47 +0200
parents 17e1c8f37d72
children ffdc8cba139a
comparison
equal deleted inserted replaced
904:f89b41fa7048 905:698b6a9bd75e
34 */ 34 */
35 35
36 static void 36 static void
37 out_of_core(void) 37 out_of_core(void)
38 { 38 {
39 fputs("\nfatal: out of memory\n", stderr); 39 fputs("\nfatal: out of memory\n", stderr);
40 exit(2); 40 exit(2);
41 } 41 }
42 void * 42 void *
43 xmalloc( size_t n ) 43 xmalloc( size_t n )
44 { 44 {
45 void *p = malloc( n ); 45 void *p = malloc( n );
46 if( !p ) 46 if( !p )
47 out_of_core(); 47 out_of_core();
48 return p; 48 return p;
49 } 49 }
50 50
51 void * 51 void *
52 xrealloc( void *a, size_t n ) 52 xrealloc( void *a, size_t n )
53 { 53 {
54 void *p = realloc( a, n ); 54 void *p = realloc( a, n );
55 if( !p ) 55 if( !p )
56 out_of_core(); 56 out_of_core();
57 return p; 57 return p;
58 } 58 }
59 59
60 char * 60 char *
61 xstrndup( const char *string, const size_t len ) 61 xstrndup( const char *string, const size_t len )
62 { 62 {
63 char *p = xmalloc( len + 1 ); 63 char *p = xmalloc( len + 1 );
64 memcpy( p, string, len ); 64 memcpy( p, string, len );
65 p[len] = '\0'; 65 p[len] = '\0';
66 return p; 66 return p;
67 } 67 }
68 68
69 unsigned int 69 unsigned int
70 strv_length (char **str_array) 70 strv_length (char **str_array)
71 { 71 {
72 unsigned int i = 0; 72 unsigned int i = 0;
73 73
74 if (!str_array) 74 if (!str_array)
75 return 0; 75 return 0;
76 76
77 while (str_array[i]) 77 while (str_array[i])
78 ++i; 78 ++i;
79 79
80 return i; 80 return i;
81 } 81 }
82 82
83 void strv_append (char ***pArray, const char *string, const size_t len) 83 void strv_append (char ***pArray, const char *string, const size_t len)
84 { 84 {
85 unsigned int old_len = 0; 85 unsigned int old_len = 0;
86 86
87 if (!*pArray) { 87 if (!*pArray)
88 *pArray = xmalloc(2 * sizeof(char*)); 88 {
89 (*pArray)[0] = xstrndup(string, len); 89 *pArray = xmalloc(2 * sizeof(char*));
90 (*pArray)[1] = NULL; 90 (*pArray)[0] = xstrndup(string, len);
91 return; 91 (*pArray)[1] = NULL;
92 } 92 return;
93 old_len = strv_length(*pArray); 93 }
94 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2)); 94 old_len = strv_length(*pArray);
95 95 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2));
96 (*pArray)[old_len] = xstrndup(string, len); 96
97 (*pArray)[old_len + 1] = NULL; 97 (*pArray)[old_len] = xstrndup(string, len);
98 (*pArray)[old_len + 1] = NULL;
98 } 99 }
99 100
100 void 101 void
101 str_append_str(char **pDst, size_t *dst_len, const char *appendage, const size_t len) 102 str_append_str(char **pDst, size_t *dst_len, const char *appendage, const size_t len)
102 { 103 {
103 if (!appendage) 104 if (!appendage)
104 return; 105 return;
105 106
106 if (!(*pDst)) { 107 if (!(*pDst))
107 *pDst = xstrndup(appendage, len); 108 {
108 *dst_len = len; 109 *pDst = xstrndup(appendage, len);
109 } else { 110 *dst_len = len;
110 size_t new_size = (*dst_len) + len + 1; 111 }
111 char *p_old = *pDst; 112 else
112 *pDst = xmalloc(new_size); 113 {
113 strncpy(*pDst, p_old, *dst_len); 114 size_t new_size = (*dst_len) + len + 1;
114 strncpy(*pDst + *dst_len, appendage, len); 115 char *p_old = *pDst;
115 *dst_len = new_size - 1; 116 *pDst = xmalloc(new_size);
116 (*pDst)[*dst_len] = '\0'; 117 strncpy(*pDst, p_old, *dst_len);
117 free (p_old); 118 strncpy(*pDst + *dst_len, appendage, len);
119 *dst_len = new_size - 1;
120 (*pDst)[*dst_len] = '\0';
121 free (p_old);
118 } 122 }
119 } 123 }
120 124
121 void 125 void
122 strv_free (char **str_array) 126 strv_free (char **str_array)
169 } 173 }
170 174
171 int str_base64_decode(char **dst, size_t *dst_size, char *src, 175 int str_base64_decode(char **dst, size_t *dst_size, char *src,
172 size_t src_size) 176 size_t src_size)
173 { 177 {
174 int ret = -1; 178 int ret = -1;
175 179
176 if (!dst || *dst) { 180 if (!dst || *dst)
177 return -1; 181 {
178 } 182 return -1;
179 183 }
180 /* Check the needed size for the buffer */ 184
181 ret = base64_decode(NULL, dst_size, 185 /* Check the needed size for the buffer */
182 (unsigned char *)src, src_size); 186 ret = base64_decode(NULL, dst_size,
183 187 (unsigned char *)src, src_size);
184 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) { 188
185 return ret; 189 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL)
186 } 190 {
187 191 return ret;
188 *dst = xmalloc(*dst_size); 192 }
189 memset (*dst, 0, *dst_size); 193
190 194 *dst = xmalloc(*dst_size);
191 ret = base64_decode((unsigned char *)*dst, dst_size, 195 memset (*dst, 0, *dst_size);
192 (unsigned char *)src, src_size); 196
193 if (ret) { 197 ret = base64_decode((unsigned char *)*dst, dst_size,
194 free (*dst); 198 (unsigned char *)src, src_size);
195 *dst = NULL; 199 if (ret)
196 *dst_size = 0; 200 {
197 } 201 free (*dst);
198 return ret; 202 *dst = NULL;
203 *dst_size = 0;
204 }
205 return ret;
199 } 206 }
200 207
201 void 208 void
202 xfree (void *p) 209 xfree (void *p)
203 { 210 {

http://wald.intevation.org/projects/trustbridge/