| 1 | #pragma once | 
|---|
| 2 |  | 
|---|
| 3 | #include <math.trait.hfa> | 
|---|
| 4 |  | 
|---|
| 5 |  | 
|---|
| 6 | // *********************************** initial declarations *********************************** | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 | struct config_entry { | 
|---|
| 10 | const char * key; | 
|---|
| 11 | void * variable; | 
|---|
| 12 | bool (*parse)( const char *, void * ); | 
|---|
| 13 | bool (*validate)( void * ); | 
|---|
| 14 | }; | 
|---|
| 15 |  | 
|---|
| 16 | bool null_validator( void * ) { return true; } | 
|---|
| 17 |  | 
|---|
| 18 | static inline void ?{}( config_entry & this ) {} | 
|---|
| 19 |  | 
|---|
| 20 | forall(T & | { bool parse( const char *, T & ); }) | 
|---|
| 21 | static inline void ?{}( config_entry & this, const char * key, T & variable ) { | 
|---|
| 22 | this.key      = key; | 
|---|
| 23 | this.variable = (void *)&variable; | 
|---|
| 24 | this.parse    = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse; | 
|---|
| 25 | this.validate = null_validator; | 
|---|
| 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; | 
|---|
| 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; | 
|---|
| 40 | this.parse    = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse; | 
|---|
| 41 | this.validate = null_validator; | 
|---|
| 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; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 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 |  | 
|---|
| 60 |  | 
|---|
| 61 | // *********************************** exceptions *********************************** | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | exception Missing_Config_Entries { | 
|---|
| 65 | unsigned int num_missing; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | [ void ] msg( * Missing_Config_Entries ex ); | 
|---|
| 69 |  | 
|---|
| 70 | exception Parse_Failure { | 
|---|
| 71 | * char failed_key; | 
|---|
| 72 | * char failed_value; | 
|---|
| 73 | }; | 
|---|
| 74 |  | 
|---|
| 75 | [ void ] msg( * Parse_Failure ex ); | 
|---|
| 76 |  | 
|---|
| 77 | exception Validation_Failure { | 
|---|
| 78 | * char failed_key; | 
|---|
| 79 | * char failed_value; | 
|---|
| 80 | }; | 
|---|
| 81 |  | 
|---|
| 82 | [ void ] msg( * Validation_Failure ex ); | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 | // *********************************** main code *********************************** | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | [ * KVPairs ] parse_tabular_config_format( [] const char config_file, size_t num_entries ); | 
|---|
| 89 |  | 
|---|
| 90 | [ void ] parse_config( | 
|---|
| 91 | [] const char config_file, | 
|---|
| 92 | [] config_entry entries, | 
|---|
| 93 | size_t num_entries, | 
|---|
| 94 | KVPairs * (*parser)(const char [], size_t)  // TODO: add sensible default parser when resolver bug is fixed | 
|---|
| 95 | ); | 
|---|
| 96 |  | 
|---|
| 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 |  | 
|---|
| 105 |  | 
|---|
| 106 | // *********************************** validation *********************************** | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 | forall(T | Relational( T )) | 
|---|
| 110 | [ bool ] is_nonnegative( & T ); | 
|---|
| 111 |  | 
|---|
| 112 | forall(T | Relational( T )) | 
|---|
| 113 | [ bool ] is_positive( & T ); | 
|---|
| 114 |  | 
|---|
| 115 | forall(T | Relational( T )) | 
|---|
| 116 | [ bool ] is_nonpositive( & T ); | 
|---|
| 117 |  | 
|---|
| 118 | forall(T | Relational( T )) | 
|---|
| 119 | [ bool ] is_negative( & T ); | 
|---|
| 120 |  | 
|---|
| 121 |  | 
|---|
| 122 | // Local Variables: // | 
|---|
| 123 | // mode: c // | 
|---|
| 124 | // tab-width: 4 // | 
|---|
| 125 | // End: // | 
|---|