source: libcfa/src/parseconfig.cfa@ c333ed2

Last change on this file since c333ed2 was 69e06ff, checked in by Peter A. Buhr <pabuhr@…>, 22 months ago

formatting

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