1 | #include <fstream.hfa> |
---|
2 | #include <parseargs.hfa> |
---|
3 | #include <stdlib.hfa> |
---|
4 | #include <string.h> |
---|
5 | #include "parseconfig.hfa" |
---|
6 | |
---|
7 | |
---|
8 | // *********************************** exceptions *********************************** |
---|
9 | |
---|
10 | |
---|
11 | EHM_VIRTUAL_TABLE(Validation_Failure, Validation_Failure_main_table); |
---|
12 | void ?{}( Validation_Failure & this, config_entry & entry ) with ( entry ) { |
---|
13 | this.virtual_table = &Validation_Failure_main_table; |
---|
14 | this.key = key; |
---|
15 | this.variable = variable; |
---|
16 | } |
---|
17 | void throwValidation_Failure( config_entry & entry ) { |
---|
18 | Validation_Failure exc = { entry }; |
---|
19 | } |
---|
20 | |
---|
21 | |
---|
22 | // *********************************** main code *********************************** |
---|
23 | |
---|
24 | |
---|
25 | struct KVPairs { |
---|
26 | int size, max_size; |
---|
27 | * [ char *, char * ] data; |
---|
28 | }; |
---|
29 | void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor |
---|
30 | size = 0; max_size = 0; data = 0p; |
---|
31 | } |
---|
32 | void ?{}( KVPairs & kvp, int size ) { // initialization |
---|
33 | kvp.[ size, max_size ] = [ 0, size ]; |
---|
34 | kvp.data = alloc( size ); |
---|
35 | } |
---|
36 | /* |
---|
37 | void ?{}( KVPairs & kvp, KVPairs val ) with( val ) { // copy, deep |
---|
38 | kvp.[ size, max_size ] = [ size, max_size ]; |
---|
39 | kvp.data = alloc( size, data ); |
---|
40 | } |
---|
41 | */ |
---|
42 | void ^?{}( KVPairs & kvp ) with ( kvp ) { // destructor |
---|
43 | free( data ); |
---|
44 | size = 0; max_size = 0; data = 0p; |
---|
45 | } |
---|
46 | |
---|
47 | void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) { |
---|
48 | if ( size == max_size ) { |
---|
49 | max_size *= 2; |
---|
50 | data = resize( data, max_size ); |
---|
51 | } |
---|
52 | |
---|
53 | data[size] = [ k, v ]; |
---|
54 | ++size; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | struct StringBuilder { |
---|
59 | int size, max_size; |
---|
60 | * char string; |
---|
61 | }; |
---|
62 | void ?{}( StringBuilder & sb ) with ( sb ) { // default constructor |
---|
63 | size = 1; max_size = 1; string = alloc( 1 ); |
---|
64 | string[0] = '\0'; |
---|
65 | } |
---|
66 | void ?{}( StringBuilder & sb, int size ) { // initialization |
---|
67 | sb.[ size, max_size ] = [ 1, size+1 ]; |
---|
68 | sb.string = alloc( size ); |
---|
69 | sb.string[0] = '\0'; |
---|
70 | } |
---|
71 | void ^?{}( StringBuilder & sb ) with ( sb ) { // destructor |
---|
72 | free( string ); |
---|
73 | size = 0; max_size = 0; string = 0p; |
---|
74 | } |
---|
75 | |
---|
76 | void add_char( StringBuilder & sb, char c ) with ( sb ) { |
---|
77 | if ( size == max_size ) { |
---|
78 | max_size *= 2; |
---|
79 | string = resize( string, max_size ); |
---|
80 | } |
---|
81 | |
---|
82 | string[size-1] = c; |
---|
83 | string[size] = '\0'; |
---|
84 | ++size; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | bool comments( ifstream & in, char name[] ) { |
---|
89 | while () { |
---|
90 | in | name; |
---|
91 | if ( eof( in ) ) return true; |
---|
92 | if ( name[0] != '#' ) return false; |
---|
93 | in | nl; // ignore remainder of line |
---|
94 | } // while |
---|
95 | } // comments |
---|
96 | |
---|
97 | // Parse configuration from a file formatted in shell style |
---|
98 | KVPairs & parse_tabular_config_format( const char * config_file, size_t num_entries ) { |
---|
99 | KVPairs kv_pairs = { num_entries }; |
---|
100 | |
---|
101 | char key[64]; |
---|
102 | char value[256]; |
---|
103 | |
---|
104 | try { |
---|
105 | ifstream in = { config_file }; // open the configuration file for input |
---|
106 | |
---|
107 | while () { |
---|
108 | for ( Parmnum ) { // parameter names can appear in any order |
---|
109 | if ( comments( in, key ) ) break; // eof ? |
---|
110 | for ( posn = 0; posn < Parmnum && strcmp( name, parms[posn].name ) != 0; posn += 1 ); // linear search |
---|
111 | if ( posn == Parmnum ) break; // configuration not found ? |
---|
112 | if ( parms[posn].value != 0 ) break; // duplicate configuration ? |
---|
113 | in | value; |
---|
114 | |
---|
115 | add_kv_pair( kv_pairs, key, value ); |
---|
116 | |
---|
117 | if ( eof( in ) ) break; |
---|
118 | in | nl; // ignore remainder of line |
---|
119 | numOfParms += 1; |
---|
120 | parms[posn].value = value; |
---|
121 | } // for |
---|
122 | } catch( Open_Failure * ex; ex->istream == &in ) { |
---|
123 | ^kv_pairs{}; |
---|
124 | exit | "Error: could not open input file \"" | config_file | "\""; |
---|
125 | } // try |
---|
126 | close( in ); |
---|
127 | |
---|
128 | return kv_pairs; |
---|
129 | } |
---|
130 | |
---|
131 | // Parse configuration values from intermediate format |
---|
132 | void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) { |
---|
133 | KVPairs kv_pairs = parse_tabular_config_format( config_file, num_entries ); |
---|
134 | |
---|
135 | int entries_so_far = 0; |
---|
136 | for ( i; kv_pairs.size ) { |
---|
137 | if ( entries_so_far == num_entries ) break; |
---|
138 | |
---|
139 | char * src_key, * src_value; |
---|
140 | [ src_key, src_value ] = kv_pairs.data[i]; |
---|
141 | |
---|
142 | for ( j; num_entries ) { |
---|
143 | if ( strcmp( src_key, entries[j].key ) != 0 ) continue; |
---|
144 | if ( entries[j].parse( src_value, entries[j].variable ) ) { |
---|
145 | ++entries_so_far; |
---|
146 | |
---|
147 | // Validate the parsed data, if necessary |
---|
148 | if ( entries[j].validate != (bool (*)(void *))0p ) { |
---|
149 | if ( !entries[j].validate( entries[j].variable ) ) throwValidation_Failure( entries[j] ); |
---|
150 | } |
---|
151 | |
---|
152 | break; |
---|
153 | } |
---|
154 | |
---|
155 | serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed"; |
---|
156 | } |
---|
157 | } |
---|
158 | } // processConfigFile |
---|
159 | |
---|
160 | |
---|
161 | // *********************************** validation *********************************** |
---|
162 | |
---|
163 | |
---|
164 | forall(T | Relational( T )) |
---|
165 | bool is_nonnegative( T & value ) { |
---|
166 | T zero_val = 0; |
---|
167 | return value >= zero_val; |
---|
168 | } |
---|
169 | |
---|
170 | forall(T | Relational( T )) |
---|
171 | bool is_positive( T & value ) { |
---|
172 | T zero_val = 0; |
---|
173 | return value > zero_val; |
---|
174 | } |
---|
175 | |
---|
176 | forall(T | Relational( T )) |
---|
177 | bool is_nonpositive( T & value ) { |
---|
178 | T zero_val = 0; |
---|
179 | return value <= zero_val; |
---|
180 | } |
---|
181 | |
---|
182 | forall(T | Relational( T )) |
---|
183 | bool is_negative( T & value ) { |
---|
184 | T zero_val = 0; |
---|
185 | return value < zero_val; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | // Local Variables: // |
---|
190 | // tab-width: 4 // |
---|
191 | // compile-command: "cfa parseconfig.cfa" // |
---|
192 | // End: // |
---|