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