74 lines
2.4 KiB
C
74 lines
2.4 KiB
C
#include "ConfigImpl.h"
|
|
#include "ILog.h"
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
static void ConfigClose(VConfig *cfg)
|
|
{
|
|
if (NULL != cfg)
|
|
{
|
|
config_destroy(&(((Config *)cfg)->cfg));
|
|
free(cfg);
|
|
}
|
|
}
|
|
static const StatusCode ConfigGetIntImpl(VConfig *cfg, const char *name, int *value)
|
|
{
|
|
int result = 0;
|
|
config_setting_t *root, *setting, *movie;
|
|
root = config_root_setting(&(((Config *)cfg)->cfg));
|
|
setting = config_setting_get_member(root, name);
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const int value)
|
|
{
|
|
// int result = 0;
|
|
// config_setting_t *root, *setting, *movie;
|
|
// root = config_root_setting(&(((Config*)cfg)->cfg));
|
|
// setting = config_setting_get_member(root, name);
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static void ConfigImplInit(Config *cfg)
|
|
{
|
|
if (NULL == cfg)
|
|
{
|
|
LogError("NULL pointer.\n");
|
|
return;
|
|
}
|
|
cfg->close = ConfigClose;
|
|
cfg->base.get_int = ConfigGetIntImpl;
|
|
cfg->base.set_int = ConfigSetIntImpl;
|
|
}
|
|
Config *NewConfig(const char *fileName)
|
|
{
|
|
LogInfo("Config file name = %s\n", 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));
|
|
#define FIEL_EXIST 0
|
|
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)));
|
|
// config_destroy(&(cfg->cfg));
|
|
return NULL;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
LogInfo("Config file doesn't exist.\n");
|
|
/* Write out the new configuration. */
|
|
if (!config_write_file(&(cfg->cfg), fileName))
|
|
{
|
|
fprintf(stderr, "Error while writing file.\n");
|
|
// config_destroy(&cfg);
|
|
return NULL;
|
|
}
|
|
}
|
|
return cfg;
|
|
} |