source: libcfa/src/parseconfig.cfa @ ecfa760

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

Re-simplified code to detect lines with comments

  • Property mode set to 100644
File size: 5.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
11EHM_VIRTUAL_TABLE(Validation_Failure, Validation_Failure_main_table);
12void ?{}( Validation_Failure & this, config_entry & entry ) with ( entry ) {
13        this.virtual_table = &Validation_Failure_main_table;
14        this.key = key;
15        this.variable = variable;
16}
17void throwValidation_Failure( config_entry & entry ) {
18        Validation_Failure exc = { entry };
19}
20
21
22// *********************************** main code ***********************************
23
24
25struct KVPairs {
26        int size, max_size;
27        * [ char *, char * ] data;
28};
29void ?{}( KVPairs & kvp ) with ( kvp ) {                                // default constructor
30        size = 0; max_size = 0; data = 0p;
31}
32void ?{}( KVPairs & kvp, int size ) {                                   // initialization
33        kvp.[ size, max_size ] = [ 0, size ];
34        kvp.data = alloc( size );
35}
36/*
37void ?{}( KVPairs & kvp, KVPairs val ) with( val ) {    // copy, deep
38        kvp.[ size, max_size ] = [ size, max_size ];
39        kvp.data = alloc( size, data );
40}
41*/
42void ^?{}( KVPairs & kvp ) with ( kvp ) {                               // destructor
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 ( size == max_size ) {
49                max_size *= 2;
50                data = resize( data, max_size );
51        }
52
53        data[size] = [ k, v ];
54        ++size;
55}
56
57
58struct StringBuilder {
59        int size, max_size;
60        * char string;
61};
62void ?{}( StringBuilder & sb ) with ( sb ) {                            // default constructor
63        size = 1; max_size = 1; string = alloc( 1 );
64        string[0] = '\0';
65}
66void ?{}( StringBuilder & sb, int size ) {                                      // initialization
67        sb.[ size, max_size ] = [ 1, size+1 ];
68        sb.string = alloc( size );
69        sb.string[0] = '\0';
70}
71void ^?{}( StringBuilder & sb ) with ( sb ) {                           // destructor
72        free( string );
73        size = 0; max_size = 0; string = 0p;
74}
75
76void add_char( StringBuilder & sb, char c ) with ( sb ) {
77        if ( size == max_size ) {
78                max_size *= 2;
79                string = resize( string, max_size );
80        }
81
82        string[size-1] = c;
83        string[size] = '\0';
84        ++size;
85}
86
87
88bool comments( ifstream & in, char name[] ) {
89        while () {
90                in | name;
91          if ( eof( in ) ) return true;
92          if ( name[0] != '#' ) return false;
93                in | nl;                                                                        // ignore remainder of line
94        } // while
95} // comments
96
97// Parse configuration from a file formatted in shell style
98KVPairs & parse_tabular_config_format( const char * config_file, size_t num_entries ) {
99        // * KVPairs kv_pairs;
100        KVPairs kv_pairs = { num_entries };
101
102        try {
103                ifstream in = { config_file };                                          // open the configuration file for input
104
105                while () {
106                        // * char key;
107                        // * char value;
108                        // if ( comments( in, key ) ) break;                    // eof ?
109
110                        // THE CODE BELOW IS TEMPORARY, TO TRY AND GET SOMETHING WORKING
111
112                        // Right now doesn't handle duplicate keys. Should use hashmap for that
113                        char c;
114                        [1024] char key;
115                        [1024] char value;
116                        //StringBuilder key_sb;
117                        //StringBuilder value_sb;
118
119                        // Doesn't handle comments
120                        in | key;
121                        /*
122                        while () {
123                                in | c;
124                                if ( c == ' ' || c == '\t' ) {
125                                        while ( c == ' ' || c == '\t' ) in | c;
126                                        break;
127                                } else {
128                                        add_char( key_sb, c );
129                                }
130                        }
131                        */
132
133                        //* char key = alloc( key_sb.size );
134                        //strcpy( key, key_sb.string );
135
136                        // Doesn't handle comments
137                        in | value;
138                        /*
139                        while () {
140                                in | c;
141                          if ( c == ' ' || c == '\t' || c == '\n' ) break;
142                                add_char( value_sb, c );
143                        }
144                        */
145
146                        //* char value = alloc( value_sb.size );
147                        //strcpy( value, value_sb.string );
148
149                        add_kv_pair( kv_pairs, key, value );
150
151                  if ( fail( in ) ) break;
152                        in | nl; // ignore remainder of line
153                } // for
154        } catch( Open_Failure * ex; ex->istream == &in ) {
155                ^kv_pairs{};
156                exit | "Error: could not open input file \"" | config_file | "\"";
157        } // try
158        close( in );
159
160        return kv_pairs;
161}
162
163// Parse configuration values from intermediate format
164void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) {
165        KVPairs kv_pairs = parse_tabular_config_format( config_file, num_entries );
166
167        int entries_so_far = 0;
168        for ( i; kv_pairs.size ) {
169          if ( entries_so_far == num_entries ) break;
170
171                char * src_key, * src_value;
172                [ src_key, src_value ] = kv_pairs.data[i];
173
174                for ( j; num_entries ) {
175                  if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
176                        if ( entries[j].parse( src_value, entries[j].variable ) ) {
177                                ++entries_so_far;
178
179                                // Validate the parsed data, if necessary
180                                if ( entries[j].validate != (bool (*)(void *))0p ) {
181                                        if ( !entries[j].validate( entries[j].variable ) ) throwValidation_Failure( entries[j] );
182                                }
183
184                                break;
185                        }
186
187                        serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed";
188                }
189        }
190} // processConfigFile
191
192
193// *********************************** validation ***********************************
194
195
196forall(T | Relational( T ))
197bool is_nonnegative( T & value ) {
198        T zero_val = 0;
199        return value >= zero_val;
200}
201
202forall(T | Relational( T ))
203bool is_positive( T & value ) {
204        T zero_val = 0;
205        return value > zero_val;
206}
207
208forall(T | Relational( T ))
209bool is_nonpositive( T & value ) {
210        T zero_val = 0;
211        return value <= zero_val;
212}
213
214forall(T | Relational( T ))
215bool is_negative( T & value ) {
216        T zero_val = 0;
217        return value < zero_val;
218}
219
220
221// Local Variables: //
222// tab-width: 4 //
223// compile-command: "cfa parseconfig.cfa" //
224// End: //
Note: See TracBrowser for help on using the repository browser.