1 | #pragma once |
---|
2 | |
---|
3 | #include <math.trait.hfa> |
---|
4 | |
---|
5 | |
---|
6 | // *********************************** initial declarations *********************************** |
---|
7 | |
---|
8 | |
---|
9 | enum config_format { |
---|
10 | TABULAR_CONFIG |
---|
11 | }; |
---|
12 | |
---|
13 | struct config_entry { |
---|
14 | const char * key; |
---|
15 | void * variable; |
---|
16 | bool (*parse)( const char *, void * ); |
---|
17 | bool (*validate)( void * ); |
---|
18 | }; |
---|
19 | |
---|
20 | bool null_validator( void * ) { return true; } |
---|
21 | |
---|
22 | static inline void ?{}( config_entry & this ) {} |
---|
23 | |
---|
24 | forall(T & | { bool parse( const char *, T & ); }) |
---|
25 | static inline void ?{}( config_entry & this, const char * key, T & variable ) { |
---|
26 | this.key = key; |
---|
27 | this.variable = (void *)&variable; |
---|
28 | this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse; |
---|
29 | this.validate = null_validator; |
---|
30 | } |
---|
31 | |
---|
32 | forall(T & | { bool parse( const char *, T & ); }) |
---|
33 | static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*validate)(T &) ) { |
---|
34 | this.key = key; |
---|
35 | this.variable = (void *)&variable; |
---|
36 | this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse; |
---|
37 | this.validate = (bool (*)(void *))(bool (*)(T &))validate; |
---|
38 | } |
---|
39 | |
---|
40 | forall(T &) |
---|
41 | static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*parse)(const char *, T &) ) { |
---|
42 | this.key = key; |
---|
43 | this.variable = (void *)&variable; |
---|
44 | this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse; |
---|
45 | this.validate = null_validator; |
---|
46 | } |
---|
47 | |
---|
48 | forall(T &) |
---|
49 | static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*parse)(const char *, T &), bool (*validate)(T &) ) { |
---|
50 | this.key = key; |
---|
51 | this.variable = (void *)&variable; |
---|
52 | this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse; |
---|
53 | this.validate = (bool (*)(void *))(bool (*)(T &))validate; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | // *********************************** exceptions *********************************** |
---|
58 | |
---|
59 | |
---|
60 | exception Missing_Config_Entries { |
---|
61 | unsigned int num_missing; |
---|
62 | }; |
---|
63 | |
---|
64 | void msg( Missing_Config_Entries * ex ); |
---|
65 | |
---|
66 | exception Parse_Failure { |
---|
67 | * char failed_key; |
---|
68 | * char failed_value; |
---|
69 | }; |
---|
70 | |
---|
71 | void msg( Parse_Failure * ex ); |
---|
72 | |
---|
73 | exception Validation_Failure { |
---|
74 | * char failed_key; |
---|
75 | * char failed_value; |
---|
76 | }; |
---|
77 | |
---|
78 | void msg( Validation_Failure * ex ); |
---|
79 | |
---|
80 | |
---|
81 | // *********************************** main code *********************************** |
---|
82 | |
---|
83 | |
---|
84 | void parse_config( const char * config_file, config_entry entries[], size_t num_entries, config_format format ); |
---|
85 | |
---|
86 | bool parse( const char *, const char * & ); |
---|
87 | bool parse( const char *, int & ); |
---|
88 | bool parse( const char *, unsigned & ); |
---|
89 | bool parse( const char *, unsigned long & ); |
---|
90 | bool parse( const char *, unsigned long long & ); |
---|
91 | bool parse( const char *, float & ); |
---|
92 | bool parse( const char *, double & ); |
---|
93 | |
---|
94 | |
---|
95 | // *********************************** validation *********************************** |
---|
96 | |
---|
97 | |
---|
98 | forall(T | Relational( T )) |
---|
99 | bool is_nonnegative( T & ); |
---|
100 | |
---|
101 | forall(T | Relational( T )) |
---|
102 | bool is_positive( T & ); |
---|
103 | |
---|
104 | forall(T | Relational( T )) |
---|
105 | bool is_nonpositive( T & ); |
---|
106 | |
---|
107 | forall(T | Relational( T )) |
---|
108 | bool is_negative( T & ); |
---|
109 | |
---|
110 | |
---|
111 | // Local Variables: // |
---|
112 | // mode: c // |
---|
113 | // tab-width: 4 // |
---|
114 | // End: // |
---|