Ipc config backup.
This commit is contained in:
parent
395f30e076
commit
7a3d819229
|
@ -24,11 +24,19 @@ const int IpcConfig::GetInt(const IpcConfigKey &key)
|
||||||
}
|
}
|
||||||
void IpcConfig::ReadAllConfigParameters(void)
|
void IpcConfig::ReadAllConfigParameters(void)
|
||||||
{
|
{
|
||||||
|
bool configChanging = false;
|
||||||
StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum));
|
StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum));
|
||||||
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST"))
|
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST"))
|
||||||
{
|
{
|
||||||
|
LogWarning("test_num doesn't exist, will make it as default.\n");
|
||||||
|
configChanging = true;
|
||||||
constexpr int DEFAULT_TEST_NUM = 0;
|
constexpr int DEFAULT_TEST_NUM = 0;
|
||||||
mAllData.testNum = DEFAULT_TEST_NUM;
|
mAllData.testNum = DEFAULT_TEST_NUM;
|
||||||
ConfigSetInt(mCfg, "test_num", mAllData.testNum);
|
ConfigSetInt(mCfg, "test_num", mAllData.testNum);
|
||||||
}
|
}
|
||||||
|
if (true == configChanging)
|
||||||
|
{
|
||||||
|
LogInfo("Save the config file.\n");
|
||||||
|
ConfigSaveFile(mCfg);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -15,10 +15,12 @@ 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 (*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);
|
||||||
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);
|
||||||
|
|
|
@ -14,6 +14,14 @@ VConfig *OpenConfigFile(const char *fileName)
|
||||||
{
|
{
|
||||||
return (VConfig *)NewConfig(fileName);
|
return (VConfig *)NewConfig(fileName);
|
||||||
}
|
}
|
||||||
|
const StatusCode ConfigSaveFile(VConfig *cfg)
|
||||||
|
{
|
||||||
|
if (NULL == cfg)
|
||||||
|
{
|
||||||
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
|
}
|
||||||
|
return cfg->save(cfg);
|
||||||
|
}
|
||||||
void CloseConfigFile(VConfig *cfg)
|
void CloseConfigFile(VConfig *cfg)
|
||||||
{
|
{
|
||||||
if (NULL == cfg)
|
if (NULL == cfg)
|
||||||
|
|
|
@ -1,12 +1,31 @@
|
||||||
#include "ConfigImpl.h"
|
#include "ConfigImpl.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
|
#include "ConfigCode.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
LogError("Save config failed.\n");
|
||||||
|
fprintf(stderr, "Error while writing file.\n");
|
||||||
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
|
}
|
||||||
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
|
}
|
||||||
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)
|
||||||
|
{
|
||||||
|
free(((Config *)cfg)->mFileName);
|
||||||
|
((Config *)cfg)->mFileName = NULL;
|
||||||
|
}
|
||||||
free(cfg);
|
free(cfg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,8 +33,11 @@ static const StatusCode ConfigGetIntImpl(VConfig *cfg, const char *name, int *va
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
config_setting_t *root, *setting, *movie;
|
config_setting_t *root, *setting, *movie;
|
||||||
root = config_root_setting(&(((Config *)cfg)->cfg));
|
result = config_lookup_int(&(((Config *)cfg)->cfg), name, value);
|
||||||
setting = config_setting_get_member(root, name);
|
if (CONFIG_FALSE == result)
|
||||||
|
{
|
||||||
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
||||||
|
}
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const int value)
|
static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const int value)
|
||||||
|
@ -33,9 +55,11 @@ static void ConfigImplInit(Config *cfg)
|
||||||
LogError("NULL pointer.\n");
|
LogError("NULL pointer.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
cfg->mFileName = NULL;
|
||||||
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.save = ConfigSaveFileImpl;
|
||||||
}
|
}
|
||||||
Config *NewConfig(const char *fileName)
|
Config *NewConfig(const char *fileName)
|
||||||
{
|
{
|
||||||
|
@ -70,5 +94,12 @@ Config *NewConfig(const char *fileName)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
unsigned int fileLength = strlen(fileName) + 1;
|
||||||
|
cfg->mFileName = (char *)malloc(fileLength);
|
||||||
|
memset(cfg->mFileName, 0, fileLength);
|
||||||
|
if (NULL != cfg->mFileName)
|
||||||
|
{
|
||||||
|
memcpy(cfg->mFileName, fileName, fileLength - 1);
|
||||||
|
}
|
||||||
return cfg;
|
return cfg;
|
||||||
}
|
}
|
|
@ -12,6 +12,7 @@ extern "C"
|
||||||
VConfig base;
|
VConfig base;
|
||||||
void (*close)(VConfig *);
|
void (*close)(VConfig *);
|
||||||
config_t cfg;
|
config_t cfg;
|
||||||
|
char *mFileName;
|
||||||
} Config;
|
} Config;
|
||||||
Config *NewConfig(const char *fileName);
|
Config *NewConfig(const char *fileName);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
Loading…
Reference in New Issue
Block a user