Mercurial > retraceit
comparison src/strhelp.c @ 0:147b08bc7d64
Initial commit of a basic Application framework.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 23 Mar 2015 12:41:52 +0100 |
parents | |
children | 64a51a42c01f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:147b08bc7d64 |
---|---|
1 /* Copyright (C) 2014 by Intevation GmbH | |
2 * | |
3 * This file is Free Software under the GNU GPL (v>=2) | |
4 * and comes with ABSOLUTELY NO WARRANTY! | |
5 * See LICENSE.txt for details. | |
6 */ | |
7 /* Needed to get asprintf */ | |
8 #define _GNU_SOURCE 1 | |
9 | |
10 /** @file See strhelp.h for documentation. */ | |
11 | |
12 #include <ctype.h> | |
13 #include <stdarg.h> | |
14 #include <stdbool.h> | |
15 #include <stdio.h> | |
16 #include <stdlib.h> | |
17 #include <string.h> | |
18 #include <assert.h> | |
19 | |
20 #ifdef WIN32 | |
21 #include <windows.h> | |
22 #endif | |
23 | |
24 static void | |
25 out_of_core(void) | |
26 { | |
27 fputs("\nfatal: out of memory\n", stderr); | |
28 exit(2); | |
29 } | |
30 | |
31 void * | |
32 xmalloc( size_t n ) | |
33 { | |
34 void *p = malloc( n ); | |
35 if( !p ) | |
36 out_of_core(); | |
37 return p; | |
38 } | |
39 | |
40 void * | |
41 xmalloc0( size_t n ) | |
42 { | |
43 void *p = malloc( n ); | |
44 if( !p ) | |
45 out_of_core(); | |
46 memset (p, 0, n); | |
47 return p; | |
48 } | |
49 | |
50 void * | |
51 xrealloc( void *a, size_t n ) | |
52 { | |
53 void *p = realloc( a, n ); | |
54 if( !p ) | |
55 out_of_core(); | |
56 return p; | |
57 } | |
58 | |
59 char * | |
60 xstrndup( const char *string, const size_t len ) | |
61 { | |
62 char *p = xmalloc( len + 1 ); | |
63 memcpy( p, string, len ); | |
64 p[len] = '\0'; | |
65 return p; | |
66 } | |
67 | |
68 unsigned int | |
69 strv_length (char **str_array) | |
70 { | |
71 unsigned int i = 0; | |
72 | |
73 if (!str_array) | |
74 return 0; | |
75 | |
76 while (str_array[i]) | |
77 ++i; | |
78 | |
79 return i; | |
80 } | |
81 | |
82 void strv_append (char ***pArray, const char *string, const size_t len) | |
83 { | |
84 unsigned int old_len = 0; | |
85 | |
86 if (!*pArray) | |
87 { | |
88 *pArray = xmalloc(2 * sizeof(char*)); | |
89 (*pArray)[0] = xstrndup(string, len); | |
90 (*pArray)[1] = NULL; | |
91 return; | |
92 } | |
93 old_len = strv_length(*pArray); | |
94 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2)); | |
95 | |
96 (*pArray)[old_len] = xstrndup(string, len); | |
97 (*pArray)[old_len + 1] = NULL; | |
98 } | |
99 | |
100 void | |
101 str_append_str(char **pDst, size_t *dst_len, const char *appendage, const size_t len) | |
102 { | |
103 if (!appendage) | |
104 return; | |
105 | |
106 if (!(*pDst)) | |
107 { | |
108 *pDst = xstrndup(appendage, len); | |
109 *dst_len = len; | |
110 } | |
111 else | |
112 { | |
113 size_t new_size = (*dst_len) + len + 1; | |
114 char *p_old = *pDst; | |
115 *pDst = xmalloc(new_size); | |
116 strncpy(*pDst, p_old, *dst_len); | |
117 strncpy(*pDst + *dst_len, appendage, len); | |
118 *dst_len = new_size - 1; | |
119 (*pDst)[*dst_len] = '\0'; | |
120 free (p_old); | |
121 } | |
122 } | |
123 | |
124 void | |
125 strv_free (char **str_array) | |
126 { | |
127 if (str_array) | |
128 { | |
129 int i; | |
130 | |
131 for (i = 0; str_array[i] != NULL; i++) | |
132 free (str_array[i]); | |
133 | |
134 free (str_array); | |
135 } | |
136 } | |
137 | |
138 bool | |
139 str_equal (char *s1, char *s2) | |
140 { | |
141 size_t l1 = strlen(s1); | |
142 size_t l2 = strlen(s2); | |
143 if ((l1 == l2) && | |
144 (strcmp(s1, s2) == 0)) | |
145 return true; | |
146 else | |
147 return false; | |
148 } | |
149 | |
150 bool | |
151 str_starts_with (char *s1, char *s2) | |
152 { | |
153 size_t l2 = strlen(s2); | |
154 if (strncmp(s1, s2, l2) == 0) | |
155 return true; | |
156 else | |
157 return false; | |
158 } | |
159 | |
160 void | |
161 str_trim (char **s) | |
162 { | |
163 size_t i; | |
164 if (*s != NULL) | |
165 { | |
166 while (isspace(**s)) | |
167 (*s)++; | |
168 i = strlen(*s); | |
169 while (isspace((*s)[--i])) | |
170 (*s)[i] = '\0'; | |
171 } | |
172 } | |
173 | |
174 void | |
175 xfree (void *p) | |
176 { | |
177 if (p) | |
178 free (p); | |
179 } | |
180 | |
181 int | |
182 xasprintf (char **strp, const char *fmt, ...) | |
183 { | |
184 int ret; | |
185 va_list ap; | |
186 va_start(ap, fmt); | |
187 ret = vasprintf(strp, fmt, ap); | |
188 va_end(ap); | |
189 | |
190 if (ret == -1) | |
191 out_of_core(); | |
192 | |
193 return ret; | |
194 } | |
195 | |
196 #ifdef WIN32 | |
197 /* Adapted from GPGOL rev. e512053 */ | |
198 char * | |
199 wchar_to_utf8 (const wchar_t *string, size_t len) | |
200 { | |
201 int n, ilen; | |
202 char *result; | |
203 | |
204 ilen = (int) len; | |
205 if (ilen < 0) | |
206 return NULL; | |
207 | |
208 /* Note, that CP_UTF8 is not defined in Windows versions earlier | |
209 than NT.*/ | |
210 n = WideCharToMultiByte (CP_UTF8, 0, string, ilen, NULL, 0, NULL, NULL); | |
211 if (n < 0) | |
212 return NULL; | |
213 | |
214 result = xmalloc ((size_t)n+1); | |
215 n = WideCharToMultiByte (CP_UTF8, 0, string, ilen, result, n, NULL, NULL); | |
216 if (n < 0) | |
217 { | |
218 xfree (result); | |
219 return NULL; | |
220 } | |
221 result[n] = 0; | |
222 return result; | |
223 } | |
224 | |
225 /* Adapted from GPGOL rev. e512053 */ | |
226 wchar_t * | |
227 utf8_to_wchar (const char *string, size_t len) | |
228 { | |
229 int n, ilen; | |
230 wchar_t *result; | |
231 | |
232 ilen = (int) len; | |
233 if (ilen < 0) | |
234 return NULL; | |
235 | |
236 n = MultiByteToWideChar (CP_UTF8, 0, string, ilen, NULL, 0); | |
237 if (n < 0 || n + 1 < 0) | |
238 return NULL; | |
239 | |
240 result = xmalloc ((size_t)(n+1) * sizeof *result); | |
241 n = MultiByteToWideChar (CP_UTF8, 0, string, ilen, result, n); | |
242 if (n < 0) | |
243 { | |
244 xfree (result); | |
245 return NULL; | |
246 } | |
247 result[n] = 0; | |
248 return result; | |
249 } | |
250 | |
251 wchar_t | |
252 *acp_to_wchar (const char *string, size_t len) | |
253 { | |
254 int n, ilen; | |
255 wchar_t *result; | |
256 | |
257 ilen = (int) len; | |
258 if (ilen < 0) | |
259 return NULL; | |
260 | |
261 n = MultiByteToWideChar (CP_ACP, 0, string, ilen, NULL, 0); | |
262 if (n < 0 || n + 1 < 0) | |
263 return NULL; | |
264 | |
265 result = xmalloc ((size_t)(n+1) * sizeof *result); | |
266 n = MultiByteToWideChar (CP_ACP, 0, string, ilen, result, n); | |
267 if (n < 0) | |
268 { | |
269 xfree (result); | |
270 return NULL; | |
271 } | |
272 result[n] = 0; | |
273 return result; | |
274 } | |
275 #endif | |
276 |