source: libcfa/src/parseconfig.cfa@ ad8072c

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

Began using new math.trait.hfa

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