diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index 2d89b43..a77deb0 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -23,6 +23,7 @@ enum class IpcConfigKey { TEST_NUM = 0, TEST_SHORT, + TEST_LONG, TEST_STRING, END }; diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index 081fa11..fc9d4b9 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -30,6 +30,10 @@ IpcConfig::IpcConfig() innerMapShort.insert(std::make_pair("test_short", std::reference_wrapper(mAllData.testShort))); mCfgMapShort.insert(std::make_pair(IpcConfigKey::TEST_SHORT, innerMapShort)); + std::map> innerMapLong; + innerMapLong.insert(std::make_pair("test_long", std::reference_wrapper(mAllData.testLong))); + mCfgMapLong.insert(std::make_pair(IpcConfigKey::TEST_LONG, innerMapLong)); + std::map> innerMapString; innerMapString.insert(std::make_pair("test_string", std::reference_wrapper(mAllData.testString))); mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString)); @@ -108,10 +112,10 @@ void IpcConfig::SetShort(const IpcConfigKey &key, const short &value) } const long IpcConfig::GetLong(const IpcConfigKey &key) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapLong.find(key); - if (iter != mCfgMapLong.end()) { - return iter->second; + if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) { + return iter->second.begin()->second; } LogError("Can't find the key.\n"); constexpr long UNKNOWN_CONFIG = -1; @@ -119,10 +123,12 @@ const long IpcConfig::GetLong(const IpcConfigKey &key) } void IpcConfig::SetLong(const IpcConfigKey &key, const long &value) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapLong.find(key); - if (iter != mCfgMapLong.end()) { - iter->second.get() = value; + if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) { + iter->second.begin()->second.get() = value; + const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * + ConfigSetLong(mCfg, name, iter->second.begin()->second); mCfgChanged = CONFIG_HAS_CHANGED; } else { @@ -314,6 +320,15 @@ void IpcConfig::ReadAllConfigParameters(void) ConfigSetShort(mCfg, "test_short", mAllData.testShort); } + StatusCode longCode = ConfigGetLong(mCfg, "test_long", &(mAllData.testLong)); + if (StatusCodeEqual(longCode, "CONFIG_CODE_PARAM_NOT_EXIST")) { + LogWarning("test_long doesn't exist, will make it as default.\n"); + mCfgChanged = CONFIG_HAS_CHANGED; + constexpr short DEFAULT_TEST_LONG_NUM = 12; + mAllData.testLong = DEFAULT_TEST_LONG_NUM; + ConfigSetLong(mCfg, "test_long", mAllData.testLong); + } + const char *testString = NULL; StatusCode stringCode = ConfigGetString(mCfg, "test_string", &(testString)); if (StatusCodeEqual(stringCode, "CONFIG_CODE_PARAM_NOT_EXIST")) { diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 32afe2b..48d3a0c 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -27,6 +27,7 @@ typedef struct Config_s { int testNum; short testShort; + long testLong; CHAR_STRING testString; } Config_s; class MapInt @@ -73,7 +74,7 @@ private: Config_s mAllData; std::map>> mCfgMapInt; std::map>> mCfgMapShort; - std::map> mCfgMapLong; + std::map>> mCfgMapLong; std::map> mCfgMapLongLong; std::map> mCfgMapChar; std::map> mCfgMapFloat; diff --git a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp index 84e6e82..ae85b54 100644 --- a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp +++ b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp @@ -21,12 +21,17 @@ TEST(IpcConfigTest, Demo) const int numShort = 888; IIpcConfig::GetInstance()->SetShort(IpcConfigKey::TEST_SHORT, numShort); + long testLong = IIpcConfig::GetInstance()->GetLong(IpcConfigKey::TEST_LONG); + LogInfo("Get test_long = %d\n", testLong); + const int numLong = 777; + IIpcConfig::GetInstance()->SetLong(IpcConfigKey::TEST_LONG, numLong); + const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING); LogInfo("Get testString = %s\n", testString.c_str()); const std::string string = "define"; IIpcConfig::GetInstance()->SetString(IpcConfigKey::TEST_STRING, string); - IIpcConfig::GetInstance()->ConfigFileSave(); + IIpcConfig::GetInstance()->ConfigFileSave(); IIpcConfig::GetInstance()->UnInit(); ILogUnInit(); DestroyLogModule(); diff --git a/utils/Config/src/Config.c b/utils/Config/src/Config.c index 2e9c6be..dcc6c89 100644 --- a/utils/Config/src/Config.c +++ b/utils/Config/src/Config.c @@ -48,7 +48,6 @@ const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value) } return cfg->set_int(cfg, name, value); } - const StatusCode ConfigGetShort(VConfig *cfg, const char *name, short *value) { if (NULL == cfg) { @@ -63,6 +62,20 @@ const StatusCode ConfigSetShort(VConfig *cfg, const char *name, const short valu } return cfg->set_short(cfg, name, value); } +const StatusCode ConfigGetLong(VConfig *cfg, const char *name, long *value) +{ + if (NULL == cfg) { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->get_long(cfg, name, value); +} +const StatusCode ConfigSetLong(VConfig *cfg, const char *name, const long value) +{ + if (NULL == cfg) { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->set_long(cfg, name, value); +} const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value) { diff --git a/utils/Config/src/ConfigImpl.c b/utils/Config/src/ConfigImpl.c index f577f0f..169c53f 100644 --- a/utils/Config/src/ConfigImpl.c +++ b/utils/Config/src/ConfigImpl.c @@ -21,6 +21,7 @@ #include #define CHECK_SHORT_LIMIT(value) (value > SHRT_MAX ? false : (value < SHRT_MIN ? false : true)) +#define CHECK_LONG_LIMIT(value) (value > LONG_MAX ? false : (value < LONG_MIN ? false : true)) static const StatusCode ConfigSaveFileImpl(VConfig *cfg) { @@ -76,7 +77,6 @@ static const StatusCode ConfigGetShortImpl(VConfig *cfg, const char *name, short *value = (short)intValue; return CreateStatusCode(STATUS_CODE_OK); } - static const StatusCode ConfigSetShortImpl(VConfig *cfg, const char *name, const short value) { config_setting_t *root, *setting; @@ -89,6 +89,29 @@ static const StatusCode ConfigSetShortImpl(VConfig *cfg, const char *name, const config_setting_set_int(setting, intValue); return CreateStatusCode(STATUS_CODE_OK); } +static const StatusCode ConfigGetLongImpl(VConfig *cfg, const char *name, long *value) +{ + long long llongValue = 0; + int result = 0; + result = config_lookup_int64(&(((Config *)cfg)->cfg), name, &llongValue); + if (CONFIG_FALSE == result && CHECK_LONG_LIMIT(llongValue)) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + *value = (long)llongValue; + return CreateStatusCode(STATUS_CODE_OK); +} +static const StatusCode ConfigSetLongImpl(VConfig *cfg, const char *name, const long value) +{ + config_setting_t *root, *setting; + root = config_root_setting(&(((Config *)cfg)->cfg)); + setting = config_setting_get_member(root, name); + if (!setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_INT); + } + long long llongValue = value; + config_setting_set_int64(setting, llongValue); + return CreateStatusCode(STATUS_CODE_OK); +} static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char **value) { int result = 0; @@ -122,6 +145,8 @@ static void ConfigImplInit(Config *cfg) cfg->base.set_int = ConfigSetIntImpl; cfg->base.get_short = ConfigGetShortImpl; cfg->base.set_short = ConfigSetShortImpl; + cfg->base.get_long = ConfigGetLongImpl; + cfg->base.set_long = ConfigSetLongImpl; cfg->base.get_string = ConfigGetStringImpl; cfg->base.set_string = ConfigSetStringImpl; cfg->base.save = ConfigSaveFileImpl;