source: libcfa/src/parseconfig.cfa@ e7e5b81

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation pthread-emulation qualifiedEnum
Last change on this file since e7e5b81 was e7e5b81, checked in by Jacob Prud'homme <jafprudhomme@…>, 4 years ago

Added code to parse values

  • Property mode set to 100644
File size: 3.8 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
69 int entries_so_far = 0;
70 for ( i; kv_pairs.size ) {
71 if ( entries_so_far == num_entries ) break;
72
73 char * src_key, * src_value;
74 [ src_key, src_value ] = kv_pairs.data[i];
75
76 for ( j; num_entries ) {
77 if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
78 if ( entries[j].parse( src_value, entries[j].variable ) ) {
79 ++entries_so_far;
80 break;
81 }
82
83 serr | "Value " | src_value | " for key " | dest_key | " could not be parsed";
84 }
85 }
86} // processConfigFile
87
88
89//-----------------------------------------------------------------------------
90// Typed argument parsing
91
92bool parse( const char * arg, const char * & value ) {
93 value = arg;
94 return true;
95}
96
97bool parse( const char * arg, int & value ) {
98 char * end;
99 int r = strtoll( arg, &end, 10 );
100 if ( *end != '\0' ) return false;
101
102 value = r;
103 return true;
104}
105
106bool parse( const char * arg, unsigned & value ) {
107 char * end;
108 unsigned long long int r = strtoull( arg, &end, 10 );
109 if ( *end != '\0' ) return false;
110 if ( r > (unsigned)MAX ) return false;
111
112 value = r;
113 return true;
114}
115
116bool parse( const char * arg, unsigned long & value ) {
117 char * end;
118 unsigned long long int r = strtoull( arg, &end, 10 );
119 if ( *end != '\0' ) return false;
120 if ( r > (unsigned long)MAX ) return false;
121
122 value = r;
123 return true;
124}
125
126bool parse( const char * arg, unsigned long long & value ) {
127 char * end;
128 unsigned long long int r = strtoull( arg, &end, 10 );
129 if ( *end != '\0' ) return false;
130 if ( r > (unsigned long long)MAX ) return false;
131
132 value = r;
133 return true;
134}
135
136bool parse( const char * arg, float & value ) {
137 char * end;
138 float r = strtof( arg, &end );
139 if ( *end != '\0' ) return false;
140
141 value = r;
142 return true;
143}
144
145bool parse( const char * arg, double & value ) {
146 char * end;
147 double r = strtod( arg, &end );
148 if ( *end != '\0' ) return false;
149
150 value = r;
151 return true;
152}
153
154
155// Local Variables: //
156// tab-width: 4 //
157// compile-command: "cfa parseconfig.cfa" //
158// End: //
Note: See TracBrowser for help on using the repository browser.