source: libcfa/src/parseconfig.cfa @ 41d3c8d

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 41d3c8d was 86530e7, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

simplify IO statement

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