[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 | |
---|
| 11 | EHM_VIRTUAL_TABLE(Validation_Failure, Validation_Failure_main_table); |
---|
| 12 | void ?{}( Validation_Failure & this, config_entry & entry ) with ( entry ) { |
---|
| 13 | this.virtual_table = &Validation_Failure_main_table; |
---|
| 14 | this.key = key; |
---|
| 15 | this.variable = variable; |
---|
| 16 | } |
---|
| 17 | void throwValidation_Failure( config_entry & entry ) { |
---|
| 18 | Validation_Failure exc = { entry }; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | // *********************************** main code *********************************** |
---|
| 23 | |
---|
| 24 | |
---|
[5e0e488] | 25 | struct KVPairs { |
---|
| 26 | int size, max_size; |
---|
| 27 | * [ char *, char * ] data; |
---|
| 28 | }; |
---|
[e74ba77] | 29 | void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor |
---|
[5e0e488] | 30 | size = 0; max_size = 0; data = 0p; |
---|
| 31 | } |
---|
[e74ba77] | 32 | void ?{}( KVPairs & kvp, int size ) { // initialization |
---|
[58ebd78] | 33 | kvp.[ size, max_size ] = [ 0, size ]; |
---|
| 34 | kvp.data = alloc( size ); |
---|
[5e0e488] | 35 | } |
---|
[e74ba77] | 36 | /* |
---|
| 37 | void ?{}( KVPairs & kvp, KVPairs val ) with( val ) { // copy, deep |
---|
| 38 | kvp.[ size, max_size ] = [ size, max_size ]; |
---|
| 39 | kvp.data = alloc( size, data ); |
---|
| 40 | } |
---|
| 41 | */ |
---|
| 42 | void ^?{}( KVPairs & kvp ) with ( kvp ) { // destructor |
---|
[5e0e488] | 43 | free( data ); |
---|
| 44 | size = 0; max_size = 0; data = 0p; |
---|
| 45 | } |
---|
| 46 | |
---|
[02a22a2] | 47 | void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) { |
---|
[5e0e488] | 48 | if ( size == max_size ) { |
---|
[58ebd78] | 49 | max_size *= 2; |
---|
[5e0e488] | 50 | data = resize( data, max_size ); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | data[size] = [ k, v ]; |
---|
| 54 | ++size; |
---|
| 55 | } |
---|
| 56 | |
---|
[ba61cd9] | 57 | |
---|
| 58 | struct StringBuilder { |
---|
| 59 | int size, max_size; |
---|
| 60 | * char string; |
---|
| 61 | }; |
---|
| 62 | void ?{}( StringBuilder & sb ) with ( sb ) { // default constructor |
---|
| 63 | size = 1; max_size = 1; string = alloc( 1 ); |
---|
| 64 | string[0] = '\0'; |
---|
| 65 | } |
---|
| 66 | void ?{}( StringBuilder & sb, int size ) { // initialization |
---|
| 67 | sb.[ size, max_size ] = [ 1, size+1 ]; |
---|
| 68 | sb.string = alloc( size ); |
---|
| 69 | sb.string[0] = '\0'; |
---|
| 70 | } |
---|
| 71 | void ^?{}( StringBuilder & sb ) with ( sb ) { // destructor |
---|
| 72 | free( string ); |
---|
| 73 | size = 0; max_size = 0; string = 0p; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void add_char( StringBuilder & sb, char c ) with ( sb ) { |
---|
| 77 | if ( size == max_size ) { |
---|
| 78 | max_size *= 2; |
---|
| 79 | string = resize( string, max_size ); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | string[size-1] = c; |
---|
| 83 | string[size] = '\0'; |
---|
| 84 | ++size; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
[2c2d32b] | 88 | bool comments( ifstream & in, char * name ) { |
---|
[ba61cd9] | 89 | StringBuilder sb; |
---|
| 90 | |
---|
| 91 | char c; |
---|
[181ef73] | 92 | while () { |
---|
[ba61cd9] | 93 | in | c; |
---|
| 94 | add_char( sb, c ); |
---|
| 95 | |
---|
| 96 | if ( fail( in ) ) { |
---|
| 97 | name = alloc( sb.size ); |
---|
| 98 | strcpy( name, sb.string ); |
---|
| 99 | |
---|
| 100 | return true; |
---|
| 101 | } |
---|
| 102 | if ( c != '#' ) break; |
---|
[1896c1f] | 103 | in | nl; // ignore remainder of line |
---|
[16f9aca] | 104 | } // for |
---|
[ba61cd9] | 105 | |
---|
| 106 | name = alloc( sb.size ); |
---|
| 107 | strcpy( name, sb.string ); |
---|
| 108 | |
---|
[16f9aca] | 109 | return false; |
---|
| 110 | } // comments |
---|
| 111 | |
---|
[33800ac8] | 112 | // Parse configuration from a file formatted in shell style |
---|
[45729a8] | 113 | KVPairs & parse_tabular_config_format( const char * config_file, size_t num_entries ) { |
---|
[02a22a2] | 114 | // * KVPairs kv_pairs; |
---|
| 115 | KVPairs kv_pairs = { num_entries }; |
---|
[5e0e488] | 116 | |
---|
[4df8fef5] | 117 | ifstream in; |
---|
[c2016b6] | 118 | try { |
---|
[4df8fef5] | 119 | open( in, config_file ); // open the configuration file for input |
---|
[16f9aca] | 120 | |
---|
[ff3be413] | 121 | while () { |
---|
[c80ac84] | 122 | // * char key; |
---|
| 123 | // * char value; |
---|
| 124 | // if ( comments( in, key ) ) break; // eof ? |
---|
| 125 | |
---|
| 126 | // THE CODE BELOW IS TEMPORARY, TO TRY AND GET SOMETHING WORKING |
---|
| 127 | |
---|
| 128 | // Right now doesn't handle duplicate keys. Should use hashmap for that |
---|
| 129 | char c; |
---|
| 130 | StringBuilder key_sb; |
---|
| 131 | StringBuilder value_sb; |
---|
| 132 | |
---|
| 133 | // Doesn't handle comments |
---|
| 134 | while () { |
---|
| 135 | in | c; |
---|
[a0e142f] | 136 | if ( c == ' ' || c == '\t' ) { |
---|
| 137 | while ( c == ' ' || c == '\t' ) in | c; |
---|
| 138 | break; |
---|
[c80ac84] | 139 | } else { |
---|
| 140 | add_char( key_sb, c ); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | * char key = alloc( key_sb.size ); |
---|
| 145 | strcpy( key, key_sb.string ); |
---|
| 146 | |
---|
| 147 | // Doesn't handle comments |
---|
| 148 | while () { |
---|
| 149 | in | c; |
---|
[a0e142f] | 150 | if ( c == ' ' || c == '\t' || c == '\n' ) break; |
---|
[c80ac84] | 151 | add_char( value_sb, c ); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | * char value = alloc( value_sb.size ); |
---|
| 155 | strcpy( value, value_sb.string ); |
---|
[d5cfc7e] | 156 | |
---|
[02a22a2] | 157 | add_kv_pair( kv_pairs, key, value ); |
---|
[d5cfc7e] | 158 | |
---|
[1896c1f] | 159 | if ( fail( in ) ) break; |
---|
| 160 | in | nl; // ignore remainder of line |
---|
[16f9aca] | 161 | } // for |
---|
[1896c1f] | 162 | } catch( Open_Failure * ex; ex->istream == &in ) { |
---|
[52c6891] | 163 | ^kv_pairs{}; |
---|
[4df8fef5] | 164 | exit | "Error: could not open input file \"" | config_file | "\""; |
---|
[16f9aca] | 165 | } // try |
---|
[ff3be413] | 166 | close( in ); |
---|
[e7e5b81] | 167 | |
---|
[02a22a2] | 168 | return kv_pairs; |
---|
[33800ac8] | 169 | } |
---|
| 170 | |
---|
[58ebd78] | 171 | // Parse configuration values from intermediate format |
---|
[33800ac8] | 172 | void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) { |
---|
[45729a8] | 173 | KVPairs kv_pairs = parse_tabular_config_format( config_file, num_entries ); |
---|
[33800ac8] | 174 | |
---|
[e7e5b81] | 175 | int entries_so_far = 0; |
---|
| 176 | for ( i; kv_pairs.size ) { |
---|
| 177 | if ( entries_so_far == num_entries ) break; |
---|
| 178 | |
---|
| 179 | char * src_key, * src_value; |
---|
| 180 | [ src_key, src_value ] = kv_pairs.data[i]; |
---|
| 181 | |
---|
| 182 | for ( j; num_entries ) { |
---|
| 183 | if ( strcmp( src_key, entries[j].key ) != 0 ) continue; |
---|
| 184 | if ( entries[j].parse( src_value, entries[j].variable ) ) { |
---|
| 185 | ++entries_so_far; |
---|
[6604ea1] | 186 | |
---|
| 187 | // Validate the parsed data, if necessary |
---|
[98d6b744] | 188 | if ( entries[j].validate != (bool (*)(void *))0p ) { |
---|
[6604ea1] | 189 | if ( !entries[j].validate( entries[j].variable ) ) throwValidation_Failure( entries[j] ); |
---|
| 190 | } |
---|
| 191 | |
---|
[e7e5b81] | 192 | break; |
---|
| 193 | } |
---|
| 194 | |
---|
[58ebd78] | 195 | serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed"; |
---|
[e7e5b81] | 196 | } |
---|
| 197 | } |
---|
[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: // |
---|