source: libcfa/src/parseconfig.cfa @ 7a2057a

ADTast-experimental
Last change on this file since 7a2057a was 4bb5d36, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

Visibility stragglers.
cfa_linkonce always means visibility default.

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