source: libcfa/src/parseconfig.cfa@ 5241ec2

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

Freed space allocated for strings

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