source: libcfa/src/parseconfig.cfa@ 32913bc

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

Fixed up comments

  • Property mode set to 100644
File size: 5.8 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
14void ?{}( Missing_Config_Entries & this, unsigned int num_missing ) {
15 this.virtual_table = &Missing_Config_Entries_vt;
16 this.num_missing = num_missing;
17}
18
19void msg( Missing_Config_Entries * ex ) {
20 serr | "The config file is missing " | ex->num_missing | " entries.";
21}
22
23
24static vtable(Parse_Failure) Parse_Failure_vt;
25
26void ?{}( Parse_Failure & this, char * failed_key, char * failed_value ) {
27 this.virtual_table = &Parse_Failure_vt;
28
29 this.failed_key = alloc( strlen( failed_key ) );
30 this.failed_value = alloc( strlen( failed_value ) );
31 strcpy( this.failed_key, failed_key );
32 strcpy( this.failed_value, failed_value );
33}
34
35void ^?{}( Parse_Failure & this ) with ( this ) {
36 free( failed_key );
37 free( failed_value );
38}
39
40void msg( Parse_Failure * ex ) {
41 serr | "Config entry " | ex->failed_key | " could not be parsed. It has value " | ex->failed_value | ".";
42}
43
44
45static vtable(Validation_Failure) Validation_Failure_vt;
46
47void ?{}( Validation_Failure & this, char * failed_key, char * failed_value ) {
48 this.virtual_table = &Validation_Failure_vt;
49
50 this.failed_key = alloc( strlen( failed_key ) );
51 this.failed_value = alloc( strlen( failed_value ) );
52 strcpy( this.failed_key, failed_key );
53 strcpy( this.failed_value, failed_value );
54}
55
56void ^?{}( Validation_Failure & this ) with ( this ) {
57 free( failed_key );
58 free( failed_value );
59}
60
61void msg( Validation_Failure * ex ) {
62 serr | "Config entry " | ex->failed_key | " could not be validated. It has value " | ex->failed_value | ".";
63}
64
65
66// *********************************** main code ***********************************
67
68
69// TODO: Replace KVPairs with vector2 when it's fully functional
70struct KVPairs {
71 size_t size, max_size;
72 * [ char *, char * ] data;
73};
74
75void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor
76 size = 0; max_size = 0; data = 0p;
77}
78
79void ?{}( KVPairs & kvp, size_t size ) { // initialization
80 kvp.[ size, max_size ] = [ 0, size ];
81 kvp.data = alloc( size );
82}
83
84void ^?{}( KVPairs & kvp ) with ( kvp ) { // destructor
85 for ( i; size ) free( data[i] );
86 free( data );
87 size = 0; max_size = 0; data = 0p;
88}
89
90void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) {
91 if ( max_size == 0 ) {
92 max_size = 1;
93 data = alloc( max_size );
94 } else if ( size == max_size ) {
95 max_size *= 2;
96 data = alloc( max_size, data`realloc );
97 }
98
99 data[size].0 = alloc( strlen( k ) );
100 data[size].1 = alloc( strlen( v ) );
101 strcpy( data[size].0, k );
102 strcpy( data[size].1, v );
103 ++size;
104}
105
106
107bool comments( ifstream & in, char name[] ) {
108 while () {
109 in | name;
110 if ( eof( in ) ) return true;
111 if ( name[0] != '#' ) return false;
112 in | nl; // ignore remainder of line
113 } // while
114} // comments
115
116// Parse configuration from a file formatted in tabular (CS 343) style
117KVPairs * parse_tabular_config_format( const char * config_file, size_t num_entries ) {
118 // TODO: Change this to a unique_ptr when we fully support returning them (move semantics)
119 * KVPairs kv_pairs = new( num_entries );
120
121 ifstream in;
122 try {
123 open( in, config_file ); // open the configuration file for input
124
125 char key[64];
126 char value[256];
127
128 while () { // parameter names can appear in any order
129 // NOTE: Must add check to see if already read in value for this key,
130 // once we switch to using hash table as intermediate storage
131 if ( comments( in, key ) ) break; // eof ?
132 in | value;
133
134 add_kv_pair( *kv_pairs, key, value );
135
136 if ( eof( in ) ) break;
137 in | nl; // ignore remainder of line
138 } // for
139 } catch( Open_Failure * ex; ex->istream == &in ) {
140 delete( kv_pairs );
141 throw *ex;
142 } // try
143 close( in );
144
145 return kv_pairs;
146}
147
148// Parse configuration values from intermediate format
149void parse_config( const char * config_file, config_entry entries[], size_t num_entries, config_format format ) {
150 * KVPairs kv_pairs = 0p;
151 choose ( format ) {
152 case TABULAR_CONFIG:
153 kv_pairs = parse_tabular_config_format( config_file, num_entries );
154 }
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} // processConfigFile
192
193
194// *********************************** validation ***********************************
195
196
197forall(T | Relational( T ))
198bool is_nonnegative( T & value ) {
199 T zero_val = 0;
200 return value >= zero_val;
201}
202
203forall(T | Relational( T ))
204bool is_positive( T & value ) {
205 T zero_val = 0;
206 return value > zero_val;
207}
208
209forall(T | Relational( T ))
210bool is_nonpositive( T & value ) {
211 T zero_val = 0;
212 return value <= zero_val;
213}
214
215forall(T | Relational( T ))
216bool is_negative( T & value ) {
217 T zero_val = 0;
218 return value < zero_val;
219}
220
221
222// Local Variables: //
223// tab-width: 4 //
224// compile-command: "cfa parseconfig.cfa" //
225// End: //
Note: See TracBrowser for help on using the repository browser.