source: libcfa/src/parseconfig.cfa@ 3be4078

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

Added more info to "missing config entry" exception

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