source: libcfa/src/parseconfig.cfa @ db19e1d

Last change on this file since db19e1d was 3ac5fd8, checked in by Peter A. Buhr <pabuhr@…>, 3 weeks ago

first attempt changing end-of-file to an exception

  • Property mode set to 100644
File size: 6.2 KB
Line 
1#pragma GCC diagnostic push
2//#pragma GCC diagnostic ignored "-Wunused-parameter"
3//#pragma GCC diagnostic ignored "-Wunused-function"
4//#pragma GCC diagnostic ignored "-Wuninitialized"
5//#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
6
7#include <fstream.hfa>
8#include <parseargs.hfa>
9#include <stdlib.hfa>
10#include <string.h>
11#include "parseconfig.hfa"
12
13
14#pragma GCC visibility push(default)
15
16
17// *********************************** exceptions ***********************************
18
19
20[ void ] ?{}( & Missing_Config_Entries this, unsigned int num_missing ) {
21        this.virtual_table = &Missing_Config_Entries_vt;
22        this.num_missing = num_missing;
23}
24
25// TODO: use string interface when it's ready (and implement exception msg protocol)
26[ void ] msg( * Missing_Config_Entries ex ) {
27        serr | "The config file is missing " | ex->num_missing | "entr" | nosep | (ex->num_missing == 1 ? "y." : "ies.");
28} // msg
29
30
31[ void ] ?{}( & Parse_Failure this, [] char failed_key, [] char failed_value ) {
32        this.virtual_table = &Parse_Failure_vt;
33
34        this.failed_key = alloc( strlen( failed_key ) );
35        this.failed_value = alloc( strlen( failed_value ) );
36        strcpy( this.failed_key, failed_key );
37        strcpy( this.failed_value, failed_value );
38}
39
40[ void ] ^?{}( & Parse_Failure this ) with ( this ) {
41        free( failed_key );
42        free( failed_value );
43}
44
45// TODO: use string interface when it's ready (and implement exception msg protocol)
46[ void ] msg( * Parse_Failure ex ) {
47        serr | "Config entry " | ex->failed_key | " could not be parsed. It has value " | ex->failed_value | ".";
48}
49
50
51[ void ] ?{}( & Validation_Failure this, [] char failed_key, [] char failed_value ) {
52        this.virtual_table = &Validation_Failure_vt;
53
54        this.failed_key = alloc( strlen( failed_key ) );
55        this.failed_value = alloc( strlen( failed_value ) );
56        strcpy( this.failed_key, failed_key );
57        strcpy( this.failed_value, failed_value );
58}
59
60[ void ] ^?{}( & Validation_Failure this ) with ( this ) {
61        free( failed_key );
62        free( failed_value );
63}
64
65// TODO: use string interface when it's ready (and implement exception msg protocol)
66[ void ] msg( * Validation_Failure ex ) {
67        serr | "Config entry " | ex->failed_key | " could not be validated. It has value " | ex->failed_value | ".";
68}
69
70
71// *********************************** main code ***********************************
72
73
74[ void ] ?{}( & KVPairs kvp ) with ( kvp ) {                    // default constructor
75        size = 0; max_size = 0; data = 0p;
76}
77
78[ void ] ?{}( & KVPairs kvp, size_t size ) {                    // initialization
79        kvp.[ size, max_size ] = [ 0, size ];
80        kvp.data = alloc( size );
81}
82
83[ void ] ^?{}( & KVPairs kvp ) with ( kvp ) {                   // destructor
84        for ( i; size ) free( data[i] );
85        free( data );
86        size = 0; max_size = 0; data = 0p;
87}
88
89[ void ] add_kv_pair( & KVPairs kv_pairs, [] char key, [] char value ) with ( kv_pairs ) {
90        if ( max_size == 0 ) {
91                max_size = 1;
92                data = alloc( max_size );
93        } else if ( size == max_size ) {
94                max_size *= 2;
95                data = alloc( max_size, data`realloc );
96        }
97
98        data[size].0 = alloc( strlen( key ) );
99        data[size].1 = alloc( strlen( value ) );
100        strcpy( data[size].0, key );
101        strcpy( data[size].1, value );
102        ++size;
103} // add_kv_pair
104
105
106static [ bool ] comments( & ifstream in, size_t size, [] char name ) {
107        bool comment = false;
108        try {
109                while () {
110                        in | wdi( size, name );
111                        if ( name[0] != '#' ) break;
112                        in | nl;                                                                        // ignore remainder of line
113                } // while
114        } catch( end_of_file * ) {
115                comment = true;
116        } // try
117        return comment;
118} // comments
119
120// Parse configuration from a file formatted in tabular (CS 343) style
121[ * KVPairs ] parse_tabular_config_format( [] const char config_file, size_t num_entries ) {
122        // TODO: Change this to a unique_ptr when we fully support returning them (move semantics)
123        * KVPairs kv_pairs = new( num_entries );
124
125        ifstream in;
126        try {
127                open( in, config_file );                                                // open the configuration file for input
128
129                [64] char key;
130                [256] char value;
131
132                try {
133                        while () {                                                                              // parameter names can appear in any order
134                                // NOTE: Must add check to see if already read in value for this key,
135                                // once we switch to using hash table as intermediate storage
136                                if ( comments( in, 64, key ) ) break;                   // eof ?
137                                in | wdi( 256, value );
138
139                                add_kv_pair( *kv_pairs, key, value );
140
141                                in | nl;                                                                        // ignore remainder of line
142                        } // while
143                } catch( end_of_file * ) {
144                } // try
145        } catch( open_failure * ex; ex->istream == &in ) {
146                delete( kv_pairs );
147                throw *ex;
148        } // try
149        close( in );
150
151        return kv_pairs;
152} // parse_tabular_config_format
153
154// Parse configuration values from intermediate format
155[ void ] parse_config(
156                [] const char config_file,
157                [] config_entry entries,
158                size_t num_entries,
159                KVPairs * (*parser)(const char [], size_t)
160) {
161        * KVPairs kv_pairs = parser( config_file, num_entries );
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} // parse_config
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#pragma GCC diagnostic pop
228
229
230// Local Variables: //
231// tab-width: 4 //
232// compile-command: "cfa parseconfig.cfa" //
233// End: //
Note: See TracBrowser for help on using the repository browser.