embedded-framework/utils/Config/src/ConfigImpl.c
2023-09-19 07:54:26 -07:00

117 lines
4.0 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 "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);
}
}
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_add(root, name, CONFIG_TYPE_INT);
config_setting_set_int(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.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;
}