source: libcfa/src/parseconfig.cfa@ 98d6b744

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

Removed warnings

  • Property mode set to 100644
File size: 3.9 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
[58ebd786]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
[2c2d32b]57bool comments( ifstream & in, char * name ) {
[181ef73]58 while () {
[1896c1f]59 in | name;
60 if ( fail( in ) ) return true;
[2c2d32b]61 if ( name[0] != '#' ) break;
[1896c1f]62 in | nl; // ignore remainder of line
[16f9aca]63 } // for
64 return false;
65} // comments
66
[33800ac8]67// Parse configuration from a file formatted in shell style
[58ebd786]68KVPairs & parse_shell_config_format( const char * config_file, size_t num_entries ) {
[e74ba77]69 * KVPairs kv_pairs;// = { num_entries };
[5e0e488]70
[4df8fef5]71 ifstream in;
[c2016b6]72 try {
[4df8fef5]73 open( in, config_file ); // open the configuration file for input
[16f9aca]74
[ff3be413]75 while () {
[98d6b744]76 char * key = 0p;
77 char * value = 0p;
[4df8fef5]78 if ( comments( in, key ) ) break; // eof ?
[ff3be413]79 // Should we just overwrite duplicate config entries? Having a hash map would make this much easier
[1896c1f]80 in | value;
[d5cfc7e]81
[e74ba77]82 add_kv_pair( *kv_pairs, key, value );
[d5cfc7e]83
[1896c1f]84 if ( fail( in ) ) break;
85 in | nl; // ignore remainder of line
[16f9aca]86 } // for
[1896c1f]87 } catch( Open_Failure * ex; ex->istream == &in ) {
[52c6891]88 ^kv_pairs{};
[4df8fef5]89 exit | "Error: could not open input file \"" | config_file | "\"";
[16f9aca]90 } // try
[ff3be413]91 close( in );
[e7e5b81]92
[e74ba77]93 return *kv_pairs;
[33800ac8]94}
95
[58ebd786]96// Parse configuration values from intermediate format
[33800ac8]97void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) {
[58ebd786]98 KVPairs kv_pairs = parse_shell_config_format( config_file, num_entries );
[33800ac8]99
[e7e5b81]100 int entries_so_far = 0;
101 for ( i; kv_pairs.size ) {
102 if ( entries_so_far == num_entries ) break;
103
104 char * src_key, * src_value;
105 [ src_key, src_value ] = kv_pairs.data[i];
106
107 for ( j; num_entries ) {
108 if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
109 if ( entries[j].parse( src_value, entries[j].variable ) ) {
110 ++entries_so_far;
[6604ea1]111
112 // Validate the parsed data, if necessary
[98d6b744]113 if ( entries[j].validate != (bool (*)(void *))0p ) {
[6604ea1]114 if ( !entries[j].validate( entries[j].variable ) ) throwValidation_Failure( entries[j] );
115 }
116
[e7e5b81]117 break;
118 }
119
[58ebd786]120 serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed";
[e7e5b81]121 }
122 }
[16f9aca]123} // processConfigFile
124
[e7e5b81]125
[930609e2]126// *********************************** validation ***********************************
127
128
[c58c65a]129forall(T | Relational( T ))
[930609e2]130bool is_nonnegative( T & value ) {
131 T zero_val = 0;
132 return value >= zero_val;
133}
134
[c58c65a]135forall(T | Relational( T ))
[930609e2]136bool is_positive( T & value ) {
137 T zero_val = 0;
138 return value > zero_val;
139}
140
[c58c65a]141forall(T | Relational( T ))
[930609e2]142bool is_nonpositive( T & value ) {
143 T zero_val = 0;
144 return value <= zero_val;
145}
146
[c58c65a]147forall(T | Relational( T ))
[930609e2]148bool is_negative( T & value ) {
149 T zero_val = 0;
150 return value < zero_val;
151}
152
153
[16f9aca]154// Local Variables: //
155// tab-width: 4 //
156// compile-command: "cfa parseconfig.cfa" //
157// End: //
Note: See TracBrowser for help on using the repository browser.