source: libcfa/src/parseconfig.cfa @ cac1d52

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since cac1d52 was cac1d52, checked in by Jacob Prud'homme <jafprudhomme@…>, 3 years ago

Fixed realloc error

  • 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        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 = alloc( max_size, data`realloc );
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                delete( 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 ) ) {
126                                                delete( kv_pairs );
127                                                throw (Validation_Failure){ entries[j] };
128                                        }
129                                }
130
131                                break;
132                        }
133
134                        serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed";
135                }
136        }
137
138        delete( kv_pairs );
139} // processConfigFile
140
141
142// *********************************** validation ***********************************
143
144
145forall(T | Relational( T ))
146bool is_nonnegative( T & value ) {
147        T zero_val = 0;
148        return value >= zero_val;
149}
150
151forall(T | Relational( T ))
152bool is_positive( T & value ) {
153        T zero_val = 0;
154        return value > zero_val;
155}
156
157forall(T | Relational( T ))
158bool is_nonpositive( T & value ) {
159        T zero_val = 0;
160        return value <= zero_val;
161}
162
163forall(T | Relational( T ))
164bool is_negative( T & value ) {
165        T zero_val = 0;
166        return value < zero_val;
167}
168
169
170// Local Variables: //
171// tab-width: 4 //
172// compile-command: "cfa parseconfig.cfa" //
173// End: //
Note: See TracBrowser for help on using the repository browser.