source: libcfa/src/parseconfig.cfa@ d120290

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

Added comments where I'm having problems

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