source: libcfa/src/parseconfig.cfa @ 02a22a2

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

Reverted to using reference to KVPairs

This will have to be changed in the future as we are returning a
stack-allocated value out of scope

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