#include #include #include "parseconfig.hfa" struct KVPairs { int size, max_size; * [ char *, char * ] data; }; void ?{}( VLA & vla ) with ( vla ) { // default constructor size = 0; max_size = 0; data = 0p; } void ?{}( VLA & vla, int size, char fill = ['\0', '\0'] ) { // initialization vla.[ size, max_size, data ] = [ size, max_size, alloc( size, fill ) ]; } void ?{}( VLA & vla, VLA val ) with( val ) { // copy, deep vla.[ size, max_size, data ] = [ size, max_size, alloc( size, data ) ]; } void ^?{}( VLA & vla ) with ( vla ) { // 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 = 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 // Process the configuration file to set the simulation parameters. void parse_config( const char * config_file, config_entry entries[], 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; if ( value < 0 ) { close( in ); exit | "Error: file \"" | configFile | "\" parameter " | name | " value " | value | " must be non-negative."; } // if 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 ); // *** WE MUST ALLOW SOME SORT OF VALIDATION FUNCTIONALITY TOO!!! *** } // processConfigFile // Local Variables: // // tab-width: 4 // // compile-command: "cfa parseconfig.cfa" // // End: //