source: libcfa/src/parseconfig.cfa @ 24ebddac

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

Added default argument

  • Property mode set to 100644
File size: 6.1 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
[32913bc]11// TODO: Add names of missing config entries to exception (see further below)
[e1e506b]12static vtable(Missing_Config_Entries) Missing_Config_Entries_vt;
13
[4a1bc44]14[ void ] ?{}( & Missing_Config_Entries this, unsigned int num_missing ) {
[dcf792a]15        this.virtual_table = &Missing_Config_Entries_vt;
16        this.num_missing = num_missing;
[e1e506b]17}
18
[dd698b4]19// TODO: use string interface when it's ready (and implement exception msg protocol)
[4a1bc44]20[ void ] msg( * Missing_Config_Entries ex ) {
[e22c841]21        serr | nlOff;
22        serr | "The config file is missing " | ex->num_missing;
23        serr | nlOn;
24        if ( ex->num_missing == 1 ) {
25                serr | " entry.";
26        } else {
27                serr | " entries.";
28        }
[45b772c]29} // msg
[a75cd3d]30
[dcf792a]31
[6d8e1ab]32static vtable(Parse_Failure) Parse_Failure_vt;
33
[4a1bc44]34[ void ] ?{}( & Parse_Failure this, [] char failed_key, [] char failed_value ) {
[dcf792a]35        this.virtual_table = &Parse_Failure_vt;
36
37        this.failed_key = alloc( strlen( failed_key ) );
38        this.failed_value = alloc( strlen( failed_value ) );
39        strcpy( this.failed_key, failed_key );
40        strcpy( this.failed_value, failed_value );
[6d8e1ab]41}
42
[4a1bc44]43[ void ] ^?{}( & Parse_Failure this ) with ( this ) {
[dcf792a]44        free( failed_key );
45        free( failed_value );
46}
47
[dd698b4]48// TODO: use string interface when it's ready (and implement exception msg protocol)
[4a1bc44]49[ void ] msg( * Parse_Failure ex ) {
[a75cd3d]50        serr | "Config entry " | ex->failed_key | " could not be parsed. It has value " | ex->failed_value | ".";
51}
52
[dcf792a]53
[d322f62]54static vtable(Validation_Failure) Validation_Failure_vt;
55
[4a1bc44]56[ void ] ?{}( & Validation_Failure this, [] char failed_key, [] char failed_value ) {
[dcf792a]57        this.virtual_table = &Validation_Failure_vt;
58
59        this.failed_key = alloc( strlen( failed_key ) );
60        this.failed_value = alloc( strlen( failed_value ) );
61        strcpy( this.failed_key, failed_key );
62        strcpy( this.failed_value, failed_value );
63}
64
[4a1bc44]65[ void ] ^?{}( & Validation_Failure this ) with ( this ) {
[dcf792a]66        free( failed_key );
67        free( failed_value );
68}
[a75cd3d]69
[dd698b4]70// TODO: use string interface when it's ready (and implement exception msg protocol)
[4a1bc44]71[ void ] msg( * Validation_Failure ex ) {
[a75cd3d]72        serr | "Config entry " | ex->failed_key | " could not be validated. It has value " | ex->failed_value | ".";
[80ae121]73}
74
75
76// *********************************** main code ***********************************
77
78
[4a1bc44]79[ void ] ?{}( & KVPairs kvp ) with ( kvp ) {                            // default constructor
[5e0e488]80        size = 0; max_size = 0; data = 0p;
81}
[f62e741]82
[4a1bc44]83[ void ] ?{}( & KVPairs kvp, size_t size ) {                            // initialization
[58ebd78]84        kvp.[ size, max_size ] = [ 0, size ];
85        kvp.data = alloc( size );
[5e0e488]86}
[f62e741]87
[4a1bc44]88[ void ] ^?{}( & KVPairs kvp ) with ( kvp ) {                           // destructor
[5241ec2]89        for ( i; size ) free( data[i] );
[5e0e488]90        free( data );
91        size = 0; max_size = 0; data = 0p;
92}
93
[4a1bc44]94[ void ] add_kv_pair( & KVPairs kv_pairs, [] char key, [] char value ) with ( kv_pairs ) {
[ad78e08]95        if ( max_size == 0 ) {
96                max_size = 1;
97                data = alloc( max_size );
98        } else if ( size == max_size ) {
[58ebd78]99                max_size *= 2;
[cac1d52]100                data = alloc( max_size, data`realloc );
[5e0e488]101        }
102
[4a1bc44]103        data[size].0 = alloc( strlen( key ) );
104        data[size].1 = alloc( strlen( value ) );
105        strcpy( data[size].0, key );
106        strcpy( data[size].1, value );
[5e0e488]107        ++size;
[45b772c]108} // add_kv_pair
[5e0e488]109
[ba61cd9]110
[4a1bc44]111[ bool ] comments( & ifstream in, [] char name ) {
[181ef73]112        while () {
[ecfa760]113                in | name;
114          if ( eof( in ) ) return true;
115          if ( name[0] != '#' ) return false;
116                in | nl;                                                                        // ignore remainder of line
117        } // while
[16f9aca]118} // comments
119
[32913bc]120// Parse configuration from a file formatted in tabular (CS 343) style
[24ebddac]121KVPairs * parse_tabular_config_format( const char config_file[], size_t num_entries ) {
[692db791]122        // TODO: Change this to a unique_ptr when we fully support returning them (move semantics)
[e4b37a1]123        * KVPairs kv_pairs = new( num_entries );
[5e0e488]124
[1e05e09]125        ifstream in;
[c2016b6]126        try {
[1e05e09]127                open( in, config_file );                                        // open the configuration file for input
[cfadd08]128
[4a1bc44]129                [64] char key;
130                [256] char value;
[16f9aca]131
[12b5b226]132                while () {                                                                      // parameter names can appear in any order
[32913bc]133                        // NOTE: Must add check to see if already read in value for this key,
[c1dafea]134                        // once we switch to using hash table as intermediate storage
135                  if ( comments( in, key ) ) break;                     // eof ?
[0cfa768]136                        in | value;
[d5cfc7e]137
[e4b37a1]138                        add_kv_pair( *kv_pairs, key, value );
[d5cfc7e]139
[8e0f2db]140                  if ( eof( in ) ) break;
[c1dafea]141                        in | nl;                                                                // ignore remainder of line
[16f9aca]142                } // for
[1896c1f]143        } catch( Open_Failure * ex; ex->istream == &in ) {
[4bdb7bf]144                delete( kv_pairs );
[6d8e1ab]145                throw *ex;
[16f9aca]146        } // try
[ff3be413]147        close( in );
[e7e5b81]148
[d8db0af]149        return kv_pairs;
[45b772c]150} // parse_tabular_config_format
[33800ac8]151
[58ebd78]152// Parse configuration values from intermediate format
[ca83227]153[ void ] parse_config(
154                [] const char config_file,
155                [] config_entry entries,
156                size_t num_entries,
157                KVPairs * (*parser)(const char [], size_t)
158) {
159        * KVPairs kv_pairs = parser( config_file, num_entries );
[33800ac8]160
[e7e5b81]161        int entries_so_far = 0;
[d8db0af]162        for ( i; kv_pairs->size ) {
[e7e5b81]163          if ( entries_so_far == num_entries ) break;
164
165                char * src_key, * src_value;
[d8db0af]166                [ src_key, src_value ] = kv_pairs->data[i];
[e7e5b81]167
168                for ( j; num_entries ) {
169                  if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
[a3f2a3e]170                        // Parse the data
[a0d6987]171                        if ( !entries[j].parse( src_value, entries[j].variable ) ) {
[4a1bc44]172                                * Parse_Failure ex = new( src_key, src_value );
[a0d6987]173                                delete( kv_pairs );
[dcf792a]174                                throw *ex;
[e7e5b81]175                        }
176
[a3f2a3e]177                        // Validate the data
178                        if ( !entries[j].validate( entries[j].variable ) ) {
[4a1bc44]179                                * Validation_Failure ex = new( src_key, src_value );
[a0d6987]180                                delete( kv_pairs );
[dcf792a]181                                throw *ex;
[a0d6987]182                        }
183
184                        ++entries_so_far;
185
186                        break;
[e7e5b81]187                }
188        }
[692db791]189        // TODO: Once we get vector2+hash_table, we can more easily add the missing config keys to this error
[e1e506b]190        if ( entries_so_far < num_entries ) {
191                delete( kv_pairs );
[3be4078]192                throw (Missing_Config_Entries){ num_entries - entries_so_far };
[e1e506b]193        }
[d8db0af]194
[4bdb7bf]195        delete( kv_pairs );
[45b772c]196} // parse_config
[16f9aca]197
[e7e5b81]198
[930609e2]199// *********************************** validation ***********************************
200
201
[c58c65a]202forall(T | Relational( T ))
[4a1bc44]203[ bool ] is_nonnegative( & T value ) {
[930609e2]204        T zero_val = 0;
205        return value >= zero_val;
206}
207
[c58c65a]208forall(T | Relational( T ))
[4a1bc44]209[ bool ] is_positive( & T value ) {
[930609e2]210        T zero_val = 0;
211        return value > zero_val;
212}
213
[c58c65a]214forall(T | Relational( T ))
[4a1bc44]215[ bool ] is_nonpositive( & T value ) {
[930609e2]216        T zero_val = 0;
217        return value <= zero_val;
218}
219
[c58c65a]220forall(T | Relational( T ))
[4a1bc44]221[ bool ] is_negative( & T value ) {
[930609e2]222        T zero_val = 0;
223        return value < zero_val;
224}
225
226
[16f9aca]227// Local Variables: //
228// tab-width: 4 //
229// compile-command: "cfa parseconfig.cfa" //
230// End: //
Note: See TracBrowser for help on using the repository browser.