comparison src/strhelp.c @ 3:8b4c49c92451

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