[zhoulongyu]: 完善ConfigFile的char[]数据类型的读写
This commit is contained in:
parent
584e202b07
commit
c629d6b63f
|
@ -22,13 +22,16 @@
|
||||||
enum class IpcConfigKey
|
enum class IpcConfigKey
|
||||||
{
|
{
|
||||||
TEST_NUM = 0,
|
TEST_NUM = 0,
|
||||||
|
TEST_STRING,
|
||||||
END
|
END
|
||||||
};
|
};
|
||||||
|
|
||||||
class IIpcConfig
|
class IIpcConfig
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
IIpcConfig() = default;
|
IIpcConfig() = default;
|
||||||
virtual ~IIpcConfig() = default;
|
virtual ~IIpcConfig() = default;
|
||||||
|
virtual const StatusCode ConfigFileSave(void) {return CreateStatusCode(STATUS_CODE_OK); }
|
||||||
static std::shared_ptr<IIpcConfig> &GetInstance(std::shared_ptr<IIpcConfig> *impl = nullptr);
|
static std::shared_ptr<IIpcConfig> &GetInstance(std::shared_ptr<IIpcConfig> *impl = nullptr);
|
||||||
virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); }
|
virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); }
|
||||||
virtual const StatusCode UnInit(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 void SetLongDouble(const IpcConfigKey &key, const long double &value) {}
|
||||||
virtual const bool GetBool(const IpcConfigKey &key) { return true; }
|
virtual const bool GetBool(const IpcConfigKey &key) { return true; }
|
||||||
virtual void SetBool(const IpcConfigKey &key, const bool &value) {}
|
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) {}
|
virtual void SetString(const IpcConfigKey &key, const std::string string) {}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
bool CreateIpcConfig(void);
|
bool CreateIpcConfig(void);
|
||||||
#endif
|
#endif
|
|
@ -25,6 +25,13 @@ IpcConfig::IpcConfig()
|
||||||
std::reference_wrapper<int>>(
|
std::reference_wrapper<int>>(
|
||||||
IpcConfigKey::TEST_NUM,
|
IpcConfigKey::TEST_NUM,
|
||||||
std::reference_wrapper<int>(mAllData.testNum)));
|
std::reference_wrapper<int>(mAllData.testNum)));
|
||||||
|
|
||||||
|
mCfgMapString.insert(
|
||||||
|
std::make_pair<
|
||||||
|
IpcConfigKey,
|
||||||
|
std::reference_wrapper<CHAR_STRING>>(
|
||||||
|
IpcConfigKey::TEST_STRING,
|
||||||
|
std::reference_wrapper<CHAR_STRING>(mAllData.testString)));
|
||||||
}
|
}
|
||||||
const StatusCode IpcConfig::Init(void)
|
const StatusCode IpcConfig::Init(void)
|
||||||
{
|
{
|
||||||
|
@ -48,6 +55,11 @@ const StatusCode IpcConfig::UnInit(void)
|
||||||
CloseConfigFile(mCfg);
|
CloseConfigFile(mCfg);
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
|
const StatusCode IpcConfig::ConfigFileSave(void)
|
||||||
|
{
|
||||||
|
return ConfigSaveFile(mCfg);
|
||||||
|
}
|
||||||
|
|
||||||
const int IpcConfig::GetInt(const IpcConfigKey &key)
|
const int IpcConfig::GetInt(const IpcConfigKey &key)
|
||||||
{
|
{
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<int>>::iterator iter;
|
std::map<IpcConfigKey, std::reference_wrapper<int>>::iterator iter;
|
||||||
|
@ -112,7 +124,6 @@ const long IpcConfig::GetLong(const IpcConfigKey &key)
|
||||||
constexpr long UNKNOWN_CONFIG = -1;
|
constexpr long UNKNOWN_CONFIG = -1;
|
||||||
return UNKNOWN_CONFIG;
|
return UNKNOWN_CONFIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IpcConfig::SetLong(const IpcConfigKey &key, const long &value)
|
void IpcConfig::SetLong(const IpcConfigKey &key, const long &value)
|
||||||
{
|
{
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<long>>::iterator iter;
|
std::map<IpcConfigKey, std::reference_wrapper<long>>::iterator iter;
|
||||||
|
@ -217,7 +228,6 @@ const double IpcConfig::GetDouble(const IpcConfigKey &key)
|
||||||
constexpr double UNKNOWN_CONFIG = -1.0;
|
constexpr double UNKNOWN_CONFIG = -1.0;
|
||||||
return UNKNOWN_CONFIG;
|
return UNKNOWN_CONFIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value)
|
void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value)
|
||||||
{
|
{
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<double>>::iterator iter;
|
std::map<IpcConfigKey, std::reference_wrapper<double>>::iterator iter;
|
||||||
|
@ -284,28 +294,28 @@ void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value)
|
||||||
LogError("Can't find the key.\n");
|
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<IpcConfigKey, std::reference_wrapper<std::string>>::iterator iter;
|
std::map<IpcConfigKey, std::reference_wrapper<CHAR_STRING>>::iterator iter;
|
||||||
iter = mCfgMapString.find(key);
|
iter = mCfgMapString.find(key);
|
||||||
if (iter != mCfgMapString.end())
|
if (iter != mCfgMapString.end())
|
||||||
{
|
{
|
||||||
std::string s = iter->second;
|
std::reference_wrapper<CHAR_STRING> s = iter->second;
|
||||||
std::string_view sv(s);
|
const std::string sv(s);
|
||||||
return sv;
|
return sv;
|
||||||
}
|
}
|
||||||
LogError("Can't find the key.\n");
|
LogError("Can't find the key.\n");
|
||||||
constexpr std::string_view UNKNOWN_CONFIG = "undefine";
|
const std::string UNKNOWN_CONFIG = "undefine";
|
||||||
return UNKNOWN_CONFIG;
|
return UNKNOWN_CONFIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IpcConfig::SetString(const IpcConfigKey &key, const std::string string)
|
void IpcConfig::SetString(const IpcConfigKey &key, const std::string string)
|
||||||
{
|
{
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<std::string>>::iterator iter;
|
std::map<IpcConfigKey, std::reference_wrapper<CHAR_STRING>>::iterator iter;
|
||||||
iter = mCfgMapString.find(key);
|
iter = mCfgMapString.find(key);
|
||||||
if (iter != mCfgMapString.end())
|
if (iter != mCfgMapString.end())
|
||||||
{
|
{
|
||||||
iter->second.get() = string;
|
strncpy(iter->second, string.c_str(), sizeof(CHAR_STRING));
|
||||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -325,6 +335,22 @@ void IpcConfig::ReadAllConfigParameters(void)
|
||||||
mAllData.testNum = DEFAULT_TEST_NUM;
|
mAllData.testNum = DEFAULT_TEST_NUM;
|
||||||
ConfigSetInt(mCfg, "test_num", mAllData.testNum);
|
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)
|
if (CONFIG_HAS_CHANGED == mCfgChanged)
|
||||||
{
|
{
|
||||||
LogInfo("Save the config file.\n");
|
LogInfo("Save the config file.\n");
|
||||||
|
|
|
@ -21,9 +21,12 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
constexpr bool CONFIG_HAS_CHANGED = true;
|
constexpr bool CONFIG_HAS_CHANGED = true;
|
||||||
constexpr bool CONFIG_HAS_NOT_CHANGED = false;
|
constexpr bool CONFIG_HAS_NOT_CHANGED = false;
|
||||||
|
|
||||||
|
typedef char CHAR_STRING[256];
|
||||||
typedef struct Config_s
|
typedef struct Config_s
|
||||||
{
|
{
|
||||||
int testNum;
|
int testNum;
|
||||||
|
CHAR_STRING testString;
|
||||||
} Config_s;
|
} Config_s;
|
||||||
class MapInt
|
class MapInt
|
||||||
{
|
{
|
||||||
|
@ -38,6 +41,7 @@ public:
|
||||||
virtual ~IpcConfig() = default;
|
virtual ~IpcConfig() = default;
|
||||||
const StatusCode Init(void) override;
|
const StatusCode Init(void) override;
|
||||||
const StatusCode UnInit(void) override;
|
const StatusCode UnInit(void) override;
|
||||||
|
const StatusCode ConfigFileSave(void) override;
|
||||||
const int GetInt(const IpcConfigKey &key) override;
|
const int GetInt(const IpcConfigKey &key) override;
|
||||||
void SetInt(const IpcConfigKey &key, const int &value) override;
|
void SetInt(const IpcConfigKey &key, const int &value) override;
|
||||||
const short GetShort(const IpcConfigKey &key) override;
|
const short GetShort(const IpcConfigKey &key) override;
|
||||||
|
@ -56,7 +60,7 @@ public:
|
||||||
void SetLongDouble(const IpcConfigKey &key, const long double &value) override;
|
void SetLongDouble(const IpcConfigKey &key, const long double &value) override;
|
||||||
const bool GetBool(const IpcConfigKey &key) override;
|
const bool GetBool(const IpcConfigKey &key) override;
|
||||||
void SetBool(const IpcConfigKey &key, const bool &value) 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;
|
void SetString(const IpcConfigKey &key, const std::string string) override;
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,6 +80,6 @@ private:
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<double>> mCfgMapDouble;
|
std::map<IpcConfigKey, std::reference_wrapper<double>> mCfgMapDouble;
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble;
|
std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble;
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<bool>> mCfgMapBool;
|
std::map<IpcConfigKey, std::reference_wrapper<bool>> mCfgMapBool;
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<std::string>> mCfgMapString;
|
std::map<IpcConfigKey, std::reference_wrapper<CHAR_STRING>> mCfgMapString;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
|
@ -29,6 +29,8 @@ extern "C"
|
||||||
{
|
{
|
||||||
const StatusCode (*get_int)(VConfig *, const char *, int *);
|
const StatusCode (*get_int)(VConfig *, const char *, int *);
|
||||||
const StatusCode (*set_int)(VConfig *, const char *, const 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 *);
|
const StatusCode (*save)(VConfig *);
|
||||||
} VConfig;
|
} VConfig;
|
||||||
const StatusCode ConfigInit(void);
|
const StatusCode ConfigInit(void);
|
||||||
|
@ -38,6 +40,9 @@ extern "C"
|
||||||
void CloseConfigFile(VConfig *cfg);
|
void CloseConfigFile(VConfig *cfg);
|
||||||
const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value);
|
const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value);
|
||||||
const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const 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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -61,3 +61,19 @@ const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value)
|
||||||
}
|
}
|
||||||
return cfg->set_int(cfg, name, value);
|
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);
|
||||||
|
}
|
|
@ -62,6 +62,25 @@ static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const i
|
||||||
config_setting_set_int(setting, value);
|
config_setting_set_int(setting, value);
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
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)
|
static void ConfigImplInit(Config *cfg)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg)
|
||||||
|
@ -73,6 +92,9 @@ static void ConfigImplInit(Config *cfg)
|
||||||
cfg->close = ConfigClose;
|
cfg->close = ConfigClose;
|
||||||
cfg->base.get_int = ConfigGetIntImpl;
|
cfg->base.get_int = ConfigGetIntImpl;
|
||||||
cfg->base.set_int = ConfigSetIntImpl;
|
cfg->base.set_int = ConfigSetIntImpl;
|
||||||
|
|
||||||
|
cfg->base.get_string = ConfigGetStringImpl;
|
||||||
|
cfg->base.set_string = ConfigSetStringImpl;
|
||||||
cfg->base.save = ConfigSaveFileImpl;
|
cfg->base.save = ConfigSaveFileImpl;
|
||||||
}
|
}
|
||||||
Config *NewConfig(const char *fileName)
|
Config *NewConfig(const char *fileName)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user