mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
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)
|
||||
{
|
||||
bool configChanging = false;
|
||||
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");
|
||||
configChanging = true;
|
||||
constexpr int DEFAULT_TEST_NUM = 0;
|
||||
mAllData.testNum = DEFAULT_TEST_NUM;
|
||||
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 (*set_int)(VConfig *, const char *, const int);
|
||||
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);
|
||||
|
|
|
@ -14,6 +14,14 @@ VConfig *OpenConfigFile(const char *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)
|
||||
{
|
||||
if (NULL == cfg)
|
||||
|
|
|
@ -1,12 +1,31 @@
|
|||
#include "ConfigImpl.h"
|
||||
#include "ILog.h"
|
||||
#include "ConfigCode.h"
|
||||
#include <stdlib.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)
|
||||
{
|
||||
if (NULL != cfg)
|
||||
{
|
||||
config_destroy(&(((Config *)cfg)->cfg));
|
||||
if (NULL != ((Config *)cfg)->mFileName)
|
||||
{
|
||||
free(((Config *)cfg)->mFileName);
|
||||
((Config *)cfg)->mFileName = NULL;
|
||||
}
|
||||
free(cfg);
|
||||
}
|
||||
}
|
||||
|
@ -14,8 +33,11 @@ static const StatusCode ConfigGetIntImpl(VConfig *cfg, const char *name, int *va
|
|||
{
|
||||
int result = 0;
|
||||
config_setting_t *root, *setting, *movie;
|
||||
root = config_root_setting(&(((Config *)cfg)->cfg));
|
||||
setting = config_setting_get_member(root, name);
|
||||
result = config_lookup_int(&(((Config *)cfg)->cfg), name, value);
|
||||
if (CONFIG_FALSE == result)
|
||||
{
|
||||
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
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");
|
||||
return;
|
||||
}
|
||||
cfg->mFileName = NULL;
|
||||
cfg->close = ConfigClose;
|
||||
cfg->base.get_int = ConfigGetIntImpl;
|
||||
cfg->base.set_int = ConfigSetIntImpl;
|
||||
cfg->base.save = ConfigSaveFileImpl;
|
||||
}
|
||||
Config *NewConfig(const char *fileName)
|
||||
{
|
||||
|
@ -70,5 +94,12 @@ Config *NewConfig(const char *fileName)
|
|||
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;
|
||||
}
|
|
@ -12,6 +12,7 @@ extern "C"
|
|||
VConfig base;
|
||||
void (*close)(VConfig *);
|
||||
config_t cfg;
|
||||
char *mFileName;
|
||||
} Config;
|
||||
Config *NewConfig(const char *fileName);
|
||||
#ifdef __cplusplus
|
||||
|
|
Loading…
Reference in New Issue
Block a user