| 1 | #include <fstream.hfa>
|
|---|
| 2 | #include <parseargs.hfa>
|
|---|
| 3 | #include "parseconfig.hfa"
|
|---|
| 4 |
|
|---|
| 5 | struct KVPairs {
|
|---|
| 6 | int size, max_size;
|
|---|
| 7 | * [ char *, char * ] data;
|
|---|
| 8 | };
|
|---|
| 9 | void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor
|
|---|
| 10 | size = 0; max_size = 0; data = 0p;
|
|---|
| 11 | }
|
|---|
| 12 | void ?{}( KVPairs & kvp, int size ) { // initialization
|
|---|
| 13 | kvp.[ size, max_size ] = [ 0, size ];
|
|---|
| 14 | kvp.data = alloc( size );
|
|---|
| 15 | }
|
|---|
| 16 | void ^?{}( KVPairs & kvp ) with ( kvp ) { // destructor
|
|---|
| 17 | free( data );
|
|---|
| 18 | size = 0; max_size = 0; data = 0p;
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | void add_kv_pair( KVPairs kv_pairs, char * k, char * v ) with ( kv_pairs ) {
|
|---|
| 22 | if ( size == max_size ) {
|
|---|
| 23 | max_size *= 2;
|
|---|
| 24 | data = resize( data, max_size );
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | data[size] = [ k, v ];
|
|---|
| 28 | ++size;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | bool comments( ifstream & in, char * name ) {
|
|---|
| 32 | while () {
|
|---|
| 33 | in | name;
|
|---|
| 34 | if ( fail( in ) ) return true;
|
|---|
| 35 | if ( name[0] != '#' ) break;
|
|---|
| 36 | in | nl; // ignore remainder of line
|
|---|
| 37 | } // for
|
|---|
| 38 | return false;
|
|---|
| 39 | } // comments
|
|---|
| 40 |
|
|---|
| 41 | // Parse configuration from a file formatted in shell style
|
|---|
| 42 | KVPairs & parse_shell_config_format( const char * config_file, size_t num_entries ) {
|
|---|
| 43 | KVPairs kv_pairs = { num_entries };
|
|---|
| 44 |
|
|---|
| 45 | ifstream in;
|
|---|
| 46 | try {
|
|---|
| 47 | open( in, config_file ); // open the configuration file for input
|
|---|
| 48 |
|
|---|
| 49 | while () {
|
|---|
| 50 | char * key;
|
|---|
| 51 | char * value;
|
|---|
| 52 | if ( comments( in, key ) ) break; // eof ?
|
|---|
| 53 | // Should we just overwrite duplicate config entries? Having a hash map would make this much easier
|
|---|
| 54 | in | value;
|
|---|
| 55 |
|
|---|
| 56 | add_kv_pair( kv_pairs, key, value );
|
|---|
| 57 |
|
|---|
| 58 | if ( fail( in ) ) break;
|
|---|
| 59 | in | nl; // ignore remainder of line
|
|---|
| 60 | } // for
|
|---|
| 61 | } catch( Open_Failure * ex; ex->istream == &in ) {
|
|---|
| 62 | exit | "Error: could not open input file \"" | config_file | "\"";
|
|---|
| 63 | } // try
|
|---|
| 64 | close( in );
|
|---|
| 65 |
|
|---|
| 66 | return kv_pairs;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | // Parse configuration values from intermediate format
|
|---|
| 70 | void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) {
|
|---|
| 71 | KVPairs kv_pairs = parse_shell_config_format( config_file, num_entries );
|
|---|
| 72 |
|
|---|
| 73 | int entries_so_far = 0;
|
|---|
| 74 | for ( i; kv_pairs.size ) {
|
|---|
| 75 | if ( entries_so_far == num_entries ) break;
|
|---|
| 76 |
|
|---|
| 77 | char * src_key, * src_value;
|
|---|
| 78 | [ src_key, src_value ] = kv_pairs.data[i];
|
|---|
| 79 |
|
|---|
| 80 | for ( j; num_entries ) {
|
|---|
| 81 | if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
|
|---|
| 82 | if ( entries[j].parse( src_value, entries[j].variable ) ) {
|
|---|
| 83 | ++entries_so_far;
|
|---|
| 84 | break;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed";
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | } // processConfigFile
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | // Local Variables: //
|
|---|
| 94 | // tab-width: 4 //
|
|---|
| 95 | // compile-command: "cfa parseconfig.cfa" //
|
|---|
| 96 | // End: //
|
|---|