The key and value pairs config wrapper class for managing app setting.
Dependents: dconfig_demo mbed_controller_demo
dconfig.h
00001 /** 00002 * @file dconfig.h 00003 * 00004 * @author hillkim7@gmail.com 00005 * @brief The key and value pairs config wrapper class for managing app setting. 00006 * Most of programs need to hold application specific setting. 00007 * The dconfig class can provide functionality for that. 00008 */ 00009 00010 #pragma once 00011 00012 #include <string> 00013 #include <map> 00014 #include <stdlib.h> 00015 00016 /** Example: 00017 * @code 00018 * 00019 * #include <stdio.h> 00020 * #include "dconfig.h" 00021 * 00022 * class MyConfig : public DConfig 00023 * { 00024 * public: 00025 * virtual void reset_default(void) 00026 * { 00027 * (*this)["tz"] = "9"; 00028 * (*this)["name"] = "my_app"; 00029 * } 00030 * }; 00031 * 00032 * int main(int argc, char* argv[]) 00033 * { 00034 * MyConfig mycfg; 00035 * std::string serialized_data; 00036 * 00037 * // setup initial key & value 00038 * mycfg.reset_default(); 00039 * 00040 * printf("name=%s\r\n", mycfg.lookup_as_cstr("name", "")); 00041 * printf("tz=%d\r\n", mycfg.lookup_as_int("tz", 0)); 00042 * return 0; 00043 * } 00044 00045 */ 00046 00047 typedef bool (*DConfigOutput_t) (void *user_data, char c); 00048 00049 class DConfig : public std::map<std::string, std::string> 00050 { 00051 public: 00052 DConfig(char kv_delimiter='=', char item_delimiter='\n') 00053 : kv_delimiter_(kv_delimiter) 00054 , item_delimiter_(item_delimiter) 00055 { 00056 } 00057 00058 /** lookup value by key and return value as int if a key exists. */ 00059 int lookup_as_int(const std::string &key_name, int default_value) const; 00060 00061 /** lookup value by key and return value as C string if a key exists. */ 00062 const char *lookup_as_cstr(const std::string &key_name, const char *default_value) const; 00063 00064 /** lookup value by key */ 00065 bool lookup(const std::string &key_name, std::string &value) const; 00066 00067 /** lookup value by key and return value as C string if a key exists. */ 00068 std::string lookup(const std::string &key_name) const; 00069 00070 /** replace value of existing value matching key. */ 00071 bool value_replace(const std::string &key_name, std::string value); 00072 00073 /** replace value of existing value matching key. */ 00074 bool value_replace(const std::string &key_name, int value); 00075 00076 /** load key & value pairs from memory. 00077 * The delimiters are same as them used in the load_from(). 00078 */ 00079 bool load_from(const char *ptr, size_t ptr_len); 00080 00081 /** save all key & value pairs by calling callback function. 00082 */ 00083 bool save_to(DConfigOutput_t func, void *user_data) const; 00084 00085 00086 /** print all item into the stdout */ 00087 void print_all(void) const; 00088 00089 /** estimate number of bytes for saving */ 00090 size_t estimate_save(void) const; 00091 00092 /** check if a key exists. */ 00093 bool has_key(const std::string &key_name) const 00094 { 00095 return this->find(key_name) != this->end(); 00096 } 00097 00098 /** virtual function for default setting. */ 00099 virtual void reset_default(void) 00100 { 00101 } 00102 00103 protected: 00104 /// kv_delimiter delimiter char between key and value. 00105 char kv_delimiter_; 00106 /// item_delimiter delimiter char between items. 00107 char item_delimiter_; 00108 }; 00109 00110
Generated on Thu Jul 14 2022 03:03:21 by
1.7.2