Mercurial > trustbridge
annotate common/strhelp.c @ 134:5f830969e4a1
Added missing include.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Mon, 24 Mar 2014 15:17:16 +0100 |
parents | c719d3fdbc15 |
children | 3fb6ddd6b70d |
rev | line source |
---|---|
134 | 1 #include <ctype.h> |
131
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
2 #include <stdbool.h> |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
3 #include <stdio.h> |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
4 #include <stdlib.h> |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
5 #include <string.h> |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
6 #include <assert.h> |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
7 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
8 static void |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
9 out_of_core(void) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
10 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
11 fputs("\nfatal: out of memory\n", stderr); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
12 exit(2); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
13 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
14 void * |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
15 xmalloc( size_t n ) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
16 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
17 void *p = malloc( n ); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
18 if( !p ) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
19 out_of_core(); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
20 return p; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
21 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
22 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
23 void * |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
24 xrealloc( void *a, size_t n ) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
25 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
26 void *p = realloc( a, n ); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
27 if( !p ) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
28 out_of_core(); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
29 return p; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
30 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
31 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
32 char * |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
33 xstrndup( const char *string, const size_t len ) |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
34 { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
35 char *p = xmalloc( len + 1 ); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
36 memcpy( p, string, len ); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
37 p[len] = '\0'; |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
38 return p; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
39 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
40 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
41 unsigned int |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
42 strv_length (char **str_array) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
43 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
44 unsigned int i = 0; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
45 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
46 if (!str_array) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
47 return 0; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
48 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
49 while (str_array[i]) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
50 ++i; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
51 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
52 return i; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
53 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
54 |
116
c602d8cfa619
Refactoring: unified naming of string vector functions.
Sascha Wilde <wilde@intevation.de>
parents:
91
diff
changeset
|
55 void strv_append (char ***pArray, const char *string, const size_t len) |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
56 { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
57 unsigned int old_len = 0; |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
58 |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
59 if (!*pArray) { |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
60 *pArray = xmalloc(2 * sizeof(char*)); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
61 (*pArray)[0] = xstrndup(string, len); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
62 (*pArray)[1] = NULL; |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
63 return; |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
64 } |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
65 old_len = strv_length(*pArray); |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
66 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2)); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
67 |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
68 (*pArray)[old_len] = xstrndup(string, len); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
69 (*pArray)[old_len + 1] = NULL; |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
70 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
71 |
91
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
72 void |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
73 str_append_str(char **pDst, size_t *dst_len, const char *appendage, const size_t len) |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
74 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
75 if (!appendage) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
76 return; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
77 |
91
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
78 if (!(*pDst)) { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
79 *pDst = xstrndup(appendage, len); |
91
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
80 *dst_len = len; |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
81 } else { |
91
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
82 size_t new_size = (*dst_len) + len + 1; |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
83 char *p_old = *pDst; |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
84 *pDst = xmalloc(new_size); |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
85 strncpy(*pDst, p_old, *dst_len); |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
86 strncpy(*pDst + *dst_len, appendage, len); |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
87 *dst_len = new_size - 1; |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
88 (*pDst)[*dst_len] = '\0'; |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
89 free (p_old); |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
90 } |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
91 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
92 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
93 void |
116
c602d8cfa619
Refactoring: unified naming of string vector functions.
Sascha Wilde <wilde@intevation.de>
parents:
91
diff
changeset
|
94 strv_free (char **str_array) |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
95 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
96 if (str_array) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
97 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
98 int i; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
99 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
100 for (i = 0; str_array[i] != NULL; i++) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
101 free (str_array[i]); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
102 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
103 free (str_array); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
104 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
105 } |
131
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
106 |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
107 bool |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
108 str_equal (char *s1, char *s2) |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
109 { |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
110 size_t l1 = strlen(s1); |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
111 size_t l2 = strlen(s2); |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
112 if ((l1 == l2) && |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
113 (strncmp(s1, s2, l1) == 0)) |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
114 return true; |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
115 else |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
116 return false; |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
117 } |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
118 |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
119 bool |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
120 str_starts_with (char *s1, char *s2) |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
121 { |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
122 if (strncmp(s1, s2, strlen(s2)) == 0) |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
123 return true; |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
124 else |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
125 return false; |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
126 } |
133
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
127 |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
128 void |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
129 str_trim (char **s) |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
130 { |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
131 size_t i; |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
132 while (isspace(**s)) |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
133 (*s)++; |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
134 i = strlen(*s); |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
135 while (isspace((*s)[--i])) |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
136 (*s)[i] = '\0'; |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
137 } |