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