#include #include #include "parseconfig.hfa" // *********************************** exceptions *********************************** EHM_VIRTUAL_TABLE(Validation_Failure, Validation_Failure_main_table); void ?{}( Validation_Failure & this, config_entry & entry ) with ( entry ) { this.virtual_table = &Validation_Failure_main_table; this.key = key; this.variable = variable; } void throwValidation_Failure( config_entry & entry ) { Validation_Failure exc = { entry }; } // *********************************** main code *********************************** struct KVPairs { int size, max_size; * [ char *, char * ] data; }; void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor size = 0; max_size = 0; data = 0p; } void ?{}( KVPairs & kvp, int size ) { // initialization kvp.[ size, max_size ] = [ 0, size ]; kvp.data = alloc( size ); } void ^?{}( KVPairs & kvp ) with ( kvp ) { // destructor free( data ); size = 0; max_size = 0; data = 0p; } void add_kv_pair( KVPairs kv_pairs, char * k, char * v ) with ( kv_pairs ) { if ( size == max_size ) { max_size *= 2; data = resize( data, max_size ); } data[size] = [ k, v ]; ++size; } bool comments( ifstream & in, char * name ) { while () { in | name; if ( fail( in ) ) return true; if ( name[0] != '#' ) break; in | nl; // ignore remainder of line } // for return false; } // comments // Parse configuration from a file formatted in shell style KVPairs & parse_shell_config_format( const char * config_file, size_t num_entries ) { KVPairs kv_pairs = { num_entries }; ifstream in; try { open( in, config_file ); // open the configuration file for input while () { char * key; char * value; if ( comments( in, key ) ) break; // eof ? // Should we just overwrite duplicate config entries? Having a hash map would make this much easier in | value; add_kv_pair( kv_pairs, key, value ); if ( fail( in ) ) break; in | nl; // ignore remainder of line } // for } catch( Open_Failure * ex; ex->istream == &in ) { exit | "Error: could not open input file \"" | config_file | "\""; } // try close( in ); return kv_pairs; } // Parse configuration values from intermediate format void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) { KVPairs kv_pairs = parse_shell_config_format( config_file, num_entries ); int entries_so_far = 0; for ( i; kv_pairs.size ) { if ( entries_so_far == num_entries ) break; char * src_key, * src_value; [ src_key, src_value ] = kv_pairs.data[i]; for ( j; num_entries ) { if ( strcmp( src_key, entries[j].key ) != 0 ) continue; if ( entries[j].parse( src_value, entries[j].variable ) ) { ++entries_so_far; break; } serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed"; } } } // processConfigFile // Local Variables: // // tab-width: 4 // // compile-command: "cfa parseconfig.cfa" // // End: //