284 lines
11 KiB
C
284 lines
11 KiB
C
/*
|
|
* Copyright (c) 2023 Fancy Code.
|
|
* 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.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
#include "ConfigImpl.h"
|
|
#include "ConfigCode.h"
|
|
#include "ILog.h"
|
|
#include <limits.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#define CHECK_SHORT_LIMIT(value) (value > SHRT_MAX ? false : (value < SHRT_MIN ? false : true))
|
|
#define CHECK_LONG_LIMIT(value) (value > LONG_MAX ? false : (value < LONG_MIN ? false : true))
|
|
#define CHECK_CHAR_LIMIT(value) (value > CHAR_MAX ? false : (value < CHAR_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)) {
|
|
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);
|
|
}
|
|
}
|
|
static const StatusCode ConfigGetIntImpl(VConfig *cfg, const char *name, int *value)
|
|
{
|
|
int result = 0;
|
|
// config_setting_t *root;
|
|
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)
|
|
{
|
|
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_INT);
|
|
}
|
|
config_setting_set_int(setting, value);
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigGetShortImpl(VConfig *cfg, const char *name, short *value)
|
|
{
|
|
int intValue = 0;
|
|
int result = 0;
|
|
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 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) {
|
|
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 ConfigGetLongImpl(VConfig *cfg, const char *name, long *value)
|
|
{
|
|
long long llongValue = 0;
|
|
int result = 0;
|
|
result = config_lookup_int64(&(((Config *)cfg)->cfg), name, &llongValue);
|
|
if (CONFIG_FALSE == result && CHECK_LONG_LIMIT(llongValue)) {
|
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
|
}
|
|
*value = (long)llongValue;
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigSetLongImpl(VConfig *cfg, const char *name, const long 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_INT64);
|
|
}
|
|
long long llongValue = value;
|
|
config_setting_set_int64(setting, llongValue);
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigGetLLongImpl(VConfig *cfg, const char *name, long long *value)
|
|
{
|
|
int result = 0;
|
|
result = config_lookup_int64(&(((Config *)cfg)->cfg), name, value);
|
|
if (CONFIG_FALSE == result) {
|
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
|
}
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigSetLLongImpl(VConfig *cfg, const char *name, const long long 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_INT64);
|
|
}
|
|
config_setting_set_int64(setting, value);
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigGetCharImpl(VConfig *cfg, const char *name, char *value)
|
|
{
|
|
int charValue = 0;
|
|
int result = 0;
|
|
result = config_lookup_int(&(((Config *)cfg)->cfg), name, &charValue);
|
|
if (CONFIG_FALSE == result && CHECK_CHAR_LIMIT(charValue)) {
|
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
|
}
|
|
*value = (char)charValue;
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigSetCharImpl(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_INT);
|
|
}
|
|
int charValue = (int)value;
|
|
config_setting_set_int(setting, charValue);
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigGetBoolImpl(VConfig *cfg, const char *name, bool *value)
|
|
{
|
|
int result = 0;
|
|
result = config_lookup_bool(&(((Config *)cfg)->cfg), name, (int *)value);
|
|
if (CONFIG_FALSE == result) {
|
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
|
}
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigSetBoolImpl(VConfig *cfg, const char *name, const bool 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_BOOL);
|
|
}
|
|
config_setting_set_bool(setting, (int)value);
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
|
|
static const StatusCode ConfigGetDoubleImpl(VConfig *cfg, const char *name, double *value)
|
|
{
|
|
int result = 0;
|
|
result = config_lookup_float(&(((Config *)cfg)->cfg), name, value);
|
|
if (CONFIG_FALSE == result) {
|
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
|
}
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static const StatusCode ConfigSetDoubleImpl(VConfig *cfg, const char *name, const double 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_FLOAT);
|
|
}
|
|
config_setting_set_float(setting, value);
|
|
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);
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
static void ConfigImplInit(Config *cfg)
|
|
{
|
|
if (NULL == cfg) {
|
|
LogError("NULL pointer.\n");
|
|
return;
|
|
}
|
|
cfg->mFileName = NULL;
|
|
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_long = ConfigGetLongImpl;
|
|
cfg->base.set_long = ConfigSetLongImpl;
|
|
cfg->base.get_llong = ConfigGetLLongImpl;
|
|
cfg->base.set_llong = ConfigSetLLongImpl;
|
|
cfg->base.get_char = ConfigGetCharImpl;
|
|
cfg->base.set_char = ConfigSetCharImpl;
|
|
cfg->base.get_bool = ConfigGetBoolImpl;
|
|
cfg->base.set_bool = ConfigSetBoolImpl;
|
|
cfg->base.get_double = ConfigGetDoubleImpl;
|
|
cfg->base.set_double = ConfigSetDoubleImpl;
|
|
cfg->base.get_string = ConfigGetStringImpl;
|
|
cfg->base.set_string = ConfigSetStringImpl;
|
|
cfg->base.save = ConfigSaveFileImpl;
|
|
}
|
|
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)));
|
|
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");
|
|
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;
|
|
} |