source: libcfa/src/parseconfig.cfa@ d120290

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

Added comments where I'm having problems

  • Property mode set to 100644
File size: 4.3 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 {
[e4b37a1]26 unsigned int size, max_size;
[5e0e488]27 * [ char *, char * ] data;
28};
[e74ba77]29void ?{}( KVPairs & kvp ) with ( kvp ) { // default constructor
[5e0e488]30 size = 0; max_size = 0; data = 0p;
31}
[e4b37a1]32void ?{}( KVPairs & kvp, size_t size ) { // initialization
[58ebd786]33 kvp.[ size, max_size ] = [ 0, size ];
34 kvp.data = alloc( size );
[5e0e488]35}
[e74ba77]36void ^?{}( KVPairs & kvp ) with ( kvp ) { // destructor
[5e0e488]37 free( data );
38 size = 0; max_size = 0; data = 0p;
39}
40
[02a22a2]41void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) {
[ad78e08]42 if ( max_size == 0 ) {
43 max_size = 1;
44 data = alloc( max_size );
45 } else if ( size == max_size ) {
[58ebd786]46 max_size *= 2;
[d120290]47 data = resize( data, max_size ); // HERE (null pointer deref)
48 // data = realloc( data, max_size );
[5e0e488]49 }
50
[82820da]51 data[size].0 = alloc( strlen( k ) );
52 data[size].1 = alloc( strlen( v ) );
53 strcpy( data[size].0, k );
54 strcpy( data[size].1, v );
[5e0e488]55 ++size;
56}
57
[ba61cd9]58
[ecfa7607]59bool comments( ifstream & in, char name[] ) {
[181ef73]60 while () {
[ecfa7607]61 in | name;
62 if ( eof( in ) ) return true;
63 if ( name[0] != '#' ) return false;
64 in | nl; // ignore remainder of line
65 } // while
[16f9aca]66} // comments
67
[33800ac8]68// Parse configuration from a file formatted in shell style
[d8db0af]69KVPairs * parse_tabular_config_format( const char * config_file, size_t num_entries ) {
[e4b37a1]70 * KVPairs kv_pairs = new( num_entries );
[5e0e488]71
[1e05e09]72 ifstream in;
[c2016b6]73 try {
[1e05e09]74 open( in, config_file ); // open the configuration file for input
[cfadd08]75
[12b5b226]76 char key[64];
77 char value[256];
[16f9aca]78
[12b5b226]79 while () { // parameter names can appear in any order
[c1dafea]80 // Must add check to see if already read in a key-value pair,
81 // once we switch to using hash table as intermediate storage
82 if ( comments( in, key ) ) break; // eof ?
[0cfa768]83 in | value;
[d5cfc7e]84
[e4b37a1]85 add_kv_pair( *kv_pairs, key, value );
[d5cfc7e]86
[8e0f2db]87 if ( eof( in ) ) break;
[c1dafea]88 in | nl; // ignore remainder of line
[16f9aca]89 } // for
[1896c1f]90 } catch( Open_Failure * ex; ex->istream == &in ) {
[e4b37a1]91 ^(*kv_pairs){};
[a434e92]92 exit | "Error: could not open input file '" | config_file | "'";
[d120290]93 // HERE (unfreed storage)
[16f9aca]94 } // try
[ff3be413]95 close( in );
[e7e5b81]96
[d8db0af]97 return kv_pairs;
[33800ac8]98}
99
[58ebd786]100// Parse configuration values from intermediate format
[d8db0af]101void parse_config( const char * config_file, config_entry entries[], size_t num_entries, config_format format ) {
102 KVPairs * kv_pairs;
103 choose ( format ) {
104 case TABULAR_CONFIG:
105 kv_pairs = parse_tabular_config_format( config_file, num_entries );
106 default:
107 exit | "Error: config file format " | format | " is not supported";
108 }
[33800ac8]109
[e7e5b81]110 int entries_so_far = 0;
[d8db0af]111 for ( i; kv_pairs->size ) {
[e7e5b81]112 if ( entries_so_far == num_entries ) break;
113
114 char * src_key, * src_value;
[d8db0af]115 [ src_key, src_value ] = kv_pairs->data[i];
[e7e5b81]116
117 for ( j; num_entries ) {
118 if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
119 if ( entries[j].parse( src_value, entries[j].variable ) ) {
120 ++entries_so_far;
[6604ea1]121
122 // Validate the parsed data, if necessary
[98d6b744]123 if ( entries[j].validate != (bool (*)(void *))0p ) {
[6604ea1]124 if ( !entries[j].validate( entries[j].variable ) ) throwValidation_Failure( entries[j] );
125 }
126
[e7e5b81]127 break;
128 }
129
[58ebd786]130 serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed";
[e7e5b81]131 }
132 }
[d8db0af]133
134 ^(*kv_pairs){};
[d120290]135 // HERE (unfreed storage)
[16f9aca]136} // processConfigFile
137
[e7e5b81]138
[930609e2]139// *********************************** validation ***********************************
140
141
[c58c65a]142forall(T | Relational( T ))
[930609e2]143bool is_nonnegative( T & value ) {
144 T zero_val = 0;
145 return value >= zero_val;
146}
147
[c58c65a]148forall(T | Relational( T ))
[930609e2]149bool is_positive( T & value ) {
150 T zero_val = 0;
151 return value > zero_val;
152}
153
[c58c65a]154forall(T | Relational( T ))
[930609e2]155bool is_nonpositive( T & value ) {
156 T zero_val = 0;
157 return value <= zero_val;
158}
159
[c58c65a]160forall(T | Relational( T ))
[930609e2]161bool is_negative( T & value ) {
162 T zero_val = 0;
163 return value < zero_val;
164}
165
166
[16f9aca]167// Local Variables: //
168// tab-width: 4 //
169// compile-command: "cfa parseconfig.cfa" //
170// End: //
Note: See TracBrowser for help on using the repository browser.