1 | #pragma GCC diagnostic push |
---|
2 | //#pragma GCC diagnostic ignored "-Wunused-parameter" |
---|
3 | //#pragma GCC diagnostic ignored "-Wunused-function" |
---|
4 | //#pragma GCC diagnostic ignored "-Wuninitialized" |
---|
5 | //#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
---|
6 | |
---|
7 | #include <fstream.hfa> |
---|
8 | #include <parseargs.hfa> |
---|
9 | #include <stdlib.hfa> |
---|
10 | #include <string.h> |
---|
11 | #include "parseconfig.hfa" |
---|
12 | |
---|
13 | |
---|
14 | #pragma GCC visibility push(default) |
---|
15 | |
---|
16 | |
---|
17 | // *********************************** exceptions *********************************** |
---|
18 | |
---|
19 | |
---|
20 | [ void ] ?{}( & Missing_Config_Entries this, unsigned int num_missing ) { |
---|
21 | this.virtual_table = &Missing_Config_Entries_vt; |
---|
22 | this.num_missing = num_missing; |
---|
23 | } |
---|
24 | |
---|
25 | // TODO: use string interface when it's ready (and implement exception msg protocol) |
---|
26 | [ void ] msg( * Missing_Config_Entries ex ) { |
---|
27 | serr | "The config file is missing " | ex->num_missing | "entr" | nosep | (ex->num_missing == 1 ? "y." : "ies."); |
---|
28 | } // msg |
---|
29 | |
---|
30 | |
---|
31 | [ void ] ?{}( & Parse_Failure this, [] char failed_key, [] char failed_value ) { |
---|
32 | this.virtual_table = &Parse_Failure_vt; |
---|
33 | |
---|
34 | this.failed_key = alloc( strlen( failed_key ) ); |
---|
35 | this.failed_value = alloc( strlen( failed_value ) ); |
---|
36 | strcpy( this.failed_key, failed_key ); |
---|
37 | strcpy( this.failed_value, failed_value ); |
---|
38 | } |
---|
39 | |
---|
40 | [ void ] ^?{}( & Parse_Failure this ) with ( this ) { |
---|
41 | free( failed_key ); |
---|
42 | free( failed_value ); |
---|
43 | } |
---|
44 | |
---|
45 | // TODO: use string interface when it's ready (and implement exception msg protocol) |
---|
46 | [ void ] msg( * Parse_Failure ex ) { |
---|
47 | serr | "Config entry " | ex->failed_key | " could not be parsed. It has value " | ex->failed_value | "."; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | [ void ] ?{}( & Validation_Failure this, [] char failed_key, [] char failed_value ) { |
---|
52 | this.virtual_table = &Validation_Failure_vt; |
---|
53 | |
---|
54 | this.failed_key = alloc( strlen( failed_key ) ); |
---|
55 | this.failed_value = alloc( strlen( failed_value ) ); |
---|
56 | strcpy( this.failed_key, failed_key ); |
---|
57 | strcpy( this.failed_value, failed_value ); |
---|
58 | } |
---|
59 | |
---|
60 | [ void ] ^?{}( & Validation_Failure this ) with ( this ) { |
---|
61 | free( failed_key ); |
---|
62 | free( failed_value ); |
---|
63 | } |
---|
64 | |
---|
65 | // TODO: use string interface when it's ready (and implement exception msg protocol) |
---|
66 | [ void ] msg( * Validation_Failure ex ) { |
---|
67 | serr | "Config entry " | ex->failed_key | " could not be validated. It has value " | ex->failed_value | "."; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | // *********************************** main code *********************************** |
---|
72 | |
---|
73 | |
---|
74 | [ void ] ?{}( & KVPairs kvp ) with ( kvp ) { // default constructor |
---|
75 | size = 0; max_size = 0; data = 0p; |
---|
76 | } |
---|
77 | |
---|
78 | [ void ] ?{}( & KVPairs kvp, size_t size ) { // initialization |
---|
79 | kvp.[ size, max_size ] = [ 0, size ]; |
---|
80 | kvp.data = alloc( size ); |
---|
81 | } |
---|
82 | |
---|
83 | [ void ] ^?{}( & KVPairs kvp ) with ( kvp ) { // destructor |
---|
84 | for ( i; size ) free( data[i] ); |
---|
85 | free( data ); |
---|
86 | size = 0; max_size = 0; data = 0p; |
---|
87 | } |
---|
88 | |
---|
89 | [ void ] add_kv_pair( & KVPairs kv_pairs, [] char key, [] char value ) with ( kv_pairs ) { |
---|
90 | if ( max_size == 0 ) { |
---|
91 | max_size = 1; |
---|
92 | data = alloc( max_size ); |
---|
93 | } else if ( size == max_size ) { |
---|
94 | max_size *= 2; |
---|
95 | data = alloc( max_size, data`realloc ); |
---|
96 | } |
---|
97 | |
---|
98 | data[size].0 = alloc( strlen( key ) ); |
---|
99 | data[size].1 = alloc( strlen( value ) ); |
---|
100 | strcpy( data[size].0, key ); |
---|
101 | strcpy( data[size].1, value ); |
---|
102 | ++size; |
---|
103 | } // add_kv_pair |
---|
104 | |
---|
105 | |
---|
106 | static [ bool ] comments( & ifstream in, size_t size, [] char name ) { |
---|
107 | while () { |
---|
108 | in | wdi( size, name ); |
---|
109 | if ( eof( in ) ) return true; |
---|
110 | if ( name[0] != '#' ) return false; |
---|
111 | in | nl; // ignore remainder of line |
---|
112 | } // while |
---|
113 | } // comments |
---|
114 | |
---|
115 | // Parse configuration from a file formatted in tabular (CS 343) style |
---|
116 | [ * KVPairs ] parse_tabular_config_format( [] const char config_file, size_t num_entries ) { |
---|
117 | // TODO: Change this to a unique_ptr when we fully support returning them (move semantics) |
---|
118 | * KVPairs kv_pairs = new( num_entries ); |
---|
119 | |
---|
120 | ifstream in; |
---|
121 | try { |
---|
122 | open( in, config_file ); // open the configuration file for input |
---|
123 | |
---|
124 | [64] char key; |
---|
125 | [256] char value; |
---|
126 | |
---|
127 | while () { // parameter names can appear in any order |
---|
128 | // NOTE: Must add check to see if already read in value for this key, |
---|
129 | // once we switch to using hash table as intermediate storage |
---|
130 | if ( comments( in, 64, key ) ) break; // eof ? |
---|
131 | in | wdi( 256, value ); |
---|
132 | |
---|
133 | add_kv_pair( *kv_pairs, key, value ); |
---|
134 | |
---|
135 | if ( eof( in ) ) break; |
---|
136 | in | nl; // ignore remainder of line |
---|
137 | } // for |
---|
138 | } catch( open_failure * ex; ex->istream == &in ) { |
---|
139 | delete( kv_pairs ); |
---|
140 | throw *ex; |
---|
141 | } // try |
---|
142 | close( in ); |
---|
143 | |
---|
144 | return kv_pairs; |
---|
145 | } // parse_tabular_config_format |
---|
146 | |
---|
147 | // Parse configuration values from intermediate format |
---|
148 | [ void ] parse_config( |
---|
149 | [] const char config_file, |
---|
150 | [] config_entry entries, |
---|
151 | size_t num_entries, |
---|
152 | KVPairs * (*parser)(const char [], size_t) |
---|
153 | ) { |
---|
154 | * KVPairs kv_pairs = parser( config_file, num_entries ); |
---|
155 | |
---|
156 | int entries_so_far = 0; |
---|
157 | for ( i; kv_pairs->size ) { |
---|
158 | if ( entries_so_far == num_entries ) break; |
---|
159 | |
---|
160 | char * src_key, * src_value; |
---|
161 | [ src_key, src_value ] = kv_pairs->data[i]; |
---|
162 | |
---|
163 | for ( j; num_entries ) { |
---|
164 | if ( strcmp( src_key, entries[j].key ) != 0 ) continue; |
---|
165 | // Parse the data |
---|
166 | if ( !entries[j].parse( src_value, entries[j].variable ) ) { |
---|
167 | * Parse_Failure ex = new( src_key, src_value ); |
---|
168 | delete( kv_pairs ); |
---|
169 | throw *ex; |
---|
170 | } |
---|
171 | |
---|
172 | // Validate the data |
---|
173 | if ( !entries[j].validate( entries[j].variable ) ) { |
---|
174 | * Validation_Failure ex = new( src_key, src_value ); |
---|
175 | delete( kv_pairs ); |
---|
176 | throw *ex; |
---|
177 | } |
---|
178 | |
---|
179 | ++entries_so_far; |
---|
180 | |
---|
181 | break; |
---|
182 | } |
---|
183 | } |
---|
184 | // TODO: Once we get vector2+hash_table, we can more easily add the missing config keys to this error |
---|
185 | if ( entries_so_far < num_entries ) { |
---|
186 | delete( kv_pairs ); |
---|
187 | throw (Missing_Config_Entries){ num_entries - entries_so_far }; |
---|
188 | } |
---|
189 | |
---|
190 | delete( kv_pairs ); |
---|
191 | } // parse_config |
---|
192 | |
---|
193 | |
---|
194 | // *********************************** validation *********************************** |
---|
195 | |
---|
196 | |
---|
197 | forall(T | relational( T )) |
---|
198 | [ bool ] is_nonnegative( & T value ) { |
---|
199 | T zero_val = 0; |
---|
200 | return value >= zero_val; |
---|
201 | } |
---|
202 | |
---|
203 | forall(T | relational( T )) |
---|
204 | [ bool ] is_positive( & T value ) { |
---|
205 | T zero_val = 0; |
---|
206 | return value > zero_val; |
---|
207 | } |
---|
208 | |
---|
209 | forall(T | relational( T )) |
---|
210 | [ bool ] is_nonpositive( & T value ) { |
---|
211 | T zero_val = 0; |
---|
212 | return value <= zero_val; |
---|
213 | } |
---|
214 | |
---|
215 | forall(T | relational( T )) |
---|
216 | [ bool ] is_negative( & T value ) { |
---|
217 | T zero_val = 0; |
---|
218 | return value < zero_val; |
---|
219 | } |
---|
220 | #pragma GCC diagnostic pop |
---|
221 | |
---|
222 | |
---|
223 | // Local Variables: // |
---|
224 | // tab-width: 4 // |
---|
225 | // compile-command: "cfa parseconfig.cfa" // |
---|
226 | // End: // |
---|