[16f9aca] | 1 | #pragma once
|
---|
| 2 |
|
---|
[c58c65a] | 3 | #include <math.trait.hfa>
|
---|
[b5e725a] | 4 | #include <Exception.hfa>
|
---|
[80ae121] | 5 |
|
---|
| 6 |
|
---|
[930609e2] | 7 | // *********************************** initial declarations ***********************************
|
---|
[80ae121] | 8 |
|
---|
| 9 |
|
---|
[4df8fef5] | 10 | struct config_entry {
|
---|
| 11 | const char * key;
|
---|
| 12 | void * variable;
|
---|
[58ebd786] | 13 | bool (*parse)( const char *, void * );
|
---|
[e54654e] | 14 | bool (*validate)( void * );
|
---|
[4df8fef5] | 15 | };
|
---|
| 16 |
|
---|
[a3f2a3e] | 17 | bool null_validator( void * ) { return true; }
|
---|
| 18 |
|
---|
[4df8fef5] | 19 | static inline void ?{}( config_entry & this ) {}
|
---|
| 20 |
|
---|
[58ebd786] | 21 | forall(T & | { bool parse( const char *, T & ); })
|
---|
[4df8fef5] | 22 | static inline void ?{}( config_entry & this, const char * key, T & variable ) {
|
---|
| 23 | this.key = key;
|
---|
| 24 | this.variable = (void *)&variable;
|
---|
[4cc6c7d] | 25 | this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
|
---|
[c166f4e] | 26 | this.validate = null_validator;
|
---|
[e54654e] | 27 | }
|
---|
| 28 |
|
---|
| 29 | forall(T & | { bool parse( const char *, T & ); })
|
---|
| 30 | static 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;
|
---|
[4df8fef5] | 35 | }
|
---|
| 36 |
|
---|
| 37 | forall(T &)
|
---|
| 38 | static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*parse)(const char *, T &) ) {
|
---|
| 39 | this.key = key;
|
---|
| 40 | this.variable = (void *)&variable;
|
---|
[4cc6c7d] | 41 | this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
|
---|
[c166f4e] | 42 | this.validate = null_validator;
|
---|
[e54654e] | 43 | }
|
---|
| 44 |
|
---|
| 45 | forall(T &)
|
---|
| 46 | static 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;
|
---|
[4df8fef5] | 51 | }
|
---|
| 52 |
|
---|
[ca83227] | 53 | // TODO: Replace KVPairs with vector2 when it's fully functional
|
---|
| 54 | struct 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 |
|
---|
[930609e2] | 61 |
|
---|
| 62 | // *********************************** exceptions ***********************************
|
---|
| 63 |
|
---|
| 64 |
|
---|
[b5e725a] | 65 | ExceptionDecl( Missing_Config_Entries,
|
---|
[dcf792a] | 66 | unsigned int num_missing;
|
---|
[b5e725a] | 67 | );
|
---|
[e1e506b] | 68 |
|
---|
[b0f225f] | 69 | [ void ] msg( * Missing_Config_Entries ex );
|
---|
[e1e506b] | 70 |
|
---|
[b5e725a] | 71 | ExceptionDecl( Parse_Failure,
|
---|
[dcf792a] | 72 | * char failed_key;
|
---|
| 73 | * char failed_value;
|
---|
[b5e725a] | 74 | );
|
---|
[6d8e1ab] | 75 |
|
---|
[b0f225f] | 76 | [ void ] msg( * Parse_Failure ex );
|
---|
[6d8e1ab] | 77 |
|
---|
[b5e725a] | 78 | ExceptionDecl( Validation_Failure,
|
---|
[dcf792a] | 79 | * char failed_key;
|
---|
| 80 | * char failed_value;
|
---|
[b5e725a] | 81 | );
|
---|
[930609e2] | 82 |
|
---|
[b0f225f] | 83 | [ void ] msg( * Validation_Failure ex );
|
---|
[930609e2] | 84 |
|
---|
| 85 |
|
---|
| 86 | // *********************************** main code ***********************************
|
---|
| 87 |
|
---|
| 88 |
|
---|
[81e0c61] | 89 | [ * KVPairs ] parse_tabular_config_format( [] const char config_file, size_t num_entries );
|
---|
[ca83227] | 90 |
|
---|
| 91 | [ void ] parse_config(
|
---|
| 92 | [] const char config_file,
|
---|
| 93 | [] config_entry entries,
|
---|
| 94 | size_t num_entries,
|
---|
[a3cadfc] | 95 | KVPairs * (*parser)(const char [], size_t) // TODO: add sensible default parser when resolver bug is fixed
|
---|
[ca83227] | 96 | );
|
---|
[16f9aca] | 97 |
|
---|
[b1eeb3aa] | 98 | bool parse( const char *, const char * & );
|
---|
| 99 | bool parse( const char *, int & );
|
---|
| 100 | bool parse( const char *, unsigned & );
|
---|
| 101 | bool parse( const char *, unsigned long & );
|
---|
| 102 | bool parse( const char *, unsigned long long & );
|
---|
| 103 | bool parse( const char *, float & );
|
---|
| 104 | bool parse( const char *, double & );
|
---|
| 105 |
|
---|
[930609e2] | 106 |
|
---|
| 107 | // *********************************** validation ***********************************
|
---|
| 108 |
|
---|
| 109 |
|
---|
[6a93e4d] | 110 | forall(T | relational( T ))
|
---|
[b0f225f] | 111 | [ bool ] is_nonnegative( & T );
|
---|
[930609e2] | 112 |
|
---|
[6a93e4d] | 113 | forall(T | relational( T ))
|
---|
[b0f225f] | 114 | [ bool ] is_positive( & T );
|
---|
[930609e2] | 115 |
|
---|
[6a93e4d] | 116 | forall(T | relational( T ))
|
---|
[b0f225f] | 117 | [ bool ] is_nonpositive( & T );
|
---|
[930609e2] | 118 |
|
---|
[6a93e4d] | 119 | forall(T | relational( T ))
|
---|
[b0f225f] | 120 | [ bool ] is_negative( & T );
|
---|
[930609e2] | 121 |
|
---|
| 122 |
|
---|
[16f9aca] | 123 | // Local Variables: //
|
---|
| 124 | // mode: c //
|
---|
| 125 | // tab-width: 4 //
|
---|
| 126 | // End: //
|
---|