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