source: libcfa/src/parseconfig.cfa@ d322f62

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

Used more modern built-in EHM

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