[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] | 106 | static [ bool ] comments( & ifstream in, size_t size, [] char name ) {
|
---|
[3ac5fd8] | 107 | bool comment = false;
|
---|
| 108 | try {
|
---|
| 109 | while () {
|
---|
| 110 | in | wdi( size, name );
|
---|
| 111 | if ( name[0] != '#' ) break;
|
---|
| 112 | in | nl; // ignore remainder of line
|
---|
| 113 | } // while
|
---|
| 114 | } catch( end_of_file * ) {
|
---|
| 115 | comment = true;
|
---|
| 116 | } // try
|
---|
| 117 | return comment;
|
---|
[16f9aca] | 118 | } // comments
|
---|
| 119 |
|
---|
[32913bc] | 120 | // Parse configuration from a file formatted in tabular (CS 343) style
|
---|
[81e0c61] | 121 | [ * KVPairs ] 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 {
|
---|
[69e06ff] | 127 | open( in, config_file ); // open the configuration file for input
|
---|
[cfadd08] | 128 |
|
---|
[4a1bc44] | 129 | [64] char key;
|
---|
| 130 | [256] char value;
|
---|
[16f9aca] | 131 |
|
---|
[3ac5fd8] | 132 | try {
|
---|
| 133 | while () { // parameter names can appear in any order
|
---|
| 134 | // NOTE: Must add check to see if already read in value for this key,
|
---|
| 135 | // once we switch to using hash table as intermediate storage
|
---|
| 136 | if ( comments( in, 64, key ) ) break; // eof ?
|
---|
| 137 | in | wdi( 256, value );
|
---|
[d5cfc7e] | 138 |
|
---|
[3ac5fd8] | 139 | add_kv_pair( *kv_pairs, key, value );
|
---|
[d5cfc7e] | 140 |
|
---|
[3ac5fd8] | 141 | in | nl; // ignore remainder of line
|
---|
| 142 | } // while
|
---|
| 143 | } catch( end_of_file * ) {
|
---|
| 144 | } // try
|
---|
[874b16e] | 145 | } catch( open_failure * ex; ex->istream == &in ) {
|
---|
[4bdb7bf] | 146 | delete( kv_pairs );
|
---|
[6d8e1ab] | 147 | throw *ex;
|
---|
[16f9aca] | 148 | } // try
|
---|
[ff3be413] | 149 | close( in );
|
---|
[e7e5b81] | 150 |
|
---|
[d8db0af] | 151 | return kv_pairs;
|
---|
[45b772c] | 152 | } // parse_tabular_config_format
|
---|
[33800ac8] | 153 |
|
---|
[58ebd786] | 154 | // Parse configuration values from intermediate format
|
---|
[ca83227] | 155 | [ void ] parse_config(
|
---|
| 156 | [] const char config_file,
|
---|
| 157 | [] config_entry entries,
|
---|
| 158 | size_t num_entries,
|
---|
| 159 | KVPairs * (*parser)(const char [], size_t)
|
---|
| 160 | ) {
|
---|
| 161 | * KVPairs kv_pairs = parser( config_file, num_entries );
|
---|
[33800ac8] | 162 |
|
---|
[e7e5b81] | 163 | int entries_so_far = 0;
|
---|
[d8db0af] | 164 | for ( i; kv_pairs->size ) {
|
---|
[e7e5b81] | 165 | if ( entries_so_far == num_entries ) break;
|
---|
| 166 |
|
---|
| 167 | char * src_key, * src_value;
|
---|
[d8db0af] | 168 | [ src_key, src_value ] = kv_pairs->data[i];
|
---|
[e7e5b81] | 169 |
|
---|
| 170 | for ( j; num_entries ) {
|
---|
| 171 | if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
|
---|
[a3f2a3e] | 172 | // Parse the data
|
---|
[a0d6987] | 173 | if ( !entries[j].parse( src_value, entries[j].variable ) ) {
|
---|
[4a1bc44] | 174 | * Parse_Failure ex = new( src_key, src_value );
|
---|
[a0d6987] | 175 | delete( kv_pairs );
|
---|
[dcf792a] | 176 | throw *ex;
|
---|
[e7e5b81] | 177 | }
|
---|
| 178 |
|
---|
[a3f2a3e] | 179 | // Validate the data
|
---|
| 180 | if ( !entries[j].validate( entries[j].variable ) ) {
|
---|
[4a1bc44] | 181 | * Validation_Failure ex = new( src_key, src_value );
|
---|
[a0d6987] | 182 | delete( kv_pairs );
|
---|
[dcf792a] | 183 | throw *ex;
|
---|
[a0d6987] | 184 | }
|
---|
| 185 |
|
---|
| 186 | ++entries_so_far;
|
---|
| 187 |
|
---|
| 188 | break;
|
---|
[e7e5b81] | 189 | }
|
---|
| 190 | }
|
---|
[692db791] | 191 | // TODO: Once we get vector2+hash_table, we can more easily add the missing config keys to this error
|
---|
[e1e506b] | 192 | if ( entries_so_far < num_entries ) {
|
---|
| 193 | delete( kv_pairs );
|
---|
[3be4078] | 194 | throw (Missing_Config_Entries){ num_entries - entries_so_far };
|
---|
[e1e506b] | 195 | }
|
---|
[d8db0af] | 196 |
|
---|
[4bdb7bf] | 197 | delete( kv_pairs );
|
---|
[45b772c] | 198 | } // parse_config
|
---|
[16f9aca] | 199 |
|
---|
[e7e5b81] | 200 |
|
---|
[930609e2] | 201 | // *********************************** validation ***********************************
|
---|
| 202 |
|
---|
| 203 |
|
---|
[6a93e4d] | 204 | forall(T | relational( T ))
|
---|
[4a1bc44] | 205 | [ bool ] is_nonnegative( & T value ) {
|
---|
[930609e2] | 206 | T zero_val = 0;
|
---|
| 207 | return value >= zero_val;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[6a93e4d] | 210 | forall(T | relational( T ))
|
---|
[4a1bc44] | 211 | [ bool ] is_positive( & T value ) {
|
---|
[930609e2] | 212 | T zero_val = 0;
|
---|
| 213 | return value > zero_val;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[6a93e4d] | 216 | forall(T | relational( T ))
|
---|
[4a1bc44] | 217 | [ bool ] is_nonpositive( & T value ) {
|
---|
[930609e2] | 218 | T zero_val = 0;
|
---|
| 219 | return value <= zero_val;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[6a93e4d] | 222 | forall(T | relational( T ))
|
---|
[4a1bc44] | 223 | [ bool ] is_negative( & T value ) {
|
---|
[930609e2] | 224 | T zero_val = 0;
|
---|
| 225 | return value < zero_val;
|
---|
| 226 | }
|
---|
[86530e7] | 227 | #pragma GCC diagnostic pop
|
---|
[930609e2] | 228 |
|
---|
| 229 |
|
---|
[16f9aca] | 230 | // Local Variables: //
|
---|
| 231 | // tab-width: 4 //
|
---|
| 232 | // compile-command: "cfa parseconfig.cfa" //
|
---|
| 233 | // End: //
|
---|