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