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