| 1 | // ========================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : ConfigMapping.h // |
|---|
| 4 | // Purpose : config <-> string mapping // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in Mar 19 // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ========================================================= // |
|---|
| 10 | |
|---|
| 11 | #ifndef CONFIGMAPPING_H |
|---|
| 12 | #define CONFIGMAPPING_H |
|---|
| 13 | |
|---|
| 14 | #ifndef ARB_CORE_H |
|---|
| 15 | #include <arb_core.h> |
|---|
| 16 | #endif |
|---|
| 17 | #ifndef ARB_STRARRAY_H |
|---|
| 18 | #include <arb_strarray.h> |
|---|
| 19 | #endif |
|---|
| 20 | |
|---|
| 21 | #ifndef _GLIBCXX_MAP |
|---|
| 22 | #include <map> |
|---|
| 23 | #endif |
|---|
| 24 | #ifndef _GLIBCXX_STRING |
|---|
| 25 | #include <string> |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | typedef std::map<std::string, std::string> config_map; |
|---|
| 29 | |
|---|
| 30 | struct ConfigMapping : public config_map { |
|---|
| 31 | |
|---|
| 32 | GB_ERROR parseFrom(const std::string& configString); |
|---|
| 33 | |
|---|
| 34 | // props + modifiers |
|---|
| 35 | bool has_entry(const char *entry) const { |
|---|
| 36 | // returns true if mapping contains 'entry' |
|---|
| 37 | return find(entry) != end(); |
|---|
| 38 | } |
|---|
| 39 | const char *get_entry(const char *entry) const { |
|---|
| 40 | // returns value of 'entry' |
|---|
| 41 | config_map::const_iterator found = find(entry); |
|---|
| 42 | return (found == end()) ? NULp : found->second.c_str(); |
|---|
| 43 | } |
|---|
| 44 | void set_entry(const std::string& entry, const std::string& value) { |
|---|
| 45 | // sets a (new) entry to value. |
|---|
| 46 | // existing entries get overwritten. |
|---|
| 47 | (*this)[entry] = value; |
|---|
| 48 | } |
|---|
| 49 | void delete_entry(const char *entry) { |
|---|
| 50 | // deletes an existing 'entry' |
|---|
| 51 | erase(entry); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | // result |
|---|
| 55 | std::string config_string() const { |
|---|
| 56 | using std::string; |
|---|
| 57 | string result; |
|---|
| 58 | for (config_map::const_iterator e = begin(); e != end(); ++e) { |
|---|
| 59 | const string& config_name(e->first); |
|---|
| 60 | string value(e->second); |
|---|
| 61 | |
|---|
| 62 | encode_escapes(value, "\'"); |
|---|
| 63 | string entry = config_name+"='"+value+'\''; |
|---|
| 64 | if (result.empty()) { |
|---|
| 65 | result = entry; |
|---|
| 66 | } |
|---|
| 67 | else { |
|---|
| 68 | result = result+';'+entry; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | return result; |
|---|
| 72 | } |
|---|
| 73 | void get_entries(class ConstStrArray& to_array) { |
|---|
| 74 | for (config_map::const_iterator e = begin(); e != end(); ++e) { |
|---|
| 75 | const std::string& key(e->first); |
|---|
| 76 | to_array.put(key.c_str()); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | static GB_ERROR decode_escapes(std::string& s); |
|---|
| 81 | static void encode_escapes(std::string& s, const char *to_escape); |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | #else |
|---|
| 86 | #error ConfigMapping.h included twice |
|---|
| 87 | #endif // CONFIGMAPPING_H |
|---|