|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "parsemap.h" |
|
20 #include <stdio.h> |
|
21 #include <string.h> |
|
22 #include <stdlib.h> |
|
23 #include <errno.h> |
|
24 |
|
25 /* function that searches untyped parseMap_XXX tables. also performs literal integer parsing. */ |
|
26 PARSEMAP_LINKAGE int ParseMapSearch(const char* text, int* result, ParseMapEntry* entries, int numEntries) |
|
27 { |
|
28 int textlen = strlen(text); |
|
29 int i; |
|
30 char* endptr = 0; |
|
31 |
|
32 /* search the parse map for the matching string */ |
|
33 for(i = 0; i < numEntries; i++) { |
|
34 if(entries[i].length == textlen && strcasecmp(text, entries[i].text) == 0) { |
|
35 *result = entries[i].value; |
|
36 return 1; |
|
37 } |
|
38 } |
|
39 /* match not found */ |
|
40 |
|
41 /* try to parse string as an integer */ |
|
42 errno = 0; |
|
43 i = strtol(text, &endptr, 0); |
|
44 if(errno == ERANGE) |
|
45 { |
|
46 errno = 0; |
|
47 i = (int) strtoul(text, &endptr, 0); |
|
48 if(errno == ERANGE) |
|
49 { |
|
50 return 0; |
|
51 } |
|
52 } |
|
53 if(endptr == (text + textlen)) /* all of string must be part of number */ |
|
54 { |
|
55 *result = i; |
|
56 return 1; |
|
57 } |
|
58 |
|
59 /* fail */ |
|
60 return 0; |
|
61 } |
|
62 |
|
63 PARSEMAP_LINKAGE const char* ParseMapSearchReverse(int value, char not_found_buf[11], ParseMapEntry* entries, int numEntries) { |
|
64 int i; |
|
65 |
|
66 /* search the parse map for the matching value */ |
|
67 for(i = 0; i < numEntries; i++) { |
|
68 if(entries[i].value == value) { |
|
69 return entries[i].text; |
|
70 } |
|
71 } |
|
72 |
|
73 /* value not found in table, format hex string */ |
|
74 sprintf(not_found_buf, "0x%08X", value); |
|
75 return not_found_buf; |
|
76 } |