source: libcfa/src/parseconfig.cfa@ 4a1bc44

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation pthread-emulation qualifiedEnum
Last change on this file since 4a1bc44 was 4a1bc44, checked in by Jacob Prud'homme <jafprudhomme@…>, 4 years ago

Used in-English-order variable declaration syntax

  • Property mode set to 100644
File size: 6.0 KB
Line 
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)
12static 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[ void ] msg( * Missing_Config_Entries ex ) {
20 serr | nlOff;
21 serr | "The config file is missing " | ex->num_missing;
22 serr | nlOn;
23 if ( ex->num_missing == 1 ) {
24 serr | " entry.";
25 } else {
26 serr | " entries.";
27 }
28}
29
30
31static vtable(Parse_Failure) Parse_Failure_vt;
32
33[ void ] ?{}( & Parse_Failure this, [] char failed_key, [] char failed_value ) {
34 this.virtual_table = &Parse_Failure_vt;
35
36 this.failed_key = alloc( strlen( failed_key ) );
37 this.failed_value = alloc( strlen( failed_value ) );
38 strcpy( this.failed_key, failed_key );
39 strcpy( this.failed_value, failed_value );
40}
41
42[ void ] ^?{}( & Parse_Failure this ) with ( this ) {
43 free( failed_key );
44 free( failed_value );
45}
46
47[ void ] msg( * Parse_Failure ex ) {
48 serr | "Config entry " | ex->failed_key | " could not be parsed. It has value " | ex->failed_value | ".";
49}
50
51
52static vtable(Validation_Failure) Validation_Failure_vt;
53
54[ void ] ?{}( & Validation_Failure this, [] char failed_key, [] char failed_value ) {
55 this.virtual_table = &Validation_Failure_vt;
56
57 this.failed_key = alloc( strlen( failed_key ) );
58 this.failed_value = alloc( strlen( failed_value ) );
59 strcpy( this.failed_key, failed_key );
60 strcpy( this.failed_value, failed_value );
61}
62
63[ void ] ^?{}( & Validation_Failure this ) with ( this ) {
64 free( failed_key );
65 free( failed_value );
66}
67
68[ void ] msg( * Validation_Failure ex ) {
69 serr | "Config entry " | ex->failed_key | " could not be validated. It has value " | ex->failed_value | ".";
70}
71
72
73// *********************************** main code ***********************************
74
75
76// TODO: Replace KVPairs with vector2 when it's fully functional
77struct KVPairs {
78 size_t size, max_size;
79 * [ * char, * char ] data;
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}
112
113
114[ 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}
154
155// Parse configuration values from intermediate format
156[ void ] parse_config( [] const char config_file, [] config_entry entries, size_t num_entries, config_format format ) {
157 * KVPairs kv_pairs = 0p;
158 choose ( format ) {
159 case TABULAR_CONFIG:
160 kv_pairs = parse_tabular_config_format( config_file, num_entries );
161 }
162
163 int entries_so_far = 0;
164 for ( i; kv_pairs->size ) {
165 if ( entries_so_far == num_entries ) break;
166
167 char * src_key, * src_value;
168 [ src_key, src_value ] = kv_pairs->data[i];
169
170 for ( j; num_entries ) {
171 if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
172 // Parse the data
173 if ( !entries[j].parse( src_value, entries[j].variable ) ) {
174 * Parse_Failure ex = new( src_key, src_value );
175 delete( kv_pairs );
176 throw *ex;
177 }
178
179 // Validate the data
180 if ( !entries[j].validate( entries[j].variable ) ) {
181 * Validation_Failure ex = new( src_key, src_value );
182 delete( kv_pairs );
183 throw *ex;
184 }
185
186 ++entries_so_far;
187
188 break;
189 }
190 }
191 // TODO: Once we get vector2+hash_table, we can more easily add the missing config keys to this error
192 if ( entries_so_far < num_entries ) {
193 delete( kv_pairs );
194 throw (Missing_Config_Entries){ num_entries - entries_so_far };
195 }
196
197 delete( kv_pairs );
198} // processConfigFile
199
200
201// *********************************** validation ***********************************
202
203
204forall(T | Relational( T ))
205[ bool ] is_nonnegative( & T value ) {
206 T zero_val = 0;
207 return value >= zero_val;
208}
209
210forall(T | Relational( T ))
211[ bool ] is_positive( & T value ) {
212 T zero_val = 0;
213 return value > zero_val;
214}
215
216forall(T | Relational( T ))
217[ bool ] is_nonpositive( & T value ) {
218 T zero_val = 0;
219 return value <= zero_val;
220}
221
222forall(T | Relational( T ))
223[ bool ] is_negative( & T value ) {
224 T zero_val = 0;
225 return value < zero_val;
226}
227
228
229// Local Variables: //
230// tab-width: 4 //
231// compile-command: "cfa parseconfig.cfa" //
232// End: //
Note: See TracBrowser for help on using the repository browser.