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