source: libcfa/src/parseconfig.cfa @ dcf792a

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

Added more information to exceptions

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