source: libcfa/src/parseconfig.cfa @ dd698b4

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since dd698b4 was dd698b4, checked in by Jacob Prud'homme <jafprudhomme@…>, 3 years ago

Added more TODOs

  • Property mode set to 100644
File size: 6.2 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// 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}
30
31
32static 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
54static 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// TODO: Replace KVPairs with vector2 when it's fully functional
80struct KVPairs {
81        size_t size, max_size;
82        * [ * char, * char ] data;
83};
84
85[ void ] ?{}( & KVPairs kvp ) with ( kvp ) {                            // default constructor
86        size = 0; max_size = 0; data = 0p;
87}
88
89[ void ] ?{}( & KVPairs kvp, size_t size ) {                            // initialization
90        kvp.[ size, max_size ] = [ 0, size ];
91        kvp.data = alloc( size );
92}
93
94[ void ] ^?{}( & KVPairs kvp ) with ( kvp ) {                           // destructor
95        for ( i; size ) free( data[i] );
96        free( data );
97        size = 0; max_size = 0; data = 0p;
98}
99
100[ void ] add_kv_pair( & KVPairs kv_pairs, [] char key, [] char value ) with ( kv_pairs ) {
101        if ( max_size == 0 ) {
102                max_size = 1;
103                data = alloc( max_size );
104        } else if ( size == max_size ) {
105                max_size *= 2;
106                data = alloc( max_size, data`realloc );
107        }
108
109        data[size].0 = alloc( strlen( key ) );
110        data[size].1 = alloc( strlen( value ) );
111        strcpy( data[size].0, key );
112        strcpy( data[size].1, value );
113        ++size;
114}
115
116
117[ bool ] comments( & ifstream in, [] char name ) {
118        while () {
119                in | name;
120          if ( eof( in ) ) return true;
121          if ( name[0] != '#' ) return false;
122                in | nl;                                                                        // ignore remainder of line
123        } // while
124} // comments
125
126// Parse configuration from a file formatted in tabular (CS 343) style
127[ * KVPairs ] parse_tabular_config_format( * const char config_file, size_t num_entries ) {
128        // TODO: Change this to a unique_ptr when we fully support returning them (move semantics)
129        * KVPairs kv_pairs = new( num_entries );
130
131        ifstream in;
132        try {
133                open( in, config_file );                                        // open the configuration file for input
134
135                [64] char key;
136                [256] char value;
137
138                while () {                                                                      // parameter names can appear in any order
139                        // NOTE: Must add check to see if already read in value for this key,
140                        // once we switch to using hash table as intermediate storage
141                  if ( comments( in, key ) ) break;                     // eof ?
142                        in | value;
143
144                        add_kv_pair( *kv_pairs, key, value );
145
146                  if ( eof( in ) ) break;
147                        in | nl;                                                                // ignore remainder of line
148                } // for
149        } catch( Open_Failure * ex; ex->istream == &in ) {
150                delete( kv_pairs );
151                throw *ex;
152        } // try
153        close( in );
154
155        return kv_pairs;
156}
157
158// Parse configuration values from intermediate format
159[ void ] parse_config( [] const char config_file, [] config_entry entries, size_t num_entries, config_format format ) {
160        * KVPairs kv_pairs = 0p;
161        choose ( format ) {
162                case TABULAR_CONFIG:
163                        kv_pairs = parse_tabular_config_format( config_file, num_entries );
164        }
165
166        int entries_so_far = 0;
167        for ( i; kv_pairs->size ) {
168          if ( entries_so_far == num_entries ) break;
169
170                char * src_key, * src_value;
171                [ src_key, src_value ] = kv_pairs->data[i];
172
173                for ( j; num_entries ) {
174                  if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
175                        // Parse the data
176                        if ( !entries[j].parse( src_value, entries[j].variable ) ) {
177                                * Parse_Failure ex = new( src_key, src_value );
178                                delete( kv_pairs );
179                                throw *ex;
180                        }
181
182                        // Validate the data
183                        if ( !entries[j].validate( entries[j].variable ) ) {
184                                * Validation_Failure ex = new( src_key, src_value );
185                                delete( kv_pairs );
186                                throw *ex;
187                        }
188
189                        ++entries_so_far;
190
191                        break;
192                }
193        }
194        // TODO: Once we get vector2+hash_table, we can more easily add the missing config keys to this error
195        if ( entries_so_far < num_entries ) {
196                delete( kv_pairs );
197                throw (Missing_Config_Entries){ num_entries - entries_so_far };
198        }
199
200        delete( kv_pairs );
201} // processConfigFile
202
203
204// *********************************** validation ***********************************
205
206
207forall(T | Relational( T ))
208[ bool ] is_nonnegative( & T value ) {
209        T zero_val = 0;
210        return value >= zero_val;
211}
212
213forall(T | Relational( T ))
214[ bool ] is_positive( & T value ) {
215        T zero_val = 0;
216        return value > zero_val;
217}
218
219forall(T | Relational( T ))
220[ bool ] is_nonpositive( & T value ) {
221        T zero_val = 0;
222        return value <= zero_val;
223}
224
225forall(T | Relational( T ))
226[ bool ] is_negative( & T value ) {
227        T zero_val = 0;
228        return value < zero_val;
229}
230
231
232// Local Variables: //
233// tab-width: 4 //
234// compile-command: "cfa parseconfig.cfa" //
235// End: //
Note: See TracBrowser for help on using the repository browser.