source: libcfa/src/parseconfig.cfa@ cfadd08

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

Slimmed down copied code

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[1896c1f]1#include <fstream.hfa>
[8f01cb04]2#include <parseargs.hfa>
[716e4e6]3#include <stdlib.hfa>
4#include <string.h>
[16f9aca]5#include "parseconfig.hfa"
6
[80ae121]7
8// *********************************** exceptions ***********************************
9
10
11EHM_VIRTUAL_TABLE(Validation_Failure, Validation_Failure_main_table);
12void ?{}( Validation_Failure & this, config_entry & entry ) with ( entry ) {
13 this.virtual_table = &Validation_Failure_main_table;
14 this.key = key;
15 this.variable = variable;
16}
17void throwValidation_Failure( config_entry & entry ) {
18 Validation_Failure exc = { entry };
19}
20
21
22// *********************************** main code ***********************************
23
24
[5e0e488]25struct KVPairs {
26 int size, max_size;
27 * [ char *, char * ] data;
28};
[e74ba77]29void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor
[5e0e488]30 size = 0; max_size = 0; data = 0p;
31}
[e74ba77]32void ?{}( KVPairs & kvp, int size ) { // initialization
[58ebd786]33 kvp.[ size, max_size ] = [ 0, size ];
34 kvp.data = alloc( size );
[5e0e488]35}
[e74ba77]36/*
37void ?{}( KVPairs & kvp, KVPairs val ) with( val ) { // copy, deep
38 kvp.[ size, max_size ] = [ size, max_size ];
39 kvp.data = alloc( size, data );
40}
41*/
42void ^?{}( KVPairs & kvp ) with ( kvp ) { // destructor
[5e0e488]43 free( data );
44 size = 0; max_size = 0; data = 0p;
45}
46
[02a22a2]47void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) {
[5e0e488]48 if ( size == max_size ) {
[58ebd786]49 max_size *= 2;
[5e0e488]50 data = resize( data, max_size );
51 }
52
53 data[size] = [ k, v ];
54 ++size;
55}
56
[ba61cd9]57
58struct StringBuilder {
59 int size, max_size;
60 * char string;
61};
62void ?{}( StringBuilder & sb ) with ( sb ) { // default constructor
63 size = 1; max_size = 1; string = alloc( 1 );
64 string[0] = '\0';
65}
66void ?{}( StringBuilder & sb, int size ) { // initialization
67 sb.[ size, max_size ] = [ 1, size+1 ];
68 sb.string = alloc( size );
69 sb.string[0] = '\0';
70}
71void ^?{}( StringBuilder & sb ) with ( sb ) { // destructor
72 free( string );
73 size = 0; max_size = 0; string = 0p;
74}
75
76void add_char( StringBuilder & sb, char c ) with ( sb ) {
77 if ( size == max_size ) {
78 max_size *= 2;
79 string = resize( string, max_size );
80 }
81
82 string[size-1] = c;
83 string[size] = '\0';
84 ++size;
85}
86
87
[ecfa7607]88bool comments( ifstream & in, char name[] ) {
[181ef73]89 while () {
[ecfa7607]90 in | name;
91 if ( eof( in ) ) return true;
92 if ( name[0] != '#' ) return false;
93 in | nl; // ignore remainder of line
94 } // while
[16f9aca]95} // comments
96
[33800ac8]97// Parse configuration from a file formatted in shell style
[45729a8]98KVPairs & parse_tabular_config_format( const char * config_file, size_t num_entries ) {
[02a22a2]99 KVPairs kv_pairs = { num_entries };
[5e0e488]100
[c2016b6]101 try {
[cfadd08]102 ifstream in = { config_file }; // open the configuration file for input
103
104 while () { // parameter names can appear in any order
105 char key[64];
106 char value[256];
[16f9aca]107
[8e0f2db]108 if ( comments( in, key ) ) break; // eof ?
[cfadd08]109 // Must add check to see if we have already read in a key-value pair, once we switch to using hash table as intermediate storage
[0cfa768]110 in | value;
[d5cfc7e]111
[02a22a2]112 add_kv_pair( kv_pairs, key, value );
[d5cfc7e]113
[8e0f2db]114 if ( eof( in ) ) break;
115 in | nl; // ignore remainder of line
[16f9aca]116 } // for
[1896c1f]117 } catch( Open_Failure * ex; ex->istream == &in ) {
[52c6891]118 ^kv_pairs{};
[4df8fef5]119 exit | "Error: could not open input file \"" | config_file | "\"";
[16f9aca]120 } // try
[ff3be413]121 close( in );
[e7e5b81]122
[02a22a2]123 return kv_pairs;
[33800ac8]124}
125
[58ebd786]126// Parse configuration values from intermediate format
[33800ac8]127void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) {
[45729a8]128 KVPairs kv_pairs = parse_tabular_config_format( config_file, num_entries );
[33800ac8]129
[e7e5b81]130 int entries_so_far = 0;
131 for ( i; kv_pairs.size ) {
132 if ( entries_so_far == num_entries ) break;
133
134 char * src_key, * src_value;
135 [ src_key, src_value ] = kv_pairs.data[i];
136
137 for ( j; num_entries ) {
138 if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
139 if ( entries[j].parse( src_value, entries[j].variable ) ) {
140 ++entries_so_far;
[6604ea1]141
142 // Validate the parsed data, if necessary
[98d6b744]143 if ( entries[j].validate != (bool (*)(void *))0p ) {
[6604ea1]144 if ( !entries[j].validate( entries[j].variable ) ) throwValidation_Failure( entries[j] );
145 }
146
[e7e5b81]147 break;
148 }
149
[58ebd786]150 serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed";
[e7e5b81]151 }
152 }
[16f9aca]153} // processConfigFile
154
[e7e5b81]155
[930609e2]156// *********************************** validation ***********************************
157
158
[c58c65a]159forall(T | Relational( T ))
[930609e2]160bool is_nonnegative( T & value ) {
161 T zero_val = 0;
162 return value >= zero_val;
163}
164
[c58c65a]165forall(T | Relational( T ))
[930609e2]166bool is_positive( T & value ) {
167 T zero_val = 0;
168 return value > zero_val;
169}
170
[c58c65a]171forall(T | Relational( T ))
[930609e2]172bool is_nonpositive( T & value ) {
173 T zero_val = 0;
174 return value <= zero_val;
175}
176
[c58c65a]177forall(T | Relational( T ))
[930609e2]178bool is_negative( T & value ) {
179 T zero_val = 0;
180 return value < zero_val;
181}
182
183
[16f9aca]184// Local Variables: //
185// tab-width: 4 //
186// compile-command: "cfa parseconfig.cfa" //
187// End: //
Note: See TracBrowser for help on using the repository browser.