source: libcfa/src/parseconfig.cfa @ 5993b6a

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

Removed old comment

  • Property mode set to 100644
File size: 4.5 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(Unknown_Config_Format) Unknown_Config_Format_vt;
18
19void ?{}( Unknown_Config_Format & this ) with ( this ) {
20        virtual_table = &Unknown_Config_Format_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                serr | "Error: could not open input file '" | config_file | "'";
104                throw *ex;
105        } // try
106        close( in );
107
108        return kv_pairs;
109}
110
111// Parse configuration values from intermediate format
112void parse_config( const char * config_file, config_entry entries[], size_t num_entries, config_format format ) {
113        KVPairs * kv_pairs = 0p;
114        choose ( format ) {
115                case TABULAR_CONFIG:
116                        kv_pairs = parse_tabular_config_format( config_file, num_entries );
117                default:
118                        serr | "Error: config file format " | format | " is not supported";
119                        throw (Unknown_Config_Format){};
120        }
121
122        int entries_so_far = 0;
123        for ( i; kv_pairs->size ) {
124          if ( entries_so_far == num_entries ) break;
125
126                char * src_key, * src_value;
127                [ src_key, src_value ] = kv_pairs->data[i];
128
129                for ( j; num_entries ) {
130                  if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
131                        if ( !entries[j].parse( src_value, entries[j].variable ) ) {
132                                delete( kv_pairs );
133                                serr | "Error: value '" | src_value | "' for key '" | src_key | "' could not be parsed";
134                                throw (Parse_Failure){};
135                        }
136
137                        // Validate the parsed data, if necessary
138                        if ( entries[j].validate != (bool (*)(void *))0p
139                                        && !entries[j].validate( entries[j].variable ) ) {
140                                delete( kv_pairs );
141                                serr | "Error: config value at key '" | entries[j].key | "' did not pass validation";
142                                throw (Validation_Failure){};
143                        }
144
145                        ++entries_so_far;
146
147                        break;
148                }
149        }
150
151        delete( kv_pairs );
152} // processConfigFile
153
154
155// *********************************** validation ***********************************
156
157
158forall(T | Relational( T ))
159bool is_nonnegative( T & value ) {
160        T zero_val = 0;
161        return value >= zero_val;
162}
163
164forall(T | Relational( T ))
165bool is_positive( T & value ) {
166        T zero_val = 0;
167        return value > zero_val;
168}
169
170forall(T | Relational( T ))
171bool is_nonpositive( T & value ) {
172        T zero_val = 0;
173        return value <= zero_val;
174}
175
176forall(T | Relational( T ))
177bool is_negative( T & value ) {
178        T zero_val = 0;
179        return value < zero_val;
180}
181
182
183// Local Variables: //
184// tab-width: 4 //
185// compile-command: "cfa parseconfig.cfa" //
186// End: //
Note: See TracBrowser for help on using the repository browser.