|
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 #ifndef PARSEMAP_H_ |
|
20 #define PARSEMAP_H_ |
|
21 |
|
22 /* define PARSEMAP_LINKAGE to prepend extern "C" in front of symbols if using a C++ compiler */ |
|
23 #ifdef __cplusplus |
|
24 #define PARSEMAP_LINKAGE extern "C" |
|
25 #else |
|
26 #define PARSEMAP_LINKAGE |
|
27 #endif |
|
28 |
|
29 /* the structure used in the parseMap_XXX tables */ |
|
30 typedef struct |
|
31 { |
|
32 int value; /* numeric value */ |
|
33 const char* text; /* textual equivalent of value */ |
|
34 int length; /* length of text. avoids heavy use of strlen */ |
|
35 } ParseMapEntry; |
|
36 |
|
37 /* function that searches untyped parseMap_XXX tables. also performs literal integer parsing. */ |
|
38 PARSEMAP_LINKAGE int ParseMapSearch(const char* text, int* result, ParseMapEntry* entries, int numEntries); |
|
39 PARSEMAP_LINKAGE const char* ParseMapSearchReverse(int value, char not_found_buf[11], ParseMapEntry* entries, int numEntries); |
|
40 |
|
41 /* macro to declare a parser function of type T */ |
|
42 #define DECL_PARSETYPE(T) \ |
|
43 PARSEMAP_LINKAGE int parse_##T(const char* text, T* result); \ |
|
44 PARSEMAP_LINKAGE const char* format_##T(T value, char not_found_buf[11]) |
|
45 |
|
46 /* macro to begin a parseMap definition of type T */ |
|
47 #define PARSE_MAP_START(T) \ |
|
48 const ParseMapEntry parseMap_##T[] = { |
|
49 |
|
50 /* macro to add a ParseMapEntry struct to a parseMap table. preprocessor is used to generate text and length values. */ |
|
51 #define PARSE_MAP_ENTRY(x) \ |
|
52 { x, #x, sizeof(#x)-1 } |
|
53 |
|
54 /* macro to create a ParseMapEntry where the text is not identical to the C identifier. */ |
|
55 #define PARSE_MAP_ALIAS(val, alias) \ |
|
56 { val, alias, sizeof(alias)-1 } |
|
57 |
|
58 #define PARSE_MAP_PREFIXENTRY(prefix, tail) \ |
|
59 PARSE_MAP_ENTRY(prefix##tail), \ |
|
60 PARSE_MAP_ALIAS(prefix##tail, #tail) |
|
61 |
|
62 /* macro to end a parseMap definition. also defines the typed parser function */ |
|
63 #define PARSE_MAP_END(T) \ |
|
64 }; \ |
|
65 \ |
|
66 PARSEMAP_LINKAGE int parse_##T(const char* text, T* result) \ |
|
67 { \ |
|
68 return ParseMapSearch(text, (int*) result, (ParseMapEntry*) parseMap_##T, sizeof(parseMap_##T) / sizeof(ParseMapEntry)); \ |
|
69 } \ |
|
70 \ |
|
71 PARSEMAP_LINKAGE const char* format_##T(T value, char not_found_buf[11]) \ |
|
72 { \ |
|
73 return ParseMapSearchReverse((int) value, not_found_buf, (ParseMapEntry*) parseMap_##T, sizeof(parseMap_##T) / sizeof(ParseMapEntry)); \ |
|
74 } \ |
|
75 |
|
76 |
|
77 #endif /* PARSEMAP_H_ */ |