hunting/middleware/IpcConfig/src/IpcConfig.cpp

381 lines
15 KiB
C++

/*
* 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 <string.h>
#define CHECK_MAP(map) (map.size() == 1 ? true : false)
IpcConfig::IpcConfig()
{
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
std::map<std::string, std::reference_wrapper<int>> innerMapInt;
innerMapInt.insert(std::make_pair("test_num", std::reference_wrapper<int>(mAllData.testNum)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::TEST_NUM, innerMapInt));
std::map<std::string, std::reference_wrapper<short>> innerMapShort;
innerMapShort.insert(std::make_pair("test_short", std::reference_wrapper<short>(mAllData.testShort)));
mCfgMapShort.insert(std::make_pair(IpcConfigKey::TEST_SHORT, innerMapShort));
std::map<std::string, std::reference_wrapper<long>> innerMapLong;
innerMapLong.insert(std::make_pair("test_long", std::reference_wrapper<long>(mAllData.testLong)));
mCfgMapLong.insert(std::make_pair(IpcConfigKey::TEST_LONG, innerMapLong));
std::map<std::string, std::reference_wrapper<long long>> innerMapLLong;
innerMapLLong.insert(std::make_pair("test_llong", std::reference_wrapper<long long>(mAllData.testLLong)));
mCfgMapLLong.insert(std::make_pair(IpcConfigKey::TEST_LLONG, innerMapLLong));
std::map<std::string, std::reference_wrapper<char>> innerMapChar;
innerMapChar.insert(std::make_pair("test_char", std::reference_wrapper<char>(mAllData.testChar)));
mCfgMapChar.insert(std::make_pair(IpcConfigKey::TEST_CHAR, innerMapChar));
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapString;
innerMapString.insert(std::make_pair("test_string", std::reference_wrapper<CHAR_STRING>(mAllData.testString)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString));
}
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<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>>::iterator iter;
iter = mCfgMapInt.find(key);
if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->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<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>>::iterator iter;
iter = mCfgMapInt.find(key);
if (iter != mCfgMapInt.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 *
ConfigSetInt(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const short IpcConfig::GetShort(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
iter = mCfgMapShort.find(key);
if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->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<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
iter = mCfgMapShort.find(key);
if (iter != mCfgMapShort.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 *
ConfigSetShort(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const long IpcConfig::GetLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
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;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetLong(const IpcConfigKey &key, const long &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
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 {
LogError("Can't find the key.\n");
}
}
const long long IpcConfig::GetLLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>>::iterator iter;
iter = mCfgMapLLong.find(key);
if (iter != mCfgMapLLong.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr long long UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetLLong(const IpcConfigKey &key, const long long &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>>::iterator iter;
iter = mCfgMapLLong.find(key);
if (iter != mCfgMapLLong.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 *
ConfigSetLLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const char IpcConfig::GetChar(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>>::iterator iter;
iter = mCfgMapChar.find(key);
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->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<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>>::iterator iter;
iter = mCfgMapChar.find(key);
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = character;
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
ConfigSetChar(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const float IpcConfig::GetFloat(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::reference_wrapper<float>>::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<IpcConfigKey, std::reference_wrapper<float>>::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<IpcConfigKey, std::reference_wrapper<double>>::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<IpcConfigKey, std::reference_wrapper<double>>::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<IpcConfigKey, std::reference_wrapper<long double>>::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<IpcConfigKey, std::reference_wrapper<long double>>::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<IpcConfigKey, std::reference_wrapper<bool>>::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<IpcConfigKey, std::reference_wrapper<bool>>::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<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>>::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<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>>::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);
}
StatusCode shortCode = ConfigGetShort(mCfg, "test_short", &(mAllData.testShort));
if (StatusCodeEqual(shortCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_short doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr short DEFAULT_TEST_SHORT_NUM = 11;
mAllData.testShort = DEFAULT_TEST_SHORT_NUM;
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 long DEFAULT_TEST_LONG_NUM = 12;
mAllData.testLong = DEFAULT_TEST_LONG_NUM;
ConfigSetLong(mCfg, "test_long", mAllData.testLong);
}
StatusCode llongCode = ConfigGetLLong(mCfg, "test_llong", &(mAllData.testLLong));
if (StatusCodeEqual(llongCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_llong doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr long long DEFAULT_TEST_LLONG_NUM = 13;
mAllData.testLLong = DEFAULT_TEST_LLONG_NUM;
ConfigSetLLong(mCfg, "test_llong", mAllData.testLLong);
}
StatusCode charCode = ConfigGetChar(mCfg, "test_char", &(mAllData.testChar));
if (StatusCodeEqual(charCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_char doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr char DEFAULT_TEST_CHAR_NUM = 'B';
mAllData.testChar = DEFAULT_TEST_CHAR_NUM;
ConfigSetChar(mCfg, "test_char", mAllData.testChar);
}
const char *testString = NULL;
StatusCode stringCode = ConfigGetString(mCfg, "test_string", &(testString));
if (StatusCodeEqual(stringCode, "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);
}
}