source: libcfa/src/parseconfig.cfa @ a75cd3d

ADTenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since a75cd3d was a75cd3d, checked in by Jacob Prud'homme <jafprudhomme@…>, 21 months ago

Added message function for exceptions I've created

Also removed constructor declarations from header file, as they are unnecessary

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