| [1896c1f] | 1 | #include <fstream.hfa>
|
|---|
| [8f01cb04] | 2 | #include <parseargs.hfa>
|
|---|
| [716e4e6] | 3 | #include <stdlib.hfa>
|
|---|
| 4 | #include <string.h>
|
|---|
| [16f9aca] | 5 | #include "parseconfig.hfa"
|
|---|
| 6 |
|
|---|
| [80ae121] | 7 |
|
|---|
| 8 | // *********************************** exceptions ***********************************
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| [32913bc] | 11 | // TODO: Add names of missing config entries to exception (see further below)
|
|---|
| [e1e506b] | 12 | static vtable(Missing_Config_Entries) Missing_Config_Entries_vt;
|
|---|
| 13 |
|
|---|
| [dcf792a] | 14 | void ?{}( Missing_Config_Entries & this, unsigned int num_missing ) {
|
|---|
| 15 | this.virtual_table = &Missing_Config_Entries_vt;
|
|---|
| 16 | this.num_missing = num_missing;
|
|---|
| [e1e506b] | 17 | }
|
|---|
| 18 |
|
|---|
| [a75cd3d] | 19 | void msg( Missing_Config_Entries * ex ) {
|
|---|
| [e22c841] | 20 | serr | nlOff;
|
|---|
| 21 | serr | "The config file is missing " | ex->num_missing;
|
|---|
| 22 | serr | nlOn;
|
|---|
| 23 | if ( ex->num_missing == 1 ) {
|
|---|
| 24 | serr | " entry.";
|
|---|
| 25 | } else {
|
|---|
| 26 | serr | " entries.";
|
|---|
| 27 | }
|
|---|
| [a75cd3d] | 28 | }
|
|---|
| 29 |
|
|---|
| [dcf792a] | 30 |
|
|---|
| [6d8e1ab] | 31 | static vtable(Parse_Failure) Parse_Failure_vt;
|
|---|
| 32 |
|
|---|
| [dcf792a] | 33 | void ?{}( Parse_Failure & this, char * failed_key, char * failed_value ) {
|
|---|
| 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 |
|
|---|
| [dcf792a] | 42 | void ^?{}( Parse_Failure & this ) with ( this ) {
|
|---|
| 43 | free( failed_key );
|
|---|
| 44 | free( failed_value );
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| [a75cd3d] | 47 | void msg( Parse_Failure * ex ) {
|
|---|
| 48 | serr | "Config entry " | ex->failed_key | " could not be parsed. It has value " | ex->failed_value | ".";
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| [dcf792a] | 51 |
|
|---|
| [d322f62] | 52 | static vtable(Validation_Failure) Validation_Failure_vt;
|
|---|
| 53 |
|
|---|
| [dcf792a] | 54 | void ?{}( Validation_Failure & this, char * failed_key, char * failed_value ) {
|
|---|
| 55 | this.virtual_table = &Validation_Failure_vt;
|
|---|
| 56 |
|
|---|
| 57 | this.failed_key = alloc( strlen( failed_key ) );
|
|---|
| 58 | this.failed_value = alloc( strlen( failed_value ) );
|
|---|
| 59 | strcpy( this.failed_key, failed_key );
|
|---|
| 60 | strcpy( this.failed_value, failed_value );
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void ^?{}( Validation_Failure & this ) with ( this ) {
|
|---|
| 64 | free( failed_key );
|
|---|
| 65 | free( failed_value );
|
|---|
| 66 | }
|
|---|
| [a75cd3d] | 67 |
|
|---|
| 68 | void msg( Validation_Failure * ex ) {
|
|---|
| 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 |
|
|---|
| [692db791] | 76 | // TODO: Replace KVPairs with vector2 when it's fully functional
|
|---|
| [5e0e488] | 77 | struct KVPairs {
|
|---|
| [57dff2f] | 78 | size_t size, max_size;
|
|---|
| [5e0e488] | 79 | * [ char *, char * ] data;
|
|---|
| 80 | };
|
|---|
| [f62e741] | 81 |
|
|---|
| [e74ba77] | 82 | void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor
|
|---|
| [5e0e488] | 83 | size = 0; max_size = 0; data = 0p;
|
|---|
| 84 | }
|
|---|
| [f62e741] | 85 |
|
|---|
| [e4b37a1] | 86 | void ?{}( KVPairs & kvp, size_t size ) { // initialization
|
|---|
| [58ebd786] | 87 | kvp.[ size, max_size ] = [ 0, size ];
|
|---|
| 88 | kvp.data = alloc( size );
|
|---|
| [5e0e488] | 89 | }
|
|---|
| [f62e741] | 90 |
|
|---|
| [e74ba77] | 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 |
|
|---|
| [02a22a2] | 97 | void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) {
|
|---|
| [ad78e08] | 98 | if ( max_size == 0 ) {
|
|---|
| 99 | max_size = 1;
|
|---|
| 100 | data = alloc( max_size );
|
|---|
| 101 | } else if ( size == max_size ) {
|
|---|
| [58ebd786] | 102 | max_size *= 2;
|
|---|
| [cac1d52] | 103 | data = alloc( max_size, data`realloc );
|
|---|
| [5e0e488] | 104 | }
|
|---|
| 105 |
|
|---|
| [82820da] | 106 | data[size].0 = alloc( strlen( k ) );
|
|---|
| 107 | data[size].1 = alloc( strlen( v ) );
|
|---|
| 108 | strcpy( data[size].0, k );
|
|---|
| 109 | strcpy( data[size].1, v );
|
|---|
| [5e0e488] | 110 | ++size;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| [ba61cd9] | 113 |
|
|---|
| [ecfa7607] | 114 | bool comments( ifstream & in, char name[] ) {
|
|---|
| [181ef73] | 115 | while () {
|
|---|
| [ecfa7607] | 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
|
|---|
| [d8db0af] | 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 |
|
|---|
| [12b5b226] | 132 | char key[64];
|
|---|
| 133 | char value[256];
|
|---|
| [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;
|
|---|
| [33800ac8] | 153 | }
|
|---|
| 154 |
|
|---|
| [58ebd786] | 155 | // Parse configuration values from intermediate format
|
|---|
| [d8db0af] | 156 | void parse_config( const char * config_file, config_entry entries[], size_t num_entries, config_format format ) {
|
|---|
| [692db791] | 157 | * KVPairs kv_pairs = 0p;
|
|---|
| [d8db0af] | 158 | choose ( format ) {
|
|---|
| 159 | case TABULAR_CONFIG:
|
|---|
| 160 | kv_pairs = parse_tabular_config_format( config_file, num_entries );
|
|---|
| 161 | }
|
|---|
| [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 ) ) {
|
|---|
| [dcf792a] | 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 ) ) {
|
|---|
| [dcf792a] | 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 );
|
|---|
| [16f9aca] | 198 | } // processConfigFile
|
|---|
| 199 |
|
|---|
| [e7e5b81] | 200 |
|
|---|
| [930609e2] | 201 | // *********************************** validation ***********************************
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| [c58c65a] | 204 | forall(T | Relational( T ))
|
|---|
| [930609e2] | 205 | bool is_nonnegative( T & value ) {
|
|---|
| 206 | T zero_val = 0;
|
|---|
| 207 | return value >= zero_val;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| [c58c65a] | 210 | forall(T | Relational( T ))
|
|---|
| [930609e2] | 211 | bool is_positive( T & value ) {
|
|---|
| 212 | T zero_val = 0;
|
|---|
| 213 | return value > zero_val;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| [c58c65a] | 216 | forall(T | Relational( T ))
|
|---|
| [930609e2] | 217 | bool is_nonpositive( T & value ) {
|
|---|
| 218 | T zero_val = 0;
|
|---|
| 219 | return value <= zero_val;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| [c58c65a] | 222 | forall(T | Relational( T ))
|
|---|
| [930609e2] | 223 | bool is_negative( T & value ) {
|
|---|
| 224 | T zero_val = 0;
|
|---|
| 225 | return value < zero_val;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| [16f9aca] | 229 | // Local Variables: //
|
|---|
| 230 | // tab-width: 4 //
|
|---|
| 231 | // compile-command: "cfa parseconfig.cfa" //
|
|---|
| 232 | // End: //
|
|---|