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