| 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 |  | 
|---|
| 9 | #include <fstream.hfa> | 
|---|
| 10 | #include <parseargs.hfa> | 
|---|
| 11 | #include <stdlib.hfa> | 
|---|
| 12 | #include <string.h> | 
|---|
| 13 | #include "parseconfig.hfa" | 
|---|
| 14 |  | 
|---|
| 15 |  | 
|---|
| 16 | #pragma GCC visibility push(default) | 
|---|
| 17 |  | 
|---|
| 18 |  | 
|---|
| 19 | // *********************************** exceptions *********************************** | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | [ void ] ?{}( & Missing_Config_Entries this, unsigned int num_missing ) { | 
|---|
| 23 | this.virtual_table = &Missing_Config_Entries_vt; | 
|---|
| 24 | this.num_missing = num_missing; | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | // TODO: use string interface when it's ready (and implement exception msg protocol) | 
|---|
| 28 | [ void ] msg( * Missing_Config_Entries ex ) { | 
|---|
| 29 | serr | "The config file is missing " | ex->num_missing | "entr" | nosep | (ex->num_missing == 1 ? "y." : "ies."); | 
|---|
| 30 | } // msg | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 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 ); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | [ void ] ^?{}( & Parse_Failure this ) with ( this ) { | 
|---|
| 43 | free( failed_key ); | 
|---|
| 44 | free( failed_value ); | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | // TODO: use string interface when it's ready (and implement exception msg protocol) | 
|---|
| 48 | [ void ] msg( * Parse_Failure ex ) { | 
|---|
| 49 | serr | "Config entry " | ex->failed_key | " could not be parsed. It has value " | ex->failed_value | "."; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | [ void ] ?{}( & Validation_Failure this, [] char failed_key, [] char failed_value ) { | 
|---|
| 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 |  | 
|---|
| 62 | [ void ] ^?{}( & Validation_Failure this ) with ( this ) { | 
|---|
| 63 | free( failed_key ); | 
|---|
| 64 | free( failed_value ); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | // TODO: use string interface when it's ready (and implement exception msg protocol) | 
|---|
| 68 | [ void ] msg( * Validation_Failure ex ) { | 
|---|
| 69 | serr | "Config entry " | ex->failed_key | " could not be validated. It has value " | ex->failed_value | "."; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 | // *********************************** main code *********************************** | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | [ void ] ?{}( & KVPairs kvp ) with ( kvp ) {                            // default constructor | 
|---|
| 77 | size = 0; max_size = 0; data = 0p; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | [ void ] ?{}( & KVPairs kvp, size_t size ) {                            // initialization | 
|---|
| 81 | kvp.[ size, max_size ] = [ 0, size ]; | 
|---|
| 82 | kvp.data = alloc( size ); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | [ void ] ^?{}( & KVPairs kvp ) with ( kvp ) {                           // destructor | 
|---|
| 86 | for ( i; size ) free( data[i] ); | 
|---|
| 87 | free( data ); | 
|---|
| 88 | size = 0; max_size = 0; data = 0p; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | [ void ] add_kv_pair( & KVPairs kv_pairs, [] char key, [] char value ) with ( kv_pairs ) { | 
|---|
| 92 | if ( max_size == 0 ) { | 
|---|
| 93 | max_size = 1; | 
|---|
| 94 | data = alloc( max_size ); | 
|---|
| 95 | } else if ( size == max_size ) { | 
|---|
| 96 | max_size *= 2; | 
|---|
| 97 | data = alloc( max_size, data`realloc ); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 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 ); | 
|---|
| 104 | ++size; | 
|---|
| 105 | } // add_kv_pair | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | static [ bool ] comments( & ifstream in, size_t size, [] char name ) { | 
|---|
| 109 | while () { | 
|---|
| 110 | in | wdi( size, name ); | 
|---|
| 111 | if ( eof( in ) ) return true; | 
|---|
| 112 | if ( name[0] != '#' ) return false; | 
|---|
| 113 | in | nl;                                                                        // ignore remainder of line | 
|---|
| 114 | } // while | 
|---|
| 115 | } // comments | 
|---|
| 116 |  | 
|---|
| 117 | // Parse configuration from a file formatted in tabular (CS 343) style | 
|---|
| 118 | [ * KVPairs ] parse_tabular_config_format( [] const char config_file, size_t num_entries ) { | 
|---|
| 119 | // TODO: Change this to a unique_ptr when we fully support returning them (move semantics) | 
|---|
| 120 | * KVPairs kv_pairs = new( num_entries ); | 
|---|
| 121 |  | 
|---|
| 122 | ifstream in; | 
|---|
| 123 | try { | 
|---|
| 124 | open( in, config_file );                                        // open the configuration file for input | 
|---|
| 125 |  | 
|---|
| 126 | [64] char key; | 
|---|
| 127 | [256] char value; | 
|---|
| 128 |  | 
|---|
| 129 | while () {                                                                      // parameter names can appear in any order | 
|---|
| 130 | // NOTE: Must add check to see if already read in value for this key, | 
|---|
| 131 | // once we switch to using hash table as intermediate storage | 
|---|
| 132 | if ( comments( in, 64, key ) ) break;   // eof ? | 
|---|
| 133 | in | wdi( 256, value ); | 
|---|
| 134 |  | 
|---|
| 135 | add_kv_pair( *kv_pairs, key, value ); | 
|---|
| 136 |  | 
|---|
| 137 | if ( eof( in ) ) break; | 
|---|
| 138 | in | nl;                                                                // ignore remainder of line | 
|---|
| 139 | } // for | 
|---|
| 140 | } catch( open_failure * ex; ex->istream == &in ) { | 
|---|
| 141 | delete( kv_pairs ); | 
|---|
| 142 | throw *ex; | 
|---|
| 143 | } // try | 
|---|
| 144 | close( in ); | 
|---|
| 145 |  | 
|---|
| 146 | return kv_pairs; | 
|---|
| 147 | } // parse_tabular_config_format | 
|---|
| 148 |  | 
|---|
| 149 | // Parse configuration values from intermediate format | 
|---|
| 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 ); | 
|---|
| 157 |  | 
|---|
| 158 | int entries_so_far = 0; | 
|---|
| 159 | for ( i; kv_pairs->size ) { | 
|---|
| 160 | if ( entries_so_far == num_entries ) break; | 
|---|
| 161 |  | 
|---|
| 162 | char * src_key, * src_value; | 
|---|
| 163 | [ src_key, src_value ] = kv_pairs->data[i]; | 
|---|
| 164 |  | 
|---|
| 165 | for ( j; num_entries ) { | 
|---|
| 166 | if ( strcmp( src_key, entries[j].key ) != 0 ) continue; | 
|---|
| 167 | // Parse the data | 
|---|
| 168 | if ( !entries[j].parse( src_value, entries[j].variable ) ) { | 
|---|
| 169 | * Parse_Failure ex = new( src_key, src_value ); | 
|---|
| 170 | delete( kv_pairs ); | 
|---|
| 171 | throw *ex; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | // Validate the data | 
|---|
| 175 | if ( !entries[j].validate( entries[j].variable ) ) { | 
|---|
| 176 | * Validation_Failure ex = new( src_key, src_value ); | 
|---|
| 177 | delete( kv_pairs ); | 
|---|
| 178 | throw *ex; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | ++entries_so_far; | 
|---|
| 182 |  | 
|---|
| 183 | break; | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 | // TODO: Once we get vector2+hash_table, we can more easily add the missing config keys to this error | 
|---|
| 187 | if ( entries_so_far < num_entries ) { | 
|---|
| 188 | delete( kv_pairs ); | 
|---|
| 189 | throw (Missing_Config_Entries){ num_entries - entries_so_far }; | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | delete( kv_pairs ); | 
|---|
| 193 | } // parse_config | 
|---|
| 194 |  | 
|---|
| 195 |  | 
|---|
| 196 | // *********************************** validation *********************************** | 
|---|
| 197 |  | 
|---|
| 198 |  | 
|---|
| 199 | forall(T | relational( T )) | 
|---|
| 200 | [ bool ] is_nonnegative( & T value ) { | 
|---|
| 201 | T zero_val = 0; | 
|---|
| 202 | return value >= zero_val; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | forall(T | relational( T )) | 
|---|
| 206 | [ bool ] is_positive( & T value ) { | 
|---|
| 207 | T zero_val = 0; | 
|---|
| 208 | return value > zero_val; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | forall(T | relational( T )) | 
|---|
| 212 | [ bool ] is_nonpositive( & T value ) { | 
|---|
| 213 | T zero_val = 0; | 
|---|
| 214 | return value <= zero_val; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | forall(T | relational( T )) | 
|---|
| 218 | [ bool ] is_negative( & T value ) { | 
|---|
| 219 | T zero_val = 0; | 
|---|
| 220 | return value < zero_val; | 
|---|
| 221 | } | 
|---|
| 222 | #pragma GCC diagnostic pop | 
|---|
| 223 |  | 
|---|
| 224 |  | 
|---|
| 225 | // Local Variables: // | 
|---|
| 226 | // tab-width: 4 // | 
|---|
| 227 | // compile-command: "cfa parseconfig.cfa" // | 
|---|
| 228 | // End: // | 
|---|