diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index e7451274..b39e7363 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -22,13 +22,16 @@ enum class IpcConfigKey { TEST_NUM = 0, + TEST_STRING, END }; + class IIpcConfig { public: IIpcConfig() = default; virtual ~IIpcConfig() = default; + virtual const StatusCode ConfigFileSave(void) {return CreateStatusCode(STATUS_CODE_OK); } static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } @@ -50,11 +53,8 @@ public: virtual void SetLongDouble(const IpcConfigKey &key, const long double &value) {} virtual const bool GetBool(const IpcConfigKey &key) { return true; } virtual void SetBool(const IpcConfigKey &key, const bool &value) {} - virtual const std::string_view GetString(const IpcConfigKey &key) { return "undefine"; } + virtual const std::string GetString(const IpcConfigKey &key) { return "undefine"; } virtual void SetString(const IpcConfigKey &key, const std::string string) {} - - - }; bool CreateIpcConfig(void); #endif \ No newline at end of file diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index f97f3a37..d7451417 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -25,6 +25,13 @@ IpcConfig::IpcConfig() std::reference_wrapper>( IpcConfigKey::TEST_NUM, std::reference_wrapper(mAllData.testNum))); + + mCfgMapString.insert( + std::make_pair< + IpcConfigKey, + std::reference_wrapper>( + IpcConfigKey::TEST_STRING, + std::reference_wrapper(mAllData.testString))); } const StatusCode IpcConfig::Init(void) { @@ -48,6 +55,11 @@ const StatusCode IpcConfig::UnInit(void) CloseConfigFile(mCfg); return CreateStatusCode(STATUS_CODE_OK); } +const StatusCode IpcConfig::ConfigFileSave(void) +{ + return ConfigSaveFile(mCfg); +} + const int IpcConfig::GetInt(const IpcConfigKey &key) { std::map>::iterator iter; @@ -112,7 +124,6 @@ const long IpcConfig::GetLong(const IpcConfigKey &key) constexpr long UNKNOWN_CONFIG = -1; return UNKNOWN_CONFIG; } - void IpcConfig::SetLong(const IpcConfigKey &key, const long &value) { std::map>::iterator iter; @@ -217,7 +228,6 @@ const double IpcConfig::GetDouble(const IpcConfigKey &key) constexpr double UNKNOWN_CONFIG = -1.0; return UNKNOWN_CONFIG; } - void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value) { std::map>::iterator iter; @@ -284,28 +294,28 @@ void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value) LogError("Can't find the key.\n"); } } -const std::string_view IpcConfig::GetString(const IpcConfigKey &key) +const std::string IpcConfig::GetString(const IpcConfigKey &key) { - std::map>::iterator iter; + std::map>::iterator iter; iter = mCfgMapString.find(key); if (iter != mCfgMapString.end()) { - std::string s = iter->second; - std::string_view sv(s); + std::reference_wrapper s = iter->second; + const std::string sv(s); return sv; } LogError("Can't find the key.\n"); - constexpr std::string_view UNKNOWN_CONFIG = "undefine"; + const std::string UNKNOWN_CONFIG = "undefine"; return UNKNOWN_CONFIG; } void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) { - std::map>::iterator iter; + std::map>::iterator iter; iter = mCfgMapString.find(key); if (iter != mCfgMapString.end()) { - iter->second.get() = string; + strncpy(iter->second, string.c_str(), sizeof(CHAR_STRING)); mCfgChanged = CONFIG_HAS_CHANGED; } else @@ -325,10 +335,26 @@ void IpcConfig::ReadAllConfigParameters(void) mAllData.testNum = DEFAULT_TEST_NUM; ConfigSetInt(mCfg, "test_num", mAllData.testNum); } + + const char *testString = NULL; + StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString)); + if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) + { + LogWarning("test_string doesn't exist, will make it as default.\n"); + mCfgChanged = CONFIG_HAS_CHANGED; + char DEFAULT_TEST_STRING[] = "undefine"; + strncpy(mAllData.testString, DEFAULT_TEST_STRING, sizeof(mAllData.testString)); + ConfigSetString(mCfg, "test_string", mAllData.testString); + } + else + { + strncpy(mAllData.testString, testString, sizeof(mAllData.testString)); + } + if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save the config file.\n"); mCfgChanged = CONFIG_HAS_NOT_CHANGED; ConfigSaveFile(mCfg); } -} \ No newline at end of file +} diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 1a2ba777..d21517c5 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -21,9 +21,12 @@ #include constexpr bool CONFIG_HAS_CHANGED = true; constexpr bool CONFIG_HAS_NOT_CHANGED = false; + +typedef char CHAR_STRING[256]; typedef struct Config_s { int testNum; + CHAR_STRING testString; } Config_s; class MapInt { @@ -38,6 +41,7 @@ public: virtual ~IpcConfig() = default; const StatusCode Init(void) override; const StatusCode UnInit(void) override; + const StatusCode ConfigFileSave(void) override; const int GetInt(const IpcConfigKey &key) override; void SetInt(const IpcConfigKey &key, const int &value) override; const short GetShort(const IpcConfigKey &key) override; @@ -56,7 +60,7 @@ public: void SetLongDouble(const IpcConfigKey &key, const long double &value) override; const bool GetBool(const IpcConfigKey &key) override; void SetBool(const IpcConfigKey &key, const bool &value) override; - const std::string_view GetString(const IpcConfigKey &key) override; + const std::string GetString(const IpcConfigKey &key) override; void SetString(const IpcConfigKey &key, const std::string string) override; @@ -76,6 +80,6 @@ private: std::map> mCfgMapDouble; std::map> mCfgMapLongDouble; std::map> mCfgMapBool; - std::map> mCfgMapString; + std::map> mCfgMapString; }; #endif \ No newline at end of file diff --git a/utils/Config/include/Config.h b/utils/Config/include/Config.h index e67fe6ed..43a7e1c5 100644 --- a/utils/Config/include/Config.h +++ b/utils/Config/include/Config.h @@ -29,6 +29,8 @@ extern "C" { const StatusCode (*get_int)(VConfig *, const char *, int *); const StatusCode (*set_int)(VConfig *, const char *, const int); + const StatusCode (*get_string)(VConfig *, const char *, const char**); + const StatusCode (*set_string)(VConfig *, const char *, const char*); const StatusCode (*save)(VConfig *); } VConfig; const StatusCode ConfigInit(void); @@ -38,6 +40,9 @@ extern "C" void CloseConfigFile(VConfig *cfg); const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value); const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value); + + const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char** value); + const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char* value); #ifdef __cplusplus } #endif diff --git a/utils/Config/src/Config.c b/utils/Config/src/Config.c index a35660d0..2477d55c 100644 --- a/utils/Config/src/Config.c +++ b/utils/Config/src/Config.c @@ -60,4 +60,20 @@ const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value) return CreateStatusCode(STATUS_CODE_NOT_OK); } return cfg->set_int(cfg, name, value); +} +const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char** value) +{ + if (NULL == cfg) + { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->get_string(cfg, name, value); +} +const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char *value) +{ + if (NULL == cfg) + { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->set_string(cfg, name, value); } \ No newline at end of file diff --git a/utils/Config/src/ConfigImpl.c b/utils/Config/src/ConfigImpl.c index 28f1223c..6939205e 100644 --- a/utils/Config/src/ConfigImpl.c +++ b/utils/Config/src/ConfigImpl.c @@ -62,6 +62,25 @@ static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const i config_setting_set_int(setting, value); return CreateStatusCode(STATUS_CODE_OK); } +static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char** value) +{ + int result = 0; + // config_setting_t *root; + result = config_lookup_string(&(((Config *)cfg)->cfg), name, value); + if (CONFIG_FALSE == result) + { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +static const StatusCode ConfigSetStringImpl(VConfig *cfg, const char *name, const char* value) +{ + config_setting_t *root, *setting; + root = config_root_setting(&(((Config *)cfg)->cfg)); + setting = config_setting_add(root, name, CONFIG_TYPE_STRING); + config_setting_set_string(setting, value); + return CreateStatusCode(STATUS_CODE_OK); +} static void ConfigImplInit(Config *cfg) { if (NULL == cfg) @@ -73,6 +92,9 @@ static void ConfigImplInit(Config *cfg) cfg->close = ConfigClose; cfg->base.get_int = ConfigGetIntImpl; cfg->base.set_int = ConfigSetIntImpl; + + cfg->base.get_string = ConfigGetStringImpl; + cfg->base.set_string = ConfigSetStringImpl; cfg->base.save = ConfigSaveFileImpl; } Config *NewConfig(const char *fileName)