source: libcfa/src/parseconfig.cfa @ c1dafea

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

Cleaned up

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