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