The key and value pairs config wrapper class for managing app setting.
Dependents: dconfig_demo mbed_controller_demo
dconfig.cpp@2:989178d925e9, 2015-03-25 (annotated)
- Committer:
- hillkim7
- Date:
- Wed Mar 25 21:04:58 2015 +0000
- Revision:
- 2:989178d925e9
- Parent:
- 1:4f7c80fbd4a1
Add print_all() method.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hillkim7 | 1:4f7c80fbd4a1 | 1 | /** |
hillkim7 | 1:4f7c80fbd4a1 | 2 | * @file dconfig.h |
hillkim7 | 1:4f7c80fbd4a1 | 3 | * |
hillkim7 | 1:4f7c80fbd4a1 | 4 | * @author hillkim7@gmail.com |
hillkim7 | 1:4f7c80fbd4a1 | 5 | * @brief The key and value pairs config wrapper class for managing app setting. |
hillkim7 | 1:4f7c80fbd4a1 | 6 | * |
hillkim7 | 1:4f7c80fbd4a1 | 7 | */ |
hillkim7 | 1:4f7c80fbd4a1 | 8 | |
hillkim7 | 1:4f7c80fbd4a1 | 9 | #include <string.h> |
hillkim7 | 1:4f7c80fbd4a1 | 10 | #include <stdio.h> |
hillkim7 | 1:4f7c80fbd4a1 | 11 | #include "dconfig.h" |
hillkim7 | 1:4f7c80fbd4a1 | 12 | |
hillkim7 | 1:4f7c80fbd4a1 | 13 | int DConfig::lookup_as_int( const std::string &key_name, int default_value ) const |
hillkim7 | 1:4f7c80fbd4a1 | 14 | { |
hillkim7 | 1:4f7c80fbd4a1 | 15 | std::string val; |
hillkim7 | 1:4f7c80fbd4a1 | 16 | |
hillkim7 | 1:4f7c80fbd4a1 | 17 | if (lookup(key_name, val)) |
hillkim7 | 1:4f7c80fbd4a1 | 18 | return atoi(val.c_str()); |
hillkim7 | 1:4f7c80fbd4a1 | 19 | else |
hillkim7 | 1:4f7c80fbd4a1 | 20 | return default_value; |
hillkim7 | 1:4f7c80fbd4a1 | 21 | } |
hillkim7 | 1:4f7c80fbd4a1 | 22 | |
hillkim7 | 1:4f7c80fbd4a1 | 23 | const char * DConfig::lookup_as_cstr( const std::string &key_name, const char *default_value ) const |
hillkim7 | 1:4f7c80fbd4a1 | 24 | { |
hillkim7 | 1:4f7c80fbd4a1 | 25 | std::map<std::string, std::string>::const_iterator it = this->find(key_name); |
hillkim7 | 1:4f7c80fbd4a1 | 26 | if (it != this->end()) |
hillkim7 | 1:4f7c80fbd4a1 | 27 | return it->second.c_str(); |
hillkim7 | 1:4f7c80fbd4a1 | 28 | |
hillkim7 | 1:4f7c80fbd4a1 | 29 | return default_value; |
hillkim7 | 1:4f7c80fbd4a1 | 30 | } |
hillkim7 | 1:4f7c80fbd4a1 | 31 | |
hillkim7 | 1:4f7c80fbd4a1 | 32 | bool DConfig::lookup( const std::string &key_name, std::string &value ) const |
hillkim7 | 1:4f7c80fbd4a1 | 33 | { |
hillkim7 | 1:4f7c80fbd4a1 | 34 | std::map<std::string, std::string>::const_iterator it = this->find(key_name); |
hillkim7 | 1:4f7c80fbd4a1 | 35 | if (it != this->end()) |
hillkim7 | 1:4f7c80fbd4a1 | 36 | { |
hillkim7 | 1:4f7c80fbd4a1 | 37 | value = it->second; |
hillkim7 | 1:4f7c80fbd4a1 | 38 | return true; |
hillkim7 | 1:4f7c80fbd4a1 | 39 | } |
hillkim7 | 1:4f7c80fbd4a1 | 40 | |
hillkim7 | 1:4f7c80fbd4a1 | 41 | return false; |
hillkim7 | 1:4f7c80fbd4a1 | 42 | } |
hillkim7 | 1:4f7c80fbd4a1 | 43 | |
hillkim7 | 1:4f7c80fbd4a1 | 44 | std::string DConfig::lookup( const std::string &key_name ) const |
hillkim7 | 1:4f7c80fbd4a1 | 45 | { |
hillkim7 | 1:4f7c80fbd4a1 | 46 | std::string s; |
hillkim7 | 1:4f7c80fbd4a1 | 47 | |
hillkim7 | 1:4f7c80fbd4a1 | 48 | lookup(key_name, s); |
hillkim7 | 1:4f7c80fbd4a1 | 49 | |
hillkim7 | 1:4f7c80fbd4a1 | 50 | return s; |
hillkim7 | 1:4f7c80fbd4a1 | 51 | } |
hillkim7 | 1:4f7c80fbd4a1 | 52 | |
hillkim7 | 2:989178d925e9 | 53 | bool DConfig::load_from( const char *ptr, size_t ptr_len ) |
hillkim7 | 1:4f7c80fbd4a1 | 54 | { |
hillkim7 | 1:4f7c80fbd4a1 | 55 | const char *end = ptr + ptr_len; |
hillkim7 | 1:4f7c80fbd4a1 | 56 | const char *begin; |
hillkim7 | 1:4f7c80fbd4a1 | 57 | std::string str_key, str_value; |
hillkim7 | 1:4f7c80fbd4a1 | 58 | |
hillkim7 | 1:4f7c80fbd4a1 | 59 | while (ptr < end) |
hillkim7 | 1:4f7c80fbd4a1 | 60 | { |
hillkim7 | 1:4f7c80fbd4a1 | 61 | // load key part |
hillkim7 | 1:4f7c80fbd4a1 | 62 | begin = ptr; |
hillkim7 | 2:989178d925e9 | 63 | while (*ptr != kv_delimiter_) // go until it reaches delimiter |
hillkim7 | 1:4f7c80fbd4a1 | 64 | { |
hillkim7 | 1:4f7c80fbd4a1 | 65 | ptr++; |
hillkim7 | 1:4f7c80fbd4a1 | 66 | if (ptr > end) |
hillkim7 | 1:4f7c80fbd4a1 | 67 | { |
hillkim7 | 1:4f7c80fbd4a1 | 68 | return false; |
hillkim7 | 1:4f7c80fbd4a1 | 69 | } |
hillkim7 | 1:4f7c80fbd4a1 | 70 | } |
hillkim7 | 1:4f7c80fbd4a1 | 71 | |
hillkim7 | 1:4f7c80fbd4a1 | 72 | std::string key(begin, ptr); |
hillkim7 | 1:4f7c80fbd4a1 | 73 | ++ptr; // skip delimiter |
hillkim7 | 1:4f7c80fbd4a1 | 74 | if (ptr >= end) |
hillkim7 | 1:4f7c80fbd4a1 | 75 | { |
hillkim7 | 1:4f7c80fbd4a1 | 76 | return false; |
hillkim7 | 1:4f7c80fbd4a1 | 77 | } |
hillkim7 | 0:7e982de4d3f5 | 78 | |
hillkim7 | 1:4f7c80fbd4a1 | 79 | // load value part |
hillkim7 | 1:4f7c80fbd4a1 | 80 | begin = ptr; |
hillkim7 | 2:989178d925e9 | 81 | while (*ptr != item_delimiter_) |
hillkim7 | 1:4f7c80fbd4a1 | 82 | { |
hillkim7 | 1:4f7c80fbd4a1 | 83 | ptr++; |
hillkim7 | 1:4f7c80fbd4a1 | 84 | if (ptr > end) |
hillkim7 | 1:4f7c80fbd4a1 | 85 | { |
hillkim7 | 1:4f7c80fbd4a1 | 86 | return false; |
hillkim7 | 1:4f7c80fbd4a1 | 87 | } |
hillkim7 | 1:4f7c80fbd4a1 | 88 | } |
hillkim7 | 1:4f7c80fbd4a1 | 89 | |
hillkim7 | 1:4f7c80fbd4a1 | 90 | std::string val(begin, ptr); |
hillkim7 | 1:4f7c80fbd4a1 | 91 | ++ptr; |
hillkim7 | 1:4f7c80fbd4a1 | 92 | if (ptr > end) |
hillkim7 | 1:4f7c80fbd4a1 | 93 | { |
hillkim7 | 1:4f7c80fbd4a1 | 94 | return false; |
hillkim7 | 1:4f7c80fbd4a1 | 95 | } |
hillkim7 | 1:4f7c80fbd4a1 | 96 | |
hillkim7 | 1:4f7c80fbd4a1 | 97 | (*this)[key] = val; |
hillkim7 | 1:4f7c80fbd4a1 | 98 | } |
hillkim7 | 1:4f7c80fbd4a1 | 99 | |
hillkim7 | 1:4f7c80fbd4a1 | 100 | return true; |
hillkim7 | 1:4f7c80fbd4a1 | 101 | } |
hillkim7 | 1:4f7c80fbd4a1 | 102 | |
hillkim7 | 1:4f7c80fbd4a1 | 103 | static bool put_cstring(DConfigOutput_t func, void *user_data, const char *s, char delimiter) |
hillkim7 | 1:4f7c80fbd4a1 | 104 | { |
hillkim7 | 1:4f7c80fbd4a1 | 105 | while (*s && func(user_data, *s)) |
hillkim7 | 1:4f7c80fbd4a1 | 106 | s++; |
hillkim7 | 1:4f7c80fbd4a1 | 107 | |
hillkim7 | 1:4f7c80fbd4a1 | 108 | return func(user_data, delimiter); |
hillkim7 | 1:4f7c80fbd4a1 | 109 | } |
hillkim7 | 1:4f7c80fbd4a1 | 110 | |
hillkim7 | 2:989178d925e9 | 111 | bool DConfig::save_to( DConfigOutput_t func, void *user_data ) const |
hillkim7 | 1:4f7c80fbd4a1 | 112 | { |
hillkim7 | 1:4f7c80fbd4a1 | 113 | for (std::map<std::string, std::string>::const_iterator it = this->begin(); |
hillkim7 | 1:4f7c80fbd4a1 | 114 | it != this->end(); ++it) |
hillkim7 | 1:4f7c80fbd4a1 | 115 | { |
hillkim7 | 2:989178d925e9 | 116 | if (!put_cstring(func, user_data, it->first.c_str(), kv_delimiter_)) |
hillkim7 | 1:4f7c80fbd4a1 | 117 | return false; |
hillkim7 | 1:4f7c80fbd4a1 | 118 | |
hillkim7 | 2:989178d925e9 | 119 | if (!put_cstring(func, user_data, it->second.c_str(), item_delimiter_)) |
hillkim7 | 1:4f7c80fbd4a1 | 120 | return false; |
hillkim7 | 1:4f7c80fbd4a1 | 121 | } |
hillkim7 | 1:4f7c80fbd4a1 | 122 | |
hillkim7 | 1:4f7c80fbd4a1 | 123 | return true; |
hillkim7 | 1:4f7c80fbd4a1 | 124 | } |
hillkim7 | 1:4f7c80fbd4a1 | 125 | |
hillkim7 | 2:989178d925e9 | 126 | |
hillkim7 | 2:989178d925e9 | 127 | static bool print_char(void *user_data, char c) |
hillkim7 | 2:989178d925e9 | 128 | { |
hillkim7 | 2:989178d925e9 | 129 | if (c == '\n') |
hillkim7 | 2:989178d925e9 | 130 | putchar('\r'); |
hillkim7 | 2:989178d925e9 | 131 | putchar(c); |
hillkim7 | 2:989178d925e9 | 132 | |
hillkim7 | 2:989178d925e9 | 133 | return true; |
hillkim7 | 2:989178d925e9 | 134 | } |
hillkim7 | 2:989178d925e9 | 135 | |
hillkim7 | 2:989178d925e9 | 136 | void DConfig::print_all( void ) const |
hillkim7 | 2:989178d925e9 | 137 | { |
hillkim7 | 2:989178d925e9 | 138 | save_to(print_char, NULL); |
hillkim7 | 2:989178d925e9 | 139 | } |
hillkim7 | 2:989178d925e9 | 140 | |
hillkim7 | 1:4f7c80fbd4a1 | 141 | static bool count_config(void *user_data, char c) |
hillkim7 | 1:4f7c80fbd4a1 | 142 | { |
hillkim7 | 1:4f7c80fbd4a1 | 143 | size_t *cnt = (size_t*)user_data; |
hillkim7 | 1:4f7c80fbd4a1 | 144 | |
hillkim7 | 1:4f7c80fbd4a1 | 145 | *cnt += 1; |
hillkim7 | 1:4f7c80fbd4a1 | 146 | |
hillkim7 | 1:4f7c80fbd4a1 | 147 | return true; |
hillkim7 | 1:4f7c80fbd4a1 | 148 | } |
hillkim7 | 1:4f7c80fbd4a1 | 149 | |
hillkim7 | 1:4f7c80fbd4a1 | 150 | size_t DConfig::estimate_save( void ) const |
hillkim7 | 1:4f7c80fbd4a1 | 151 | { |
hillkim7 | 1:4f7c80fbd4a1 | 152 | size_t cnt = 0; |
hillkim7 | 1:4f7c80fbd4a1 | 153 | |
hillkim7 | 1:4f7c80fbd4a1 | 154 | save_to(count_config, &cnt); |
hillkim7 | 1:4f7c80fbd4a1 | 155 | |
hillkim7 | 1:4f7c80fbd4a1 | 156 | return cnt; |
hillkim7 | 1:4f7c80fbd4a1 | 157 | } |
hillkim7 | 1:4f7c80fbd4a1 | 158 | |
hillkim7 | 1:4f7c80fbd4a1 | 159 | bool DConfig::value_replace( const std::string &key_name, std::string value ) |
hillkim7 | 1:4f7c80fbd4a1 | 160 | { |
hillkim7 | 1:4f7c80fbd4a1 | 161 | if (!has_key(key_name)) |
hillkim7 | 1:4f7c80fbd4a1 | 162 | return false; |
hillkim7 | 1:4f7c80fbd4a1 | 163 | |
hillkim7 | 1:4f7c80fbd4a1 | 164 | (*this)[key_name] = value; |
hillkim7 | 1:4f7c80fbd4a1 | 165 | |
hillkim7 | 1:4f7c80fbd4a1 | 166 | return true; |
hillkim7 | 1:4f7c80fbd4a1 | 167 | } |
hillkim7 | 1:4f7c80fbd4a1 | 168 | |
hillkim7 | 1:4f7c80fbd4a1 | 169 | bool DConfig::value_replace( const std::string &key_name, int value ) |
hillkim7 | 1:4f7c80fbd4a1 | 170 | { |
hillkim7 | 1:4f7c80fbd4a1 | 171 | char tmp[16]; |
hillkim7 | 1:4f7c80fbd4a1 | 172 | |
hillkim7 | 1:4f7c80fbd4a1 | 173 | sprintf(tmp, "%d", value); |
hillkim7 | 1:4f7c80fbd4a1 | 174 | |
hillkim7 | 1:4f7c80fbd4a1 | 175 | return value_replace(key_name, tmp); |
hillkim7 | 1:4f7c80fbd4a1 | 176 | } |
hillkim7 | 2:989178d925e9 | 177 |