| 1 | #include <fstream.hfa>
|
|---|
| 2 | #include <parseargs.hfa>
|
|---|
| 3 | #include <stdlib.hfa>
|
|---|
| 4 | #include <string.h>
|
|---|
| 5 | #include "parseconfig.hfa"
|
|---|
| 6 |
|
|---|
| 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 |
|
|---|
| 25 | struct KVPairs {
|
|---|
| 26 | unsigned int size, max_size;
|
|---|
| 27 | * [ char *, char * ] data;
|
|---|
| 28 | };
|
|---|
| 29 | void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor
|
|---|
| 30 | size = 0; max_size = 0; data = 0p;
|
|---|
| 31 | }
|
|---|
| 32 | void ?{}( KVPairs & kvp, size_t size ) { // initialization
|
|---|
| 33 | kvp.[ size, max_size ] = [ 0, size ];
|
|---|
| 34 | kvp.data = alloc( size );
|
|---|
| 35 | }
|
|---|
| 36 | void ^?{}( KVPairs & kvp ) with ( kvp ) { // destructor
|
|---|
| 37 | free( data );
|
|---|
| 38 | size = 0; max_size = 0; data = 0p;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) {
|
|---|
| 42 | if ( max_size == 0 ) {
|
|---|
| 43 | max_size = 1;
|
|---|
| 44 | data = alloc( max_size );
|
|---|
| 45 | } else if ( size == max_size ) {
|
|---|
| 46 | max_size *= 2;
|
|---|
| 47 | data = resize( data, max_size );
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | data[size].0 = alloc( strlen( k ) );
|
|---|
| 51 | data[size].1 = alloc( strlen( v ) );
|
|---|
| 52 | strcpy( data[size].0, k );
|
|---|
| 53 | strcpy( data[size].1, v );
|
|---|
| 54 | ++size;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | bool comments( ifstream & in, char name[] ) {
|
|---|
| 59 | while () {
|
|---|
| 60 | in | name;
|
|---|
| 61 | if ( eof( in ) ) return true;
|
|---|
| 62 | if ( name[0] != '#' ) return false;
|
|---|
| 63 | in | nl; // ignore remainder of line
|
|---|
| 64 | } // while
|
|---|
| 65 | } // comments
|
|---|
| 66 |
|
|---|
| 67 | // Parse configuration from a file formatted in shell style
|
|---|
| 68 | KVPairs & parse_tabular_config_format( const char * config_file, size_t num_entries ) {
|
|---|
| 69 | * KVPairs kv_pairs = new( num_entries );
|
|---|
| 70 |
|
|---|
| 71 | ifstream in;
|
|---|
| 72 | try {
|
|---|
| 73 | open( in, config_file ); // open the configuration file for input
|
|---|
| 74 |
|
|---|
| 75 | char key[64];
|
|---|
| 76 | char value[256];
|
|---|
| 77 |
|
|---|
| 78 | while () { // parameter names can appear in any order
|
|---|
| 79 | // Must add check to see if already read in a key-value pair,
|
|---|
| 80 | // once we switch to using hash table as intermediate storage
|
|---|
| 81 | if ( comments( in, key ) ) break; // eof ?
|
|---|
| 82 | in | value;
|
|---|
| 83 |
|
|---|
| 84 | add_kv_pair( *kv_pairs, key, value );
|
|---|
| 85 |
|
|---|
| 86 | if ( eof( in ) ) break;
|
|---|
| 87 | in | nl; // ignore remainder of line
|
|---|
| 88 | } // for
|
|---|
| 89 | } catch( Open_Failure * ex; ex->istream == &in ) {
|
|---|
| 90 | ^(*kv_pairs){};
|
|---|
| 91 | exit | "Error: could not open input file \"" | config_file | "\"";
|
|---|
| 92 | } // try
|
|---|
| 93 | close( in );
|
|---|
| 94 |
|
|---|
| 95 | return *kv_pairs;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | // Parse configuration values from intermediate format
|
|---|
| 99 | void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) {
|
|---|
| 100 | KVPairs & kv_pairs = parse_tabular_config_format( config_file, num_entries );
|
|---|
| 101 |
|
|---|
| 102 | int entries_so_far = 0;
|
|---|
| 103 | for ( i; kv_pairs.size ) {
|
|---|
| 104 | if ( entries_so_far == num_entries ) break;
|
|---|
| 105 |
|
|---|
| 106 | char * src_key, * src_value;
|
|---|
| 107 | [ src_key, src_value ] = kv_pairs.data[i];
|
|---|
| 108 |
|
|---|
| 109 | for ( j; num_entries ) {
|
|---|
| 110 | if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
|
|---|
| 111 | if ( entries[j].parse( src_value, entries[j].variable ) ) {
|
|---|
| 112 | ++entries_so_far;
|
|---|
| 113 |
|
|---|
| 114 | // Validate the parsed data, if necessary
|
|---|
| 115 | if ( entries[j].validate != (bool (*)(void *))0p ) {
|
|---|
| 116 | if ( !entries[j].validate( entries[j].variable ) ) throwValidation_Failure( entries[j] );
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | break;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed";
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | } // processConfigFile
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | // *********************************** validation ***********************************
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | forall(T | Relational( T ))
|
|---|
| 132 | bool is_nonnegative( T & value ) {
|
|---|
| 133 | T zero_val = 0;
|
|---|
| 134 | return value >= zero_val;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | forall(T | Relational( T ))
|
|---|
| 138 | bool is_positive( T & value ) {
|
|---|
| 139 | T zero_val = 0;
|
|---|
| 140 | return value > zero_val;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | forall(T | Relational( T ))
|
|---|
| 144 | bool is_nonpositive( T & value ) {
|
|---|
| 145 | T zero_val = 0;
|
|---|
| 146 | return value <= zero_val;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | forall(T | Relational( T ))
|
|---|
| 150 | bool is_negative( T & value ) {
|
|---|
| 151 | T zero_val = 0;
|
|---|
| 152 | return value < zero_val;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | // Local Variables: //
|
|---|
| 157 | // tab-width: 4 //
|
|---|
| 158 | // compile-command: "cfa parseconfig.cfa" //
|
|---|
| 159 | // End: //
|
|---|