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