[zhoulongyu]: 1.完成配置库的short数据类型; 2.使用代码格式化工具编译规范代码
This commit is contained in:
parent
d06477d707
commit
9aebd2158b
|
@ -22,6 +22,7 @@
|
||||||
enum class IpcConfigKey
|
enum class IpcConfigKey
|
||||||
{
|
{
|
||||||
TEST_NUM = 0,
|
TEST_NUM = 0,
|
||||||
|
TEST_SHORT,
|
||||||
TEST_STRING,
|
TEST_STRING,
|
||||||
END
|
END
|
||||||
};
|
};
|
||||||
|
|
|
@ -26,6 +26,10 @@ IpcConfig::IpcConfig()
|
||||||
innerMapInt.insert(std::make_pair("test_num", std::reference_wrapper<int>(mAllData.testNum)));
|
innerMapInt.insert(std::make_pair("test_num", std::reference_wrapper<int>(mAllData.testNum)));
|
||||||
mCfgMapInt.insert(std::make_pair(IpcConfigKey::TEST_NUM, innerMapInt));
|
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<CHAR_STRING>> innerMapString;
|
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapString;
|
||||||
innerMapString.insert(std::make_pair("test_string", std::reference_wrapper<CHAR_STRING>(mAllData.testString)));
|
innerMapString.insert(std::make_pair("test_string", std::reference_wrapper<CHAR_STRING>(mAllData.testString)));
|
||||||
mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString));
|
mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString));
|
||||||
|
@ -79,10 +83,10 @@ void IpcConfig::SetInt(const IpcConfigKey &key, const int &value)
|
||||||
}
|
}
|
||||||
const short IpcConfig::GetShort(const IpcConfigKey &key)
|
const short IpcConfig::GetShort(const IpcConfigKey &key)
|
||||||
{
|
{
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<short>>::iterator iter;
|
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
|
||||||
iter = mCfgMapShort.find(key);
|
iter = mCfgMapShort.find(key);
|
||||||
if (iter != mCfgMapShort.end()) {
|
if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) {
|
||||||
return iter->second;
|
return iter->second.begin()->second;
|
||||||
}
|
}
|
||||||
LogError("Can't find the key.\n");
|
LogError("Can't find the key.\n");
|
||||||
constexpr short UNKNOWN_CONFIG = -1;
|
constexpr short UNKNOWN_CONFIG = -1;
|
||||||
|
@ -90,10 +94,12 @@ const short IpcConfig::GetShort(const IpcConfigKey &key)
|
||||||
}
|
}
|
||||||
void IpcConfig::SetShort(const IpcConfigKey &key, const short &value)
|
void IpcConfig::SetShort(const IpcConfigKey &key, const short &value)
|
||||||
{
|
{
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<short>>::iterator iter;
|
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
|
||||||
iter = mCfgMapShort.find(key);
|
iter = mCfgMapShort.find(key);
|
||||||
if (iter != mCfgMapShort.end()) {
|
if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) {
|
||||||
iter->second.get() = value;
|
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;
|
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -298,9 +304,19 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
const char *testString = NULL;
|
const char *testString = NULL;
|
||||||
StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString));
|
StatusCode stringCode = ConfigGetString(mCfg, "test_string", &(testString));
|
||||||
if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
if (StatusCodeEqual(stringCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
||||||
LogWarning("test_string doesn't exist, will make it as default.\n");
|
LogWarning("test_string doesn't exist, will make it as default.\n");
|
||||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||||
char DEFAULT_TEST_STRING[] = "undefine";
|
char DEFAULT_TEST_STRING[] = "undefine";
|
||||||
|
|
|
@ -26,6 +26,7 @@ typedef char CHAR_STRING[256];
|
||||||
typedef struct Config_s
|
typedef struct Config_s
|
||||||
{
|
{
|
||||||
int testNum;
|
int testNum;
|
||||||
|
short testShort;
|
||||||
CHAR_STRING testString;
|
CHAR_STRING testString;
|
||||||
} Config_s;
|
} Config_s;
|
||||||
class MapInt
|
class MapInt
|
||||||
|
@ -71,7 +72,7 @@ private:
|
||||||
VConfig *mCfg;
|
VConfig *mCfg;
|
||||||
Config_s mAllData;
|
Config_s mAllData;
|
||||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>> mCfgMapInt;
|
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>> mCfgMapInt;
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<short>> mCfgMapShort;
|
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>> mCfgMapShort;
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<long>> mCfgMapLong;
|
std::map<IpcConfigKey, std::reference_wrapper<long>> mCfgMapLong;
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<long long>> mCfgMapLongLong;
|
std::map<IpcConfigKey, std::reference_wrapper<long long>> mCfgMapLongLong;
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<char>> mCfgMapChar;
|
std::map<IpcConfigKey, std::reference_wrapper<char>> mCfgMapChar;
|
||||||
|
|
|
@ -13,8 +13,13 @@ TEST(IpcConfigTest, Demo)
|
||||||
IIpcConfig::GetInstance()->Init();
|
IIpcConfig::GetInstance()->Init();
|
||||||
int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM);
|
int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM);
|
||||||
LogInfo("Get testNum = %d\n", testNum);
|
LogInfo("Get testNum = %d\n", testNum);
|
||||||
const int num999 = 999;
|
const int numInt = 999;
|
||||||
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, num999);
|
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, numInt);
|
||||||
|
|
||||||
|
short testShort = IIpcConfig::GetInstance()->GetShort(IpcConfigKey::TEST_SHORT);
|
||||||
|
LogInfo("Get test_short = %d\n", testShort);
|
||||||
|
const int numShort = 888;
|
||||||
|
IIpcConfig::GetInstance()->SetShort(IpcConfigKey::TEST_SHORT, numShort);
|
||||||
|
|
||||||
const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING);
|
const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING);
|
||||||
LogInfo("Get testString = %s\n", testString.c_str());
|
LogInfo("Get testString = %s\n", testString.c_str());
|
||||||
|
|
|
@ -16,33 +16,35 @@
|
||||||
#define CONFIG_H
|
#define CONFIG_H
|
||||||
#include "StatusCode.h"
|
#include "StatusCode.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C" {
|
||||||
{
|
|
||||||
#endif
|
#endif
|
||||||
enum CONFIG_CODE
|
enum CONFIG_CODE
|
||||||
{
|
{
|
||||||
CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END,
|
CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END,
|
||||||
CONFIG_CODE_END
|
CONFIG_CODE_END
|
||||||
};
|
};
|
||||||
typedef struct v_config VConfig;
|
typedef struct v_config VConfig;
|
||||||
typedef struct v_config
|
typedef struct v_config
|
||||||
{
|
{
|
||||||
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 (*get_short)(VConfig *, const char *, short *);
|
||||||
const StatusCode (*set_string)(VConfig *, const char *, const char*);
|
const StatusCode (*set_short)(VConfig *, const char *, const short);
|
||||||
|
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);
|
||||||
const StatusCode ConfigUnInit(void);
|
const StatusCode ConfigUnInit(void);
|
||||||
VConfig *OpenConfigFile(const char *fileName);
|
VConfig *OpenConfigFile(const char *fileName);
|
||||||
const StatusCode ConfigSaveFile(VConfig *cfg);
|
const StatusCode ConfigSaveFile(VConfig *cfg);
|
||||||
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 ConfigGetShort(VConfig *cfg, const char *name, short *value);
|
||||||
const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char** value);
|
const StatusCode ConfigSetShort(VConfig *cfg, const char *name, const short value);
|
||||||
const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char* 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
|
||||||
|
|
|
@ -16,30 +16,19 @@
|
||||||
#include "ConfigImpl.h"
|
#include "ConfigImpl.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
const StatusCode ConfigInit(void)
|
const StatusCode ConfigInit(void) { return CreateStatusCode(STATUS_CODE_OK); }
|
||||||
{
|
const StatusCode ConfigUnInit(void) { return CreateStatusCode(STATUS_CODE_OK); }
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
VConfig *OpenConfigFile(const char *fileName) { return (VConfig *)NewConfig(fileName); }
|
||||||
}
|
|
||||||
const StatusCode ConfigUnInit(void)
|
|
||||||
{
|
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
|
||||||
}
|
|
||||||
VConfig *OpenConfigFile(const char *fileName)
|
|
||||||
{
|
|
||||||
return (VConfig *)NewConfig(fileName);
|
|
||||||
}
|
|
||||||
const StatusCode ConfigSaveFile(VConfig *cfg)
|
const StatusCode ConfigSaveFile(VConfig *cfg)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg) {
|
||||||
{
|
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
return cfg->save(cfg);
|
return cfg->save(cfg);
|
||||||
}
|
}
|
||||||
void CloseConfigFile(VConfig *cfg)
|
void CloseConfigFile(VConfig *cfg)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg) {
|
||||||
{
|
|
||||||
LogError("NULL config poniter.\n");
|
LogError("NULL config poniter.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -47,32 +36,44 @@ void CloseConfigFile(VConfig *cfg)
|
||||||
}
|
}
|
||||||
const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value)
|
const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg) {
|
||||||
{
|
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
return cfg->get_int(cfg, name, value);
|
return cfg->get_int(cfg, name, value);
|
||||||
}
|
}
|
||||||
const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value)
|
const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg) {
|
||||||
{
|
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
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)
|
|
||||||
|
const StatusCode ConfigGetShort(VConfig *cfg, const char *name, short *value)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg) {
|
||||||
{
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
|
}
|
||||||
|
return cfg->get_short(cfg, name, value);
|
||||||
|
}
|
||||||
|
const StatusCode ConfigSetShort(VConfig *cfg, const char *name, const short value)
|
||||||
|
{
|
||||||
|
if (NULL == cfg) {
|
||||||
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
|
}
|
||||||
|
return cfg->set_short(cfg, name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value)
|
||||||
|
{
|
||||||
|
if (NULL == cfg) {
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
return cfg->get_string(cfg, name, value);
|
return cfg->get_string(cfg, name, value);
|
||||||
}
|
}
|
||||||
const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char *value)
|
const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char *value)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg) {
|
||||||
{
|
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
return cfg->set_string(cfg, name, value);
|
return cfg->set_string(cfg, name, value);
|
||||||
|
|
|
@ -15,14 +15,12 @@
|
||||||
#include "ConfigCode.h"
|
#include "ConfigCode.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = {
|
static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = {"CONFIG_CODE_PARAM_NOT_EXIST",
|
||||||
"CONFIG_CODE_PARAM_NOT_EXIST",
|
|
||||||
"CONFIG_CODE_END"};
|
"CONFIG_CODE_END"};
|
||||||
static const char *PrintStringConfigCode(const StatusCode this)
|
static const char *PrintStringConfigCode(const StatusCode this)
|
||||||
{
|
{
|
||||||
const int CODE_INDEX = this.mStatusCode - STATUS_CODE_END;
|
const int CODE_INDEX = this.mStatusCode - STATUS_CODE_END;
|
||||||
if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= CONFIG_CODE_END)
|
if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= CONFIG_CODE_END) {
|
||||||
{
|
|
||||||
LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]);
|
LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]);
|
||||||
return ConfigCodeString[CODE_INDEX];
|
return ConfigCodeString[CODE_INDEX];
|
||||||
}
|
}
|
||||||
|
@ -31,18 +29,14 @@ static const char *PrintStringConfigCode(const StatusCode this)
|
||||||
}
|
}
|
||||||
static const bool CodeEqual(const StatusCode code, const char *value)
|
static const bool CodeEqual(const StatusCode code, const char *value)
|
||||||
{
|
{
|
||||||
if (memcmp(value, ConfigCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0)
|
if (memcmp(value, ConfigCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0) {
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
static StatusCode NewConfigCode(const long int code)
|
static StatusCode NewConfigCode(const long int code)
|
||||||
{
|
{
|
||||||
StatusCode result = {
|
StatusCode result = {PrintStringConfigCode, CodeEqual, code};
|
||||||
PrintStringConfigCode,
|
|
||||||
CodeEqual,
|
|
||||||
code};
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
const StatusCode CreateConfigCode(const long int code)
|
const StatusCode CreateConfigCode(const long int code)
|
||||||
|
@ -51,8 +45,7 @@ const StatusCode CreateConfigCode(const long int code)
|
||||||
// {
|
// {
|
||||||
// return CreateStatusCode(code);
|
// return CreateStatusCode(code);
|
||||||
// }
|
// }
|
||||||
if (STATUS_CODE_END <= code && code < CONFIG_CODE_END)
|
if (STATUS_CODE_END <= code && code < CONFIG_CODE_END) {
|
||||||
{
|
|
||||||
return NewConfigCode(code);
|
return NewConfigCode(code);
|
||||||
}
|
}
|
||||||
LogError("undefined code.\n");
|
LogError("undefined code.\n");
|
||||||
|
|
|
@ -16,13 +16,12 @@
|
||||||
#define CONFIGCODE_H
|
#define CONFIGCODE_H
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C" {
|
||||||
{
|
|
||||||
#endif
|
#endif
|
||||||
#ifndef CONFIG_OWNER
|
#ifndef CONFIG_OWNER
|
||||||
#error This is internal file, never include it.
|
#error This is internal file, never include it.
|
||||||
#endif
|
#endif
|
||||||
const StatusCode CreateConfigCode(const long int code);
|
const StatusCode CreateConfigCode(const long int code);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -13,17 +13,20 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
#include "ConfigImpl.h"
|
#include "ConfigImpl.h"
|
||||||
#include "ILog.h"
|
|
||||||
#include "ConfigCode.h"
|
#include "ConfigCode.h"
|
||||||
|
#include "ILog.h"
|
||||||
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define CHECK_SHORT_LIMIT(value) (value > SHRT_MAX ? false : (value < SHRT_MIN ? false : true))
|
||||||
|
|
||||||
static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
|
static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
|
||||||
{
|
{
|
||||||
/* Write out the new configuration. */
|
/* Write out the new configuration. */
|
||||||
LogInfo("Save file[%s].\n", ((Config *)cfg)->mFileName);
|
LogInfo("Save file[%s].\n", ((Config *)cfg)->mFileName);
|
||||||
if (!config_write_file(&(((Config *)cfg)->cfg), ((Config *)cfg)->mFileName))
|
if (!config_write_file(&(((Config *)cfg)->cfg), ((Config *)cfg)->mFileName)) {
|
||||||
{
|
|
||||||
LogError("Save config failed.\n");
|
LogError("Save config failed.\n");
|
||||||
fprintf(stderr, "Error while writing file.\n");
|
fprintf(stderr, "Error while writing file.\n");
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
|
@ -32,11 +35,9 @@ static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
|
||||||
}
|
}
|
||||||
static void ConfigClose(VConfig *cfg)
|
static void ConfigClose(VConfig *cfg)
|
||||||
{
|
{
|
||||||
if (NULL != cfg)
|
if (NULL != cfg) {
|
||||||
{
|
|
||||||
config_destroy(&(((Config *)cfg)->cfg));
|
config_destroy(&(((Config *)cfg)->cfg));
|
||||||
if (NULL != ((Config *)cfg)->mFileName)
|
if (NULL != ((Config *)cfg)->mFileName) {
|
||||||
{
|
|
||||||
free(((Config *)cfg)->mFileName);
|
free(((Config *)cfg)->mFileName);
|
||||||
((Config *)cfg)->mFileName = NULL;
|
((Config *)cfg)->mFileName = NULL;
|
||||||
}
|
}
|
||||||
|
@ -48,8 +49,7 @@ static const StatusCode ConfigGetIntImpl(VConfig *cfg, const char *name, int *va
|
||||||
int result = 0;
|
int result = 0;
|
||||||
// config_setting_t *root;
|
// config_setting_t *root;
|
||||||
result = config_lookup_int(&(((Config *)cfg)->cfg), name, value);
|
result = config_lookup_int(&(((Config *)cfg)->cfg), name, value);
|
||||||
if (CONFIG_FALSE == result)
|
if (CONFIG_FALSE == result) {
|
||||||
{
|
|
||||||
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
||||||
}
|
}
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
|
@ -59,31 +59,52 @@ static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const i
|
||||||
config_setting_t *root, *setting;
|
config_setting_t *root, *setting;
|
||||||
root = config_root_setting(&(((Config *)cfg)->cfg));
|
root = config_root_setting(&(((Config *)cfg)->cfg));
|
||||||
setting = config_setting_get_member(root, name);
|
setting = config_setting_get_member(root, name);
|
||||||
if(!setting)
|
if (!setting) {
|
||||||
{
|
|
||||||
setting = config_setting_add(root, name, CONFIG_TYPE_INT);
|
setting = config_setting_add(root, name, CONFIG_TYPE_INT);
|
||||||
}
|
}
|
||||||
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)
|
static const StatusCode ConfigGetShortImpl(VConfig *cfg, const char *name, short *value)
|
||||||
{
|
{
|
||||||
|
int intValue = 0;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
// config_setting_t *root;
|
result = config_lookup_int(&(((Config *)cfg)->cfg), name, &intValue);
|
||||||
result = config_lookup_string(&(((Config *)cfg)->cfg), name, value);
|
if (CONFIG_FALSE == result && CHECK_SHORT_LIMIT(intValue)) {
|
||||||
if (CONFIG_FALSE == result)
|
|
||||||
{
|
|
||||||
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
||||||
}
|
}
|
||||||
|
*value = (short)intValue;
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
static const StatusCode ConfigSetStringImpl(VConfig *cfg, const char *name, const char* value)
|
|
||||||
|
static const StatusCode ConfigSetShortImpl(VConfig *cfg, const char *name, const short value)
|
||||||
{
|
{
|
||||||
config_setting_t *root, *setting;
|
config_setting_t *root, *setting;
|
||||||
root = config_root_setting(&(((Config *)cfg)->cfg));
|
root = config_root_setting(&(((Config *)cfg)->cfg));
|
||||||
setting = config_setting_get_member(root, name);
|
setting = config_setting_get_member(root, name);
|
||||||
if(!setting)
|
if (!setting) {
|
||||||
{
|
setting = config_setting_add(root, name, CONFIG_TYPE_INT);
|
||||||
|
}
|
||||||
|
int intValue = value;
|
||||||
|
config_setting_set_int(setting, intValue);
|
||||||
|
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_get_member(root, name);
|
||||||
|
if (!setting) {
|
||||||
setting = config_setting_add(root, name, CONFIG_TYPE_STRING);
|
setting = config_setting_add(root, name, CONFIG_TYPE_STRING);
|
||||||
}
|
}
|
||||||
config_setting_set_string(setting, value);
|
config_setting_set_string(setting, value);
|
||||||
|
@ -91,8 +112,7 @@ static const StatusCode ConfigSetStringImpl(VConfig *cfg, const char *name, cons
|
||||||
}
|
}
|
||||||
static void ConfigImplInit(Config *cfg)
|
static void ConfigImplInit(Config *cfg)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg) {
|
||||||
{
|
|
||||||
LogError("NULL pointer.\n");
|
LogError("NULL pointer.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -100,6 +120,8 @@ 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_short = ConfigGetShortImpl;
|
||||||
|
cfg->base.set_short = ConfigSetShortImpl;
|
||||||
cfg->base.get_string = ConfigGetStringImpl;
|
cfg->base.get_string = ConfigGetStringImpl;
|
||||||
cfg->base.set_string = ConfigSetStringImpl;
|
cfg->base.set_string = ConfigSetStringImpl;
|
||||||
cfg->base.save = ConfigSaveFileImpl;
|
cfg->base.save = ConfigSaveFileImpl;
|
||||||
|
@ -110,27 +132,25 @@ Config *NewConfig(const char *fileName)
|
||||||
Config *cfg = (Config *)malloc(sizeof(Config));
|
Config *cfg = (Config *)malloc(sizeof(Config));
|
||||||
ConfigImplInit(cfg);
|
ConfigImplInit(cfg);
|
||||||
config_init(&(cfg->cfg));
|
config_init(&(cfg->cfg));
|
||||||
config_set_options(&(cfg->cfg), (CONFIG_OPTION_FSYNC |
|
config_set_options(&(cfg->cfg),
|
||||||
CONFIG_OPTION_SEMICOLON_SEPARATORS |
|
(CONFIG_OPTION_FSYNC | CONFIG_OPTION_SEMICOLON_SEPARATORS |
|
||||||
CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS |
|
CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE));
|
||||||
CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE));
|
|
||||||
#define FIEL_EXIST 0
|
#define FIEL_EXIST 0
|
||||||
if (FIEL_EXIST == access(fileName, F_OK))
|
if (FIEL_EXIST == access(fileName, F_OK)) {
|
||||||
{
|
if (!config_read_file(&(cfg->cfg), fileName)) {
|
||||||
if (!config_read_file(&(cfg->cfg), fileName))
|
|
||||||
{
|
|
||||||
LogError("Read file failed[%s].\n", fileName);
|
LogError("Read file failed[%s].\n", fileName);
|
||||||
fprintf(stderr, "%s:%d - %s\n", config_error_file(&(cfg->cfg)),
|
fprintf(stderr,
|
||||||
config_error_line(&(cfg->cfg)), config_error_text(&(cfg->cfg)));
|
"%s:%d - %s\n",
|
||||||
|
config_error_file(&(cfg->cfg)),
|
||||||
|
config_error_line(&(cfg->cfg)),
|
||||||
|
config_error_text(&(cfg->cfg)));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogInfo("Config file doesn't exist.\n");
|
LogInfo("Config file doesn't exist.\n");
|
||||||
/* Write out the new configuration. */
|
/* Write out the new configuration. */
|
||||||
if (!config_write_file(&(cfg->cfg), fileName))
|
if (!config_write_file(&(cfg->cfg), fileName)) {
|
||||||
{
|
|
||||||
fprintf(stderr, "Error while writing file.\n");
|
fprintf(stderr, "Error while writing file.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -138,8 +158,7 @@ Config *NewConfig(const char *fileName)
|
||||||
unsigned int fileLength = strlen(fileName) + 1;
|
unsigned int fileLength = strlen(fileName) + 1;
|
||||||
cfg->mFileName = (char *)malloc(fileLength);
|
cfg->mFileName = (char *)malloc(fileLength);
|
||||||
memset(cfg->mFileName, 0, fileLength);
|
memset(cfg->mFileName, 0, fileLength);
|
||||||
if (NULL != cfg->mFileName)
|
if (NULL != cfg->mFileName) {
|
||||||
{
|
|
||||||
memcpy(cfg->mFileName, fileName, fileLength - 1);
|
memcpy(cfg->mFileName, fileName, fileLength - 1);
|
||||||
}
|
}
|
||||||
return cfg;
|
return cfg;
|
||||||
|
|
|
@ -17,21 +17,20 @@
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include <libconfig.h>
|
#include <libconfig.h>
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C" {
|
||||||
{
|
|
||||||
#endif
|
#endif
|
||||||
#ifndef CONFIG_OWNER
|
#ifndef CONFIG_OWNER
|
||||||
#error This is internal file, never include it.
|
#error This is internal file, never include it.
|
||||||
#endif
|
#endif
|
||||||
typedef struct config Config;
|
typedef struct config Config;
|
||||||
typedef struct config
|
typedef struct config
|
||||||
{
|
{
|
||||||
VConfig base;
|
VConfig base;
|
||||||
void (*close)(VConfig *);
|
void (*close)(VConfig *);
|
||||||
config_t cfg;
|
config_t cfg;
|
||||||
char *mFileName;
|
char *mFileName;
|
||||||
} Config;
|
} Config;
|
||||||
Config *NewConfig(const char *fileName);
|
Config *NewConfig(const char *fileName);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user