source: libcfa/src/parseconfig.cfa @ e1e506b

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

Added exception for when there are missing config entries

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