1 | #include <fstream.hfa> |
---|
2 | #include <heap.hfa> |
---|
3 | #include "parseconfig.hfa" |
---|
4 | |
---|
5 | struct KVPairs { |
---|
6 | int size, max_size; |
---|
7 | * [ char *, char * ] data; |
---|
8 | }; |
---|
9 | void ?{}( VLA & vla ) with ( vla ) { // default constructor |
---|
10 | size = 0; max_size = 0; data = 0p; |
---|
11 | } |
---|
12 | void ?{}( VLA & vla, int size, char fill = ['\0', '\0'] ) { // initialization |
---|
13 | vla.[ size, max_size, data ] = [ size, max_size, alloc( size, fill ) ]; |
---|
14 | } |
---|
15 | void ?{}( VLA & vla, VLA val ) with( val ) { // copy, deep |
---|
16 | vla.[ size, max_size, data ] = [ size, max_size, alloc( size, data ) ]; |
---|
17 | } |
---|
18 | void ^?{}( VLA & vla ) with ( vla ) { // destructor |
---|
19 | free( data ); |
---|
20 | size = 0; max_size = 0; data = 0p; |
---|
21 | } |
---|
22 | |
---|
23 | void add_kv_pair( KVPairs kv_pairs, char * k, char * v ) with( kv_pairs ) { |
---|
24 | if ( size == max_size ) { |
---|
25 | max_size = max_size * 2; |
---|
26 | data = resize( data, max_size ); |
---|
27 | } |
---|
28 | |
---|
29 | data[size] = [ k, v ]; |
---|
30 | ++size; |
---|
31 | } |
---|
32 | |
---|
33 | bool comments( ifstream & in, char * name ) { |
---|
34 | while () { |
---|
35 | in | name; |
---|
36 | if ( fail( in ) ) return true; |
---|
37 | if ( name[0] != '#' ) break; |
---|
38 | in | nl; // ignore remainder of line |
---|
39 | } // for |
---|
40 | return false; |
---|
41 | } // comments |
---|
42 | |
---|
43 | // Process the configuration file to set the simulation parameters. |
---|
44 | void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) { |
---|
45 | KVPairs kv_pairs{ num_entries }; |
---|
46 | |
---|
47 | ifstream in; |
---|
48 | try { |
---|
49 | open( in, config_file ); // open the configuration file for input |
---|
50 | |
---|
51 | while () { |
---|
52 | char * key; |
---|
53 | char * value; |
---|
54 | if ( comments( in, key ) ) break; // eof ? |
---|
55 | // Should we just overwrite duplicate config entries? Having a hash map would make this much easier |
---|
56 | in | value; |
---|
57 | if ( fail( in ) ) break; |
---|
58 | in | nl; // ignore remainder of line |
---|
59 | } // for |
---|
60 | } catch( Open_Failure * ex; ex->istream == &in ) { |
---|
61 | exit | "Error: could not open input file \"" | config_file | "\""; |
---|
62 | } // try |
---|
63 | close( in ); |
---|
64 | // *** WE MUST ALLOW SOME SORT OF VALIDATION FUNCTIONALITY TOO!!! *** |
---|
65 | } // processConfigFile |
---|
66 | |
---|
67 | // Local Variables: // |
---|
68 | // tab-width: 4 // |
---|
69 | // compile-command: "cfa parseconfig.cfa" // |
---|
70 | // End: // |
---|