source: libcfa/src/parseconfig.cfa @ c166f4e

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

Simplified validation logic

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