Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: dconfig_demo mbed_controller_demo
dconfig.cpp
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 * 00007 */ 00008 00009 #include <string.h> 00010 #include <stdio.h> 00011 #include "dconfig.h" 00012 00013 int DConfig::lookup_as_int( const std::string &key_name, int default_value ) const 00014 { 00015 std::string val; 00016 00017 if (lookup(key_name, val)) 00018 return atoi(val.c_str()); 00019 else 00020 return default_value; 00021 } 00022 00023 const char * DConfig::lookup_as_cstr( const std::string &key_name, const char *default_value ) const 00024 { 00025 std::map<std::string, std::string>::const_iterator it = this->find(key_name); 00026 if (it != this->end()) 00027 return it->second.c_str(); 00028 00029 return default_value; 00030 } 00031 00032 bool DConfig::lookup( const std::string &key_name, std::string &value ) const 00033 { 00034 std::map<std::string, std::string>::const_iterator it = this->find(key_name); 00035 if (it != this->end()) 00036 { 00037 value = it->second; 00038 return true; 00039 } 00040 00041 return false; 00042 } 00043 00044 std::string DConfig::lookup( const std::string &key_name ) const 00045 { 00046 std::string s; 00047 00048 lookup(key_name, s); 00049 00050 return s; 00051 } 00052 00053 bool DConfig::load_from( const char *ptr, size_t ptr_len ) 00054 { 00055 const char *end = ptr + ptr_len; 00056 const char *begin; 00057 std::string str_key, str_value; 00058 00059 while (ptr < end) 00060 { 00061 // load key part 00062 begin = ptr; 00063 while (*ptr != kv_delimiter_) // go until it reaches delimiter 00064 { 00065 ptr++; 00066 if (ptr > end) 00067 { 00068 return false; 00069 } 00070 } 00071 00072 std::string key(begin, ptr); 00073 ++ptr; // skip delimiter 00074 if (ptr >= end) 00075 { 00076 return false; 00077 } 00078 00079 // load value part 00080 begin = ptr; 00081 while (*ptr != item_delimiter_) 00082 { 00083 ptr++; 00084 if (ptr > end) 00085 { 00086 return false; 00087 } 00088 } 00089 00090 std::string val(begin, ptr); 00091 ++ptr; 00092 if (ptr > end) 00093 { 00094 return false; 00095 } 00096 00097 (*this)[key] = val; 00098 } 00099 00100 return true; 00101 } 00102 00103 static bool put_cstring(DConfigOutput_t func, void *user_data, const char *s, char delimiter) 00104 { 00105 while (*s && func(user_data, *s)) 00106 s++; 00107 00108 return func(user_data, delimiter); 00109 } 00110 00111 bool DConfig::save_to( DConfigOutput_t func, void *user_data ) const 00112 { 00113 for (std::map<std::string, std::string>::const_iterator it = this->begin(); 00114 it != this->end(); ++it) 00115 { 00116 if (!put_cstring(func, user_data, it->first.c_str(), kv_delimiter_)) 00117 return false; 00118 00119 if (!put_cstring(func, user_data, it->second.c_str(), item_delimiter_)) 00120 return false; 00121 } 00122 00123 return true; 00124 } 00125 00126 00127 static bool print_char(void *user_data, char c) 00128 { 00129 if (c == '\n') 00130 putchar('\r'); 00131 putchar(c); 00132 00133 return true; 00134 } 00135 00136 void DConfig::print_all( void ) const 00137 { 00138 save_to(print_char, NULL); 00139 } 00140 00141 static bool count_config(void *user_data, char c) 00142 { 00143 size_t *cnt = (size_t*)user_data; 00144 00145 *cnt += 1; 00146 00147 return true; 00148 } 00149 00150 size_t DConfig::estimate_save( void ) const 00151 { 00152 size_t cnt = 0; 00153 00154 save_to(count_config, &cnt); 00155 00156 return cnt; 00157 } 00158 00159 bool DConfig::value_replace( const std::string &key_name, std::string value ) 00160 { 00161 if (!has_key(key_name)) 00162 return false; 00163 00164 (*this)[key_name] = value; 00165 00166 return true; 00167 } 00168 00169 bool DConfig::value_replace( const std::string &key_name, int value ) 00170 { 00171 char tmp[16]; 00172 00173 sprintf(tmp, "%d", value); 00174 00175 return value_replace(key_name, tmp); 00176 } 00177
Generated on Thu Jul 14 2022 03:03:21 by
1.7.2