source: libcfa/src/parseconfig.cfa @ 247d4ac

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

Removed error for unknown config format

Since we're using an enum, we can only use the options we explicitly provided

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