/* * Copyright (c) 2023 Fancy Code. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "IpcConfig.h" #include "ILog.h" #include #define CHECK_MAP(map) (map.size() == 1 ? true : false) IpcConfig::IpcConfig() { mCfgChanged = CONFIG_HAS_NOT_CHANGED; mCfgMapInt.insert(std::make_pair>( IpcConfigKey::TEST_NUM, std::reference_wrapper(mAllData.testNum))); std::map> innerMap; innerMap.insert(std::make_pair("test_string", std::reference_wrapper(mAllData.testString))); mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMap)); } const StatusCode IpcConfig::Init(void) { memset(&mAllData, 0, sizeof(Config_s)); mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH); if (nullptr == mCfg) { LogError("Open config file failed.\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); } ReadAllConfigParameters(); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode IpcConfig::UnInit(void) { if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save config files.\n"); ConfigSaveFile(mCfg); } 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; iter = mCfgMapInt.find(key); if (iter != mCfgMapInt.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr int UNKNOWN_CONFIG = -1; return UNKNOWN_CONFIG; } void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) { std::map>::iterator iter; iter = mCfgMapInt.find(key); if (iter != mCfgMapInt.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const short IpcConfig::GetShort(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapShort.find(key); if (iter != mCfgMapShort.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr short UNKNOWN_CONFIG = -1; return UNKNOWN_CONFIG; } void IpcConfig::SetShort(const IpcConfigKey &key, const short &value) { std::map>::iterator iter; iter = mCfgMapShort.find(key); if (iter != mCfgMapShort.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const long IpcConfig::GetLong(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLong.find(key); if (iter != mCfgMapLong.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr long UNKNOWN_CONFIG = -1; return UNKNOWN_CONFIG; } void IpcConfig::SetLong(const IpcConfigKey &key, const long &value) { std::map>::iterator iter; iter = mCfgMapLong.find(key); if (iter != mCfgMapLong.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const long long IpcConfig::GetLongLong(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLongLong.find(key); if (iter != mCfgMapLongLong.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr long long UNKNOWN_CONFIG = -1; return UNKNOWN_CONFIG; } void IpcConfig::SetLongLong(const IpcConfigKey &key, const long long &value) { std::map>::iterator iter; iter = mCfgMapLongLong.find(key); if (iter != mCfgMapLongLong.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const char IpcConfig::GetChar(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapChar.find(key); if (iter != mCfgMapChar.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr char UNKNOWN_CONFIG = '\0'; return UNKNOWN_CONFIG; } void IpcConfig::SetChar(const IpcConfigKey &key, const char &character) { std::map>::iterator iter; iter = mCfgMapChar.find(key); if (iter != mCfgMapChar.end()) { iter->second.get() = character; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const float IpcConfig::GetFloat(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapFloat.find(key); if (iter != mCfgMapFloat.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr float UNKNOWN_CONFIG = -1.0; return UNKNOWN_CONFIG; } void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value) { std::map>::iterator iter; iter = mCfgMapFloat.find(key); if (iter != mCfgMapFloat.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const double IpcConfig::GetDouble(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapDouble.find(key); if (iter != mCfgMapDouble.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr double UNKNOWN_CONFIG = -1.0; return UNKNOWN_CONFIG; } void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value) { std::map>::iterator iter; iter = mCfgMapDouble.find(key); if (iter != mCfgMapDouble.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const long double IpcConfig::GetLongDouble(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLongDouble.find(key); if (iter != mCfgMapLongDouble.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr long double UNKNOWN_CONFIG = -1.0; return UNKNOWN_CONFIG; } void IpcConfig::SetLongDouble(const IpcConfigKey &key, const long double &value) { std::map>::iterator iter; iter = mCfgMapLongDouble.find(key); if (iter != mCfgMapLongDouble.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const bool IpcConfig::GetBool(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapBool.find(key); if (iter != mCfgMapBool.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr bool UNKNOWN_CONFIG = false; return UNKNOWN_CONFIG; } void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value) { std::map>::iterator iter; iter = mCfgMapBool.find(key); if (iter != mCfgMapBool.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } const std::string IpcConfig::GetString(const IpcConfigKey &key) { std::map>>::iterator iter; iter = mCfgMapString.find(key); if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) { const std::string sv(iter->second.begin()->second); // char[] --> const std::strinbg return sv; } LogError("Can't find the key.\n"); const std::string UNKNOWN_CONFIG = "undefine"; return UNKNOWN_CONFIG; } void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) { std::map>>::iterator iter; iter = mCfgMapString.find(key); if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) { strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[] const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * ConfigSetString(mCfg, name, iter->second.begin()->second); mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } void IpcConfig::ReadAllConfigParameters(void) { StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum)); if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) { LogWarning("test_num doesn't exist, will make it as default.\n"); mCfgChanged = CONFIG_HAS_CHANGED; constexpr int DEFAULT_TEST_NUM = 10; 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); } }