source: libcfa/src/parseconfig.hfa @ 86841ee

Last change on this file since 86841ee was b5e725a, checked in by Peter A. Buhr <pabuhr@…>, 15 months ago

move setting the default random-generator size from PRNG.cfa to stdlib.hfa, change to the exception macros

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#pragma once
2
3#include <math.trait.hfa>
4#include <Exception.hfa>
5
6
7// *********************************** initial declarations ***********************************
8
9
10struct config_entry {
11        const char * key;
12        void * variable;
13        bool (*parse)( const char *, void * );
14        bool (*validate)( void * );
15};
16
17bool null_validator( void * ) { return true; }
18
19static inline void ?{}( config_entry & this ) {}
20
21forall(T & | { bool parse( const char *, T & ); })
22static inline void ?{}( config_entry & this, const char * key, T & variable ) {
23        this.key      = key;
24        this.variable = (void *)&variable;
25        this.parse    = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
26        this.validate = null_validator;
27}
28
29forall(T & | { bool parse( const char *, T & ); })
30static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*validate)(T &) ) {
31        this.key      = key;
32        this.variable = (void *)&variable;
33        this.parse    = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
34        this.validate = (bool (*)(void *))(bool (*)(T &))validate;
35}
36
37forall(T &)
38static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*parse)(const char *, T &) ) {
39        this.key      = key;
40        this.variable = (void *)&variable;
41        this.parse    = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
42        this.validate = null_validator;
43}
44
45forall(T &)
46static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*parse)(const char *, T &), bool (*validate)(T &) ) {
47        this.key      = key;
48        this.variable = (void *)&variable;
49        this.parse    = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
50        this.validate = (bool (*)(void *))(bool (*)(T &))validate;
51}
52
53// TODO: Replace KVPairs with vector2 when it's fully functional
54struct KVPairs {
55        size_t size, max_size;
56        * [ * char, * char ] data;
57};
58
59[ void ] add_kv_pair( & KVPairs kv_pairs, [] char key, [] char value );
60
61
62// *********************************** exceptions ***********************************
63
64
65ExceptionDecl( Missing_Config_Entries,
66        unsigned int num_missing;
67);
68
69[ void ] msg( * Missing_Config_Entries ex );
70
71ExceptionDecl( Parse_Failure,
72        * char failed_key;
73        * char failed_value;
74);
75
76[ void ] msg( * Parse_Failure ex );
77
78ExceptionDecl( Validation_Failure,
79        * char failed_key;
80        * char failed_value;
81);
82
83[ void ] msg( * Validation_Failure ex );
84
85
86// *********************************** main code ***********************************
87
88
89[ * KVPairs ] parse_tabular_config_format( [] const char config_file, size_t num_entries );
90
91[ void ] parse_config(
92        [] const char config_file,
93        [] config_entry entries,
94        size_t num_entries,
95        KVPairs * (*parser)(const char [], size_t)  // TODO: add sensible default parser when resolver bug is fixed
96);
97
98bool parse( const char *, const char * & );
99bool parse( const char *, int & );
100bool parse( const char *, unsigned & );
101bool parse( const char *, unsigned long & );
102bool parse( const char *, unsigned long long & );
103bool parse( const char *, float & );
104bool parse( const char *, double & );
105
106
107// *********************************** validation ***********************************
108
109
110forall(T | relational( T ))
111[ bool ] is_nonnegative( & T );
112
113forall(T | relational( T ))
114[ bool ] is_positive( & T );
115
116forall(T | relational( T ))
117[ bool ] is_nonpositive( & T );
118
119forall(T | relational( T ))
120[ bool ] is_negative( & T );
121
122
123// Local Variables: //
124// mode: c //
125// tab-width: 4 //
126// End: //
Note: See TracBrowser for help on using the repository browser.