source: libcfa/src/parseconfig.cfa @ 82820da

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

Fixed issue caused by reuse of string buffers

  • Property mode set to 100644
File size: 3.9 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}
36void ^?{}( KVPairs & kvp ) with ( kvp ) {                               // destructor
37        free( data );
38        size = 0; max_size = 0; data = 0p;
39}
40
41void add_kv_pair( KVPairs & kv_pairs, char * k, char * v ) with ( kv_pairs ) {
42        if ( size == max_size ) {
43                max_size *= 2;
44                data = resize( data, max_size );
45        }
46
47        data[size].0 = alloc( strlen( k ) );
48        data[size].1 = alloc( strlen( v ) );
49        strcpy( data[size].0, k );
50        strcpy( data[size].1, v );
51        ++size;
52}
53
54
55bool comments( ifstream & in, char name[] ) {
56        while () {
57                in | name;
58          if ( eof( in ) ) return true;
59          if ( name[0] != '#' ) return false;
60                in | nl;                                                                        // ignore remainder of line
61        } // while
62} // comments
63
64// Parse configuration from a file formatted in shell style
65KVPairs & parse_tabular_config_format( const char * config_file, size_t num_entries ) {
66        KVPairs kv_pairs = { num_entries };
67
68        ifstream in;
69        try {
70                open( in, config_file );                                        // open the configuration file for input
71
72                while () {                                                                      // parameter names can appear in any order
73                        char key[64];
74                        char value[256];
75
76                        // Must add check to see if already read in a key-value pair,
77                        // once we switch to using hash table as intermediate storage
78                  if ( comments( in, key ) ) break;                     // eof ?
79                        in | value;
80
81                        add_kv_pair( kv_pairs, key, value );
82
83                  if ( eof( in ) ) break;
84                        in | nl;                                                                // ignore remainder of line
85                } // for
86        } catch( Open_Failure * ex; ex->istream == &in ) {
87                ^kv_pairs{};
88                exit | "Error: could not open input file \"" | config_file | "\"";
89        } // try
90        close( in );
91
92        return kv_pairs;
93}
94
95// Parse configuration values from intermediate format
96void parse_config( const char * config_file, config_entry entries[], size_t num_entries ) {
97        KVPairs kv_pairs = parse_tabular_config_format( config_file, num_entries );
98
99        int entries_so_far = 0;
100        for ( i; kv_pairs.size ) {
101          if ( entries_so_far == num_entries ) break;
102
103                char * src_key, * src_value;
104                [ src_key, src_value ] = kv_pairs.data[i];
105
106                for ( j; num_entries ) {
107                  if ( strcmp( src_key, entries[j].key ) != 0 ) continue;
108                        if ( entries[j].parse( src_value, entries[j].variable ) ) {
109                                ++entries_so_far;
110
111                                // Validate the parsed data, if necessary
112                                if ( entries[j].validate != (bool (*)(void *))0p ) {
113                                        if ( !entries[j].validate( entries[j].variable ) ) throwValidation_Failure( entries[j] );
114                                }
115
116                                break;
117                        }
118
119                        serr | "Value '" | src_value | "' for key '" | src_key | "' could not be parsed";
120                }
121        }
122} // processConfigFile
123
124
125// *********************************** validation ***********************************
126
127
128forall(T | Relational( T ))
129bool is_nonnegative( T & value ) {
130        T zero_val = 0;
131        return value >= zero_val;
132}
133
134forall(T | Relational( T ))
135bool is_positive( T & value ) {
136        T zero_val = 0;
137        return value > zero_val;
138}
139
140forall(T | Relational( T ))
141bool is_nonpositive( T & value ) {
142        T zero_val = 0;
143        return value <= zero_val;
144}
145
146forall(T | Relational( T ))
147bool is_negative( T & value ) {
148        T zero_val = 0;
149        return value < zero_val;
150}
151
152
153// Local Variables: //
154// tab-width: 4 //
155// compile-command: "cfa parseconfig.cfa" //
156// End: //
Note: See TracBrowser for help on using the repository browser.