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