source: libcfa/src/parseconfig.cfa @ d5cfc7e

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since d5cfc7e was d5cfc7e, checked in by Jacob Prud'homme <jafprudhomme@…>, 3 years ago

Began using intermediate format

  • Property mode set to 100644
File size: 2.0 KB
Line 
1#include <fstream.hfa>
2#include <heap.hfa>
3#include "parseconfig.hfa"
4
5struct KVPairs {
6        int size, max_size;
7        * [ char *, char * ] data;
8};
9void ?{}( VLA & vla ) with ( vla ) {    // default constructor
10        size = 0; max_size = 0; data = 0p;
11}
12void ?{}( VLA & vla, int size, char fill = ['\0', '\0'] ) { // initialization
13        vla.[ size, max_size, data ] = [ size, max_size, alloc( size, fill ) ];
14}
15void ?{}( VLA & vla, VLA val ) with( val ) { // copy, deep
16        vla.[ size, max_size, data ] = [ size, max_size, alloc( size, data ) ];
17}
18void ^?{}( VLA & vla ) with ( vla ) {   // destructor
19        free( data );
20        size = 0; max_size = 0; data = 0p;
21}
22
23void 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
33bool 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.
44void 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
58                        add_kv_pair( kv_pairs, key, value );
59
60                  if ( fail( in ) ) break;
61                        in | nl; // ignore remainder of line
62                } // for
63        } catch( Open_Failure * ex; ex->istream == &in ) {
64                exit | "Error: could not open input file \"" | config_file | "\"";
65        } // try
66        close( in );
67        // *** WE MUST ALLOW SOME SORT OF VALIDATION FUNCTIONALITY TOO!!! ***
68} // processConfigFile
69
70// Local Variables: //
71// tab-width: 4 //
72// compile-command: "cfa parseconfig.cfa" //
73// End: //
Note: See TracBrowser for help on using the repository browser.