source: libcfa/src/parseconfig.cfa @ 33800ac8

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

Broke apart reading to intermediate format and parsing from intermediate

  • Property mode set to 100644
File size: 3.9 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 ] = [ 0, 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// Parse configuration from a file formatted in shell style
44        KVPairs kv_pairs{ num_entries };
45
46        ifstream in;
47        try {
48                open( in, config_file );                                                        // open the configuration file for input
49
50                while () {
51                        char * key;
52                        char * value;
53                  if ( comments( in, key ) ) break;                     // eof ?
54                        // Should we just overwrite duplicate config entries? Having a hash map would make this much easier
55                        in | value;
56
57                        add_kv_pair( kv_pairs, key, value );
58
59                  if ( fail( in ) ) break;
60                        in | nl; // ignore remainder of line
61                } // for
62        } catch( Open_Failure * ex; ex->istream == &in ) {
63                exit | "Error: could not open input file \"" | config_file | "\"";
64        } // try
65        close( in );
66
67        return kv_pairs;
68}
69
70// Process the configuration file to set the simulation parameters.
71void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) {
72        KVPairs kv_pairs = parse_shell_config_format( config_file );
73
74        // *** WE MUST ALLOW SOME SORT OF VALIDATION FUNCTIONALITY TOO!!! ***
75        int entries_so_far = 0;
76        for ( i; kv_pairs.size ) {
77          if ( entries_so_far == num_entries ) break;
78
79                char * src_key, * src_value;
80                [ src_key, src_value ] = kv_pairs.data[i];
81
82                for ( j; num_entries ) {
83                  if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
84                        if ( entries[j].parse( src_value, entries[j].variable ) ) {
85                                ++entries_so_far;
86                                break;
87                        }
88
89                        serr | "Value " | src_value | " for key " | dest_key | " could not be parsed";
90                }
91        }
92} // processConfigFile
93
94
95//-----------------------------------------------------------------------------
96// Typed argument parsing
97
98bool parse( const char * arg, const char * & value ) {
99        value = arg;
100        return true;
101}
102
103bool parse( const char * arg, int & value ) {
104        char * end;
105        int r = strtoll( arg, &end, 10 );
106  if ( *end != '\0' ) return false;
107
108        value = r;
109        return true;
110}
111
112bool parse( const char * arg, unsigned & value ) {
113        char * end;
114        unsigned long long int r = strtoull( arg, &end, 10 );
115  if ( *end != '\0' ) return false;
116  if ( r > (unsigned)MAX ) return false;
117
118        value = r;
119        return true;
120}
121
122bool parse( const char * arg, unsigned long & value ) {
123        char * end;
124        unsigned long long int r = strtoull( arg, &end, 10 );
125  if ( *end != '\0' ) return false;
126  if ( r > (unsigned long)MAX ) return false;
127
128        value = r;
129        return true;
130}
131
132bool parse( const char * arg, unsigned long long & value ) {
133        char * end;
134        unsigned long long int r = strtoull( arg, &end, 10 );
135  if ( *end != '\0' ) return false;
136  if ( r > (unsigned long long)MAX ) return false;
137
138        value = r;
139        return true;
140}
141
142bool parse( const char * arg, float & value ) {
143        char * end;
144        float r = strtof( arg, &end );
145  if ( *end != '\0' ) return false;
146
147        value = r;
148        return true;
149}
150
151bool parse( const char * arg, double & value ) {
152        char * end;
153        double r = strtod( arg, &end );
154  if ( *end != '\0' ) return false;
155
156        value = r;
157        return true;
158}
159
160
161// Local Variables: //
162// tab-width: 4 //
163// compile-command: "cfa parseconfig.cfa" //
164// End: //
Note: See TracBrowser for help on using the repository browser.