Commit | Line | Data |
---|---|---|
d44e3c4f | 1 | /****************************************************************************** |
2 | * Copyright (c) 2000-2016 Ericsson Telecom AB | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Balasko, Jeno | |
10 | * Beres, Szabolcs | |
11 | * | |
12 | ******************************************************************************/ | |
970ed795 EL |
13 | #include "config_preproc.h" |
14 | #include <string.h> | |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | ||
127ecaf5 | 18 | // Solaris 10 on x86 may define ERR in /usr/include/sys/regset.h depending on compiler and flags. |
19 | // Workaround enum conflict with undef. | |
20 | #undef ERR | |
21 | ||
970ed795 EL |
22 | void string_chain_add(string_chain_t **ec, char *s) |
23 | { | |
24 | string_chain_t *i = *ec, *new_ec; | |
25 | if (i != NULL) { | |
26 | for ( ; ; ) { | |
27 | if (!strcmp(i->str, s)) { | |
28 | Free(s); | |
29 | return; | |
30 | } | |
31 | if (i->next == NULL) break; | |
32 | i = i->next; | |
33 | } | |
34 | } | |
35 | new_ec = (string_chain_t*) Malloc(sizeof(*new_ec)); | |
36 | new_ec->str = s; | |
37 | new_ec->next = NULL; | |
38 | if (i != NULL) i->next = new_ec; | |
39 | else *ec = new_ec; | |
40 | } | |
41 | ||
42 | char* string_chain_cut(string_chain_t **ec) | |
43 | { | |
44 | string_chain_t *i = *ec; | |
45 | if (i != NULL) { | |
46 | char *s = i->str; | |
47 | *ec = i->next; | |
48 | Free(i); | |
49 | return s; | |
50 | } else return NULL; | |
51 | } | |
52 | ||
53 | /** search the position of a key; returns 1 if found, 0 otherwise. the | |
54 | * position is returned in \a pos. */ | |
55 | static int string_map_pos(const string_map_t *map, const char *key, size_t *pos) | |
56 | { | |
57 | size_t l=0, r=map->n; | |
58 | string_keyvalue_t **data=map->data; | |
59 | while(l<r) { | |
60 | size_t m=(l+r)/2; | |
61 | if(strcmp(data[m]->key, key)<0) l=m+1; | |
62 | else r=m; | |
63 | } | |
64 | if(l>=map->n) { | |
65 | *pos=map->n; | |
66 | return 0; | |
67 | } | |
68 | else { | |
69 | *pos=l; | |
70 | if(!strcmp(data[l]->key, key)) return 1; | |
71 | return 0; | |
72 | } | |
73 | } | |
74 | ||
75 | const char* string_map_add(string_map_t *map, char *key, | |
76 | char *value, size_t value_len) | |
77 | { | |
78 | size_t pos; | |
79 | if (string_map_pos(map, key, &pos)) { | |
80 | /* replacing the old value */ | |
81 | Free(map->data[pos]->value); | |
82 | map->data[pos]->value = value; | |
83 | map->data[pos]->value_len = value_len; | |
84 | return map->data[pos]->key; | |
85 | } else { | |
86 | /* creating a new entry */ | |
87 | map->n++; | |
88 | map->data = (string_keyvalue_t**) | |
89 | Realloc(map->data, (map->n) * sizeof(*map->data)); | |
90 | memmove(map->data + pos + 1, map->data + pos, | |
91 | (map->n - pos - 1) * sizeof(*map->data)); | |
92 | map->data[pos] = (string_keyvalue_t*)Malloc(sizeof(**map->data)); | |
93 | map->data[pos]->key = key; | |
94 | map->data[pos]->value = value; | |
95 | map->data[pos]->value_len = value_len; | |
96 | return NULL; | |
97 | } | |
98 | } | |
99 | ||
100 | const char* string_map_get_bykey(const string_map_t *map, | |
101 | const char *key, size_t *value_len) | |
102 | { | |
103 | size_t pos; | |
104 | const char* s; | |
105 | if(string_map_pos(map, key, &pos)) { | |
106 | *value_len=map->data[pos]->value_len; | |
107 | return map->data[pos]->value; | |
108 | } | |
109 | else if((s=getenv(key))) { | |
110 | *value_len=strlen(s); | |
111 | return s; | |
112 | } | |
113 | else { | |
114 | *value_len=0; | |
115 | return NULL; | |
116 | } | |
117 | } | |
118 | ||
119 | string_map_t* string_map_new(void) | |
120 | { | |
121 | string_map_t *map=(string_map_t*)Malloc(sizeof(string_map_t)); | |
122 | map->n=0; | |
123 | map->data=NULL; | |
124 | return map; | |
125 | } | |
126 | ||
127 | /* Used only for debugging purposes | |
128 | void string_map_dump(const string_map_t *map) | |
129 | { | |
130 | size_t i; | |
131 | fprintf(stderr, "--------\n- n: %u -\n--------\n", map->n); | |
132 | for(i=0; i<map->n; i++) | |
133 | fprintf(stderr, "%u: `%s' -> `%s' (%u)\n", i, | |
134 | map->data[i]->key, map->data[i]->value, | |
135 | map->data[i]->value_len); | |
136 | fprintf(stderr, "--------\n"); | |
137 | } | |
138 | */ | |
139 | ||
140 | void string_map_free(string_map_t *map) | |
141 | { | |
142 | size_t i; | |
143 | for(i=0; i<map->n; i++) { | |
144 | Free(map->data[i]->key); | |
145 | Free(map->data[i]->value); | |
146 | Free(map->data[i]); | |
147 | } | |
148 | Free(map->data); | |
149 | Free(map); | |
150 | } | |
151 | ||
152 | char *get_macro_id_from_ref(const char *str) | |
153 | { | |
154 | char *ret_val = NULL; | |
155 | if (str != NULL && str[0] == '$' && str[1] == '{') { | |
156 | size_t i = 2; | |
157 | /* skip over the whitespaces after the brace */ | |
158 | while (str[i] == ' ' || str[i] == '\t') i++; | |
159 | if ((str[i] >= 'A' && str[i] <= 'Z') || | |
160 | (str[i] >= 'a' && str[i] <= 'z')) { | |
161 | /* the first character of the id shall be a letter */ | |
162 | do { | |
163 | ret_val = mputc(ret_val, str[i]); | |
164 | i++; | |
165 | } while ((str[i] >= 'A' && str[i] <= 'Z') || | |
166 | (str[i] >= 'a' && str[i] <= 'z') || | |
167 | (str[i] >= '0' && str[i] <= '9') || | |
168 | str[i] == '_'); | |
169 | if (str[i] != ' ' && str[i] != '\t' && str[i] != ',' && str[i] != '}') { | |
170 | /* the next character after the id is not a whitespace or , or } */ | |
171 | Free(ret_val); | |
172 | ret_val = NULL; | |
173 | } | |
174 | } | |
175 | } | |
176 | return ret_val; | |
177 | } | |
178 | ||
179 | int string_is_int(const char *str, size_t len) | |
180 | { | |
181 | enum { INITIAL, FIRST, ZERO, MORE, ERR } state = INITIAL; | |
182 | /* state: expected characters | |
183 | * INITIAL: +, -, first digit | |
184 | * FIRST: first digit | |
185 | * MORE, ZERO: more digit(s) | |
186 | * ERR: error was found, stop | |
187 | */ | |
188 | size_t i; | |
189 | if(str==NULL || str[0]=='\0') return 0; | |
190 | for (i = 0; str[i] != '\0'; i++) { | |
191 | char c = str[i]; | |
192 | switch (state) { | |
193 | case INITIAL: | |
194 | if (c == '+' || c == '-') state = FIRST; | |
195 | else if (c == '0') state = ZERO; | |
196 | else if (c >= '1' && c <= '9') state = MORE; | |
197 | else state = ERR; | |
198 | break; | |
199 | case FIRST: | |
200 | if (c == '0') state = ZERO; | |
201 | else if (c >= '1' && c <= '9') state = MORE; | |
202 | else state = ERR; | |
203 | break; | |
204 | case ZERO: | |
205 | if (c >= '0' && c <= '9') { | |
206 | state = MORE; | |
207 | } else state = ERR; | |
208 | break; | |
209 | case MORE: | |
210 | if (c >= '0' && c <= '9') /* do nothing */; | |
211 | else state = ERR; | |
212 | default: | |
213 | break; | |
214 | } | |
215 | if (state == ERR) return 0; | |
216 | } | |
217 | if (state != ZERO && state != MORE) return 0; | |
218 | if(i!=len) return 0; | |
219 | return 1; | |
220 | } | |
221 | ||
222 | int string_is_float(const char *str, size_t len) | |
223 | { | |
224 | enum { INITIAL, FIRST_M, ZERO_M, MORE_M, FIRST_F, MORE_F, INITIAL_E, | |
225 | FIRST_E, ZERO_E, MORE_E, ERR } state = INITIAL; | |
226 | /* state: expected characters | |
227 | * INITIAL: +, -, first digit of integer part in mantissa | |
228 | * FIRST_M: first digit of integer part in mantissa | |
229 | * ZERO_M, MORE_M: more digits of mantissa, decimal dot, E | |
230 | * FIRST_F: first digit of fraction | |
231 | * MORE_F: more digits of fraction, E | |
232 | * INITIAL_E: +, -, first digit of exponent | |
233 | * FIRST_E: first digit of exponent | |
234 | * ZERO_E, MORE_E: more digits of exponent | |
235 | * ERR: error was found, stop | |
236 | */ | |
237 | size_t i; | |
238 | if(str==NULL || str[0]=='\0') return 0; | |
239 | for (i = 0; str[i] != '\0'; i++) { | |
240 | char c = str[i]; | |
241 | switch (state) { | |
242 | case INITIAL: | |
243 | if (c == '+' || c == '-') state = FIRST_M; | |
244 | else if (c == '0') state = ZERO_M; | |
245 | else if (c >= '1' && c <= '9') state = MORE_M; | |
246 | else state = ERR; | |
247 | break; | |
248 | case FIRST_M: | |
249 | if (c == '0') state = ZERO_M; | |
250 | else if (c >= '1' && c <= '9') state = MORE_M; | |
251 | else state = ERR; | |
252 | break; | |
253 | case ZERO_M: | |
254 | if (c == '.') state = FIRST_F; | |
255 | else if (c == 'E' || c == 'e') state = INITIAL_E; | |
256 | else if (c >= '0' && c <= '9') { | |
257 | state = MORE_M; | |
258 | } else state = ERR; | |
259 | break; | |
260 | case MORE_M: | |
261 | if (c == '.') state = FIRST_F; | |
262 | else if (c == 'E' || c == 'e') state = INITIAL_E; | |
263 | else if (c >= '0' && c <= '9') /* do nothing */; | |
264 | else state = ERR; | |
265 | break; | |
266 | case FIRST_F: | |
267 | if (c >= '0' && c <= '9') state = MORE_F; | |
268 | else state = ERR; | |
269 | break; | |
270 | case MORE_F: | |
271 | if (c == 'E' || c == 'e') state = INITIAL_E; | |
272 | else if (c >= '0' && c <= '9') /* do nothing */; | |
273 | else state = ERR; | |
274 | break; | |
275 | case INITIAL_E: | |
276 | if (c == '+' || c == '-') state = FIRST_E; | |
277 | else if (c == '0') state = ZERO_E; | |
278 | else if (c >= '1' && c <= '9') state = MORE_E; | |
279 | else state = ERR; | |
280 | break; | |
281 | case FIRST_E: | |
282 | if (c == '0') state = ZERO_E; | |
283 | else if (c >= '1' && c <= '9') state = MORE_E; | |
284 | else state = ERR; | |
285 | break; | |
286 | case ZERO_E: | |
287 | if (c >= '0' && c <= '9') { | |
288 | state = MORE_E; | |
289 | } else state = ERR; | |
290 | break; | |
291 | case MORE_E: | |
292 | if (c >= '0' && c <= '9') /* do nothing */; | |
293 | else state = ERR; | |
294 | default: | |
295 | break; | |
296 | } | |
297 | if (state == ERR) return 0; | |
298 | } | |
299 | if (state != MORE_F && state != ZERO_E && state != MORE_E | |
300 | && state != ZERO_M && state != MORE_M) | |
301 | return 0; | |
302 | if(i!=len) return 0; | |
303 | return 1; | |
304 | } | |
305 | ||
306 | int string_is_id(const char *str, size_t len) | |
307 | { | |
308 | size_t i; | |
309 | int has_hyphen, has_underscore; | |
310 | char first_char; | |
311 | if (len == 0) return 0; | |
312 | first_char = str[0]; | |
313 | if ((first_char < 'a' || first_char > 'z') && | |
314 | (first_char < 'A' || first_char > 'Z')) return 0; | |
315 | has_hyphen = 0; | |
316 | has_underscore = 0; | |
317 | for (i = 1; i < len; i++) { | |
318 | char c = str[i]; | |
319 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | |
320 | (c >= '0' && c <= '9')) { | |
321 | /* do nothing */ | |
322 | } else if (c == '_') { | |
323 | if (has_hyphen) return 0; | |
324 | else has_underscore = 1; | |
325 | } else if (c == '-') { | |
326 | if (has_underscore || str[i - 1] == '-' || i == len - 1 || | |
327 | first_char < 'a' || first_char > 'z') return 0; | |
328 | else has_hyphen = 1; | |
329 | } else return 0; | |
330 | } | |
331 | return 1; | |
332 | } | |
333 | ||
334 | int string_is_bstr(const char *str, size_t len) | |
335 | { | |
336 | size_t i; | |
337 | for (i = 0; i < len; i++) { | |
338 | char c = str[i]; | |
339 | if (c != '0' && c != '1') return 0; | |
340 | } | |
341 | return 1; | |
342 | } | |
343 | ||
344 | int string_is_hstr(const char *str, size_t len) | |
345 | { | |
346 | size_t i; | |
347 | for (i = 0; i < len; i++) { | |
348 | char c = str[i]; | |
349 | if ((c < '0' || c >'9') && (c < 'A' || c > 'F') && (c < 'a' || c > 'f')) | |
350 | return 0; | |
351 | } | |
352 | return 1; | |
353 | } | |
354 | ||
355 | int string_is_ostr(const char *str, size_t len) | |
356 | { | |
357 | if (len % 2) return 0; | |
358 | else return string_is_hstr(str, len); | |
359 | } | |
360 | ||
361 | int string_is_hostname(const char *str, size_t len) | |
362 | { | |
363 | enum { INITIAL, ALPHANUM, DOT, DASH, COLON, PERCENT } state = INITIAL; | |
364 | size_t i; | |
365 | for (i = 0; i < len; i++) { | |
366 | char c = str[i]; | |
367 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || | |
368 | (c >= '0' && c <= '9')) { | |
369 | state = ALPHANUM; | |
370 | } else if (c == '.') { | |
371 | if (state == ALPHANUM) state = DOT; | |
372 | else return 0; | |
373 | } else if (c == ':') { | |
374 | if (state == INITIAL || state == ALPHANUM || state == COLON) state = COLON; | |
375 | else return 0; | |
376 | } else if (c == '%') { | |
377 | if (state == ALPHANUM) state = PERCENT; | |
378 | else return 0; | |
379 | } else if (c == '-' || c == '_') { | |
380 | if (state == INITIAL || state == DOT || state == COLON || state == PERCENT) return 0; | |
381 | else state = DASH; | |
382 | } else { | |
383 | return 0; | |
384 | } | |
385 | } | |
386 | if (state == ALPHANUM || state == DOT) return 1; | |
387 | else return 0; | |
388 | } |