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