source: libcfa/src/parseconfig.cfa @ f62e741

enumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since f62e741 was f62e741, checked in by Jacob Prud'homme <jafprudhomme@…>, 19 months ago

Added spaces

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