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
12 ******************************************************************************/
13 #include "config_preproc.h"
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.
22 void string_chain_add(string_chain_t
**ec
, char *s
)
24 string_chain_t
*i
= *ec
, *new_ec
;
27 if (!strcmp(i
->str
, s
)) {
31 if (i
->next
== NULL
) break;
35 new_ec
= (string_chain_t
*) Malloc(sizeof(*new_ec
));
38 if (i
!= NULL
) i
->next
= new_ec
;
42 char* string_chain_cut(string_chain_t
**ec
)
44 string_chain_t
*i
= *ec
;
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
)
58 string_keyvalue_t
**data
=map
->data
;
61 if(strcmp(data
[m
]->key
, key
)<0) l
=m
+1;
70 if(!strcmp(data
[l
]->key
, key
)) return 1;
75 const char* string_map_add(string_map_t
*map
, char *key
,
76 char *value
, size_t value_len
)
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
;
86 /* creating a new entry */
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
;
100 const char* string_map_get_bykey(const string_map_t
*map
,
101 const char *key
, size_t *value_len
)
105 if(string_map_pos(map
, key
, &pos
)) {
106 *value_len
=map
->data
[pos
]->value_len
;
107 return map
->data
[pos
]->value
;
109 else if((s
=getenv(key
))) {
110 *value_len
=strlen(s
);
119 string_map_t
* string_map_new(void)
121 string_map_t
*map
=(string_map_t
*)Malloc(sizeof(string_map_t
));
127 /* Used only for debugging purposes
128 void string_map_dump(const string_map_t *map)
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");
140 void string_map_free(string_map_t
*map
)
143 for(i
=0; i
<map
->n
; i
++) {
144 Free(map
->data
[i
]->key
);
145 Free(map
->data
[i
]->value
);
152 char *get_macro_id_from_ref(const char *str
)
154 char *ret_val
= NULL
;
155 if (str
!= NULL
&& str
[0] == '$' && str
[1] == '{') {
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 */
163 ret_val
= mputc(ret_val
, str
[i
]);
165 } while ((str
[i
] >= 'A' && str
[i
] <= 'Z') ||
166 (str
[i
] >= 'a' && str
[i
] <= 'z') ||
167 (str
[i
] >= '0' && str
[i
] <= '9') ||
169 if (str
[i
] != ' ' && str
[i
] != '\t' && str
[i
] != ',' && str
[i
] != '}') {
170 /* the next character after the id is not a whitespace or , or } */
179 int string_is_int(const char *str
, size_t len
)
181 enum { INITIAL
, FIRST
, ZERO
, MORE
, ERR
} state
= INITIAL
;
182 /* state: expected characters
183 * INITIAL: +, -, first digit
185 * MORE, ZERO: more digit(s)
186 * ERR: error was found, stop
189 if(str
==NULL
|| str
[0]=='\0') return 0;
190 for (i
= 0; str
[i
] != '\0'; i
++) {
194 if (c
== '+' || c
== '-') state
= FIRST
;
195 else if (c
== '0') state
= ZERO
;
196 else if (c
>= '1' && c
<= '9') state
= MORE
;
200 if (c
== '0') state
= ZERO
;
201 else if (c
>= '1' && c
<= '9') state
= MORE
;
205 if (c
>= '0' && c
<= '9') {
210 if (c
>= '0' && c
<= '9') /* do nothing */;
215 if (state
== ERR
) return 0;
217 if (state
!= ZERO
&& state
!= MORE
) return 0;
222 int string_is_float(const char *str
, size_t len
)
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
238 if(str
==NULL
|| str
[0]=='\0') return 0;
239 for (i
= 0; str
[i
] != '\0'; i
++) {
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
;
249 if (c
== '0') state
= ZERO_M
;
250 else if (c
>= '1' && c
<= '9') state
= MORE_M
;
254 if (c
== '.') state
= FIRST_F
;
255 else if (c
== 'E' || c
== 'e') state
= INITIAL_E
;
256 else if (c
>= '0' && c
<= '9') {
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 */;
267 if (c
>= '0' && c
<= '9') state
= MORE_F
;
271 if (c
== 'E' || c
== 'e') state
= INITIAL_E
;
272 else if (c
>= '0' && c
<= '9') /* do nothing */;
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
;
282 if (c
== '0') state
= ZERO_E
;
283 else if (c
>= '1' && c
<= '9') state
= MORE_E
;
287 if (c
>= '0' && c
<= '9') {
292 if (c
>= '0' && c
<= '9') /* do nothing */;
297 if (state
== ERR
) return 0;
299 if (state
!= MORE_F
&& state
!= ZERO_E
&& state
!= MORE_E
300 && state
!= ZERO_M
&& state
!= MORE_M
)
306 int string_is_id(const char *str
, size_t len
)
309 int has_hyphen
, has_underscore
;
311 if (len
== 0) return 0;
313 if ((first_char
< 'a' || first_char
> 'z') &&
314 (first_char
< 'A' || first_char
> 'Z')) return 0;
317 for (i
= 1; i
< len
; i
++) {
319 if ((c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
320 (c
>= '0' && c
<= '9')) {
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;
334 int string_is_bstr(const char *str
, size_t len
)
337 for (i
= 0; i
< len
; i
++) {
339 if (c
!= '0' && c
!= '1') return 0;
344 int string_is_hstr(const char *str
, size_t len
)
347 for (i
= 0; i
< len
; i
++) {
349 if ((c
< '0' || c
>'9') && (c
< 'A' || c
> 'F') && (c
< 'a' || c
> 'f'))
355 int string_is_ostr(const char *str
, size_t len
)
357 if (len
% 2) return 0;
358 else return string_is_hstr(str
, len
);
361 int string_is_hostname(const char *str
, size_t len
)
363 enum { INITIAL
, ALPHANUM
, DOT
, DASH
, COLON
, PERCENT
} state
= INITIAL
;
365 for (i
= 0; i
< len
; i
++) {
367 if ((c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z') ||
368 (c
>= '0' && c
<= '9')) {
370 } else if (c
== '.') {
371 if (state
== ALPHANUM
) state
= DOT
;
373 } else if (c
== ':') {
374 if (state
== INITIAL
|| state
== ALPHANUM
|| state
== COLON
) state
= COLON
;
376 } else if (c
== '%') {
377 if (state
== ALPHANUM
) state
= PERCENT
;
379 } else if (c
== '-' || c
== '_') {
380 if (state
== INITIAL
|| state
== DOT
|| state
== COLON
|| state
== PERCENT
) return 0;
386 if (state
== ALPHANUM
|| state
== DOT
) return 1;
This page took 0.040729 seconds and 5 git commands to generate.