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
RevLine 
[1896c1f]1#include <fstream.hfa>
[8f01cb04]2#include <parseargs.hfa>
[716e4e6]3#include <stdlib.hfa>
4#include <string.h>
[16f9aca]5#include "parseconfig.hfa"
6
[80ae121]7
8// *********************************** exceptions ***********************************
9
10
[6d8e1ab]11static vtable(Parse_Failure) Parse_Failure_vt;
12
13void ?{}( Parse_Failure & this ) with ( this ) {
14        virtual_table = &Parse_Failure_vt;
15}
16
[d322f62]17static vtable(Validation_Failure) Validation_Failure_vt;
18
[6d8e1ab]19void ?{}( Validation_Failure & this ) with ( this ) {
20        virtual_table = &Validation_Failure_vt;
[80ae121]21}
22
23
24// *********************************** main code ***********************************
25
26
[5e0e488]27struct KVPairs {
[57dff2f]28        size_t size, max_size;
[5e0e488]29        * [ char *, char * ] data;
30};
[f62e741]31
[e74ba77]32void ?{}( KVPairs & kvp ) with ( kvp ) {                                // default constructor
[5e0e488]33        size = 0; max_size = 0; data = 0p;
34}
[f62e741]35
[e4b37a1]36void ?{}( KVPairs & kvp, size_t size ) {                                // initialization
[58ebd78]37        kvp.[ size, max_size ] = [ 0, size ];
38        kvp.data = alloc( size );
[5e0e488]39}
[f62e741]40
[e74ba77]41void ^?{}( KVPairs & kvp ) with ( kvp ) {                               // destructor
[5241ec2]42        for ( i; size ) free( data[i] );
[5e0e488]43        free( data );
44        size = 0; max_size = 0; data = 0p;
45}
46
[02a22a2]47void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) {
[ad78e08]48        if ( max_size == 0 ) {
49                max_size = 1;
50                data = alloc( max_size );
51        } else if ( size == max_size ) {
[58ebd78]52                max_size *= 2;
[cac1d52]53                data = alloc( max_size, data`realloc );
[5e0e488]54        }
55
[82820da]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 );
[5e0e488]60        ++size;
61}
62
[ba61cd9]63
[ecfa760]64bool comments( ifstream & in, char name[] ) {
[181ef73]65        while () {
[ecfa760]66                in | name;
67          if ( eof( in ) ) return true;
68          if ( name[0] != '#' ) return false;
69                in | nl;                                                                        // ignore remainder of line
70        } // while
[16f9aca]71} // comments
72
[33800ac8]73// Parse configuration from a file formatted in shell style
[d8db0af]74KVPairs * parse_tabular_config_format( const char * config_file, size_t num_entries ) {
[e4b37a1]75        * KVPairs kv_pairs = new( num_entries );
[5e0e488]76
[1e05e09]77        ifstream in;
[c2016b6]78        try {
[1e05e09]79                open( in, config_file );                                        // open the configuration file for input
[cfadd08]80
[12b5b226]81                char key[64];
82                char value[256];
[16f9aca]83
[12b5b226]84                while () {                                                                      // parameter names can appear in any order
[c1dafea]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 ?
[0cfa768]88                        in | value;
[d5cfc7e]89
[e4b37a1]90                        add_kv_pair( *kv_pairs, key, value );
[d5cfc7e]91
[8e0f2db]92                  if ( eof( in ) ) break;
[c1dafea]93                        in | nl;                                                                // ignore remainder of line
[16f9aca]94                } // for
[1896c1f]95        } catch( Open_Failure * ex; ex->istream == &in ) {
[4bdb7bf]96                delete( kv_pairs );
[6d8e1ab]97                serr | "Error: could not open input file '" | config_file | "'";
98                throw *ex;
[16f9aca]99        } // try
[ff3be413]100        close( in );
[e7e5b81]101
[d8db0af]102        return kv_pairs;
[33800ac8]103}
104
[58ebd78]105// Parse configuration values from intermediate format
[d8db0af]106void parse_config( const char * config_file, config_entry entries[], size_t num_entries, config_format format ) {
[f03b1db]107        KVPairs * kv_pairs = 0p;
[d8db0af]108        choose ( format ) {
109                case TABULAR_CONFIG:
110                        kv_pairs = parse_tabular_config_format( config_file, num_entries );
111        }
[33800ac8]112
[e7e5b81]113        int entries_so_far = 0;
[d8db0af]114        for ( i; kv_pairs->size ) {
[e7e5b81]115          if ( entries_so_far == num_entries ) break;
116
117                char * src_key, * src_value;
[d8db0af]118                [ src_key, src_value ] = kv_pairs->data[i];
[e7e5b81]119
120                for ( j; num_entries ) {
121                  if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
[a0d6987]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){};
[e7e5b81]126                        }
127
[a0d6987]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;
[e7e5b81]139                }
140        }
[d8db0af]141
[4bdb7bf]142        delete( kv_pairs );
[16f9aca]143} // processConfigFile
144
[e7e5b81]145
[930609e2]146// *********************************** validation ***********************************
147
148
[c58c65a]149forall(T | Relational( T ))
[930609e2]150bool is_nonnegative( T & value ) {
151        T zero_val = 0;
152        return value >= zero_val;
153}
154
[c58c65a]155forall(T | Relational( T ))
[930609e2]156bool is_positive( T & value ) {
157        T zero_val = 0;
158        return value > zero_val;
159}
160
[c58c65a]161forall(T | Relational( T ))
[930609e2]162bool is_nonpositive( T & value ) {
163        T zero_val = 0;
164        return value <= zero_val;
165}
166
[c58c65a]167forall(T | Relational( T ))
[930609e2]168bool is_negative( T & value ) {
169        T zero_val = 0;
170        return value < zero_val;
171}
172
173
[16f9aca]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.