embedded-framework/utils/ConfigBase/src/ConfigBaseImpl.cpp
2024-05-23 22:42:00 +08:00

340 lines
12 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 "ConfigBaseImpl.h"
#include "ConfigBaseCode.h"
#include "ILog.h"
#include <limits.h>
#include <math.h>
#include <unistd.h>
constexpr int INVALID_RESULT = -1;
#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))
#define CHECK_FLOAT_LIMIT(value) (fabs(value - ((float)value)) < 0.000001 ? false : true)
ConfigBaseImpl::ConfigBaseImpl(const std::string &fileName) : mFileName(fileName)
{
}
bool ConfigBaseImpl::OpenConfigFile(void)
{
config_init(&mCfg);
config_set_options(&mCfg,
(CONFIG_OPTION_FSYNC | CONFIG_OPTION_SEMICOLON_SEPARATORS |
CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE));
constexpr int FIEL_EXIST = 0;
if (FIEL_EXIST == access(mFileName.c_str(), F_OK)) {
if (!config_read_file(&mCfg, mFileName.c_str())) {
LogError("Read file failed[%s].\n", mFileName.c_str());
fprintf(stderr, "%s:%d - %s\n", config_error_file(&mCfg), config_error_line(&mCfg), config_error_text(&mCfg));
return false;
}
}
else {
LogInfo("Config file doesn't exist.mFileName = %s\n", mFileName.c_str());
/* Write out the new configuration. */
if (!config_write_file(&mCfg, mFileName.c_str())) {
fprintf(stderr, "Error while writing file.\n");
return false;
}
}
return true;
}
void ConfigBaseImpl::CloseConfigFile(void)
{
config_destroy(&mCfg);
}
StatusCode ConfigBaseImpl::ConfigSaveFile(void)
{
LogInfo("Save file[%s].\n", mFileName.c_str());
if (!config_write_file(&mCfg, mFileName.c_str())) {
LogError("Save config failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetInt(const char *name, int *value)
{
int result = INVALID_RESULT;
result = config_lookup_int(&mCfg, name, value);
if (CONFIG_FALSE == result) {
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
}
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigSetInt(const char *name, const int value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (nullptr == setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_INT);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
config_setting_set_int(setting, value);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetShort(const char *name, short *value)
{
int intValue = 0;
int result = 0;
result = config_lookup_int(&mCfg, 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);
}
StatusCode ConfigBaseImpl::ConfigSetShort(const char *name, const short value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (nullptr == setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_INT);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
int intValue = value;
config_setting_set_int(setting, intValue);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetLong(const char *name, long *value)
{
long long llongValue = 0;
int result = 0;
result = config_lookup_int64(&mCfg, 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);
}
StatusCode ConfigBaseImpl::ConfigSetLong(const char *name, const long value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (nullptr == setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_INT64);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
long long llongValue = value;
config_setting_set_int64(setting, llongValue);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetLLong(const char *name, long long *value)
{
int result = 0;
result = config_lookup_int64(&mCfg, name, value);
if (CONFIG_FALSE == result) {
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
}
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigSetLLong(const char *name, const long long value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (nullptr == setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_INT64);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
config_setting_set_int64(setting, value);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetChar(const char *name, char *value)
{
int charValue = 0;
int result = 0;
result = config_lookup_int(&mCfg, 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);
}
StatusCode ConfigBaseImpl::ConfigSetChar(const char *name, const char value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (nullptr == setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_INT);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
int charValue = (int)value;
config_setting_set_int(setting, charValue);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetBool(const char *name, bool *value)
{
int result = 0;
result = config_lookup_bool(&mCfg, name, (int *)value);
if (CONFIG_FALSE == result) {
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
}
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigSetBool(const char *name, const bool value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (nullptr == setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_BOOL);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
config_setting_set_bool(setting, (int)value);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetFloat(const char *name, float *value)
{
double dValue = 0;
int result = 0;
result = config_lookup_float(&mCfg, name, &dValue);
if (CONFIG_FALSE == result || CHECK_FLOAT_LIMIT(dValue)) {
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
}
*value = (float)dValue;
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigSetFloat(const char *name, const float value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (nullptr == setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_FLOAT);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
double dValue = value;
config_setting_set_float(setting, dValue);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetDouble(const char *name, double *value)
{
int result = 0;
result = config_lookup_float(&mCfg, name, value);
if (CONFIG_FALSE == result) {
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
}
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigSetDouble(const char *name, const double value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (nullptr == setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_FLOAT);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
config_setting_set_float(setting, value);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigGetString(const char *name, const char **value)
{
int result = 0;
result = config_lookup_string(&mCfg, name, value);
if (CONFIG_FALSE == result) {
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
}
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode ConfigBaseImpl::ConfigSetString(const char *name, const char *value)
{
config_setting_t *root = nullptr;
config_setting_t *setting = nullptr;
root = config_root_setting(&mCfg);
if (nullptr == root) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
setting = config_setting_get_member(root, name);
if (!setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_STRING);
}
if (nullptr == setting) {
LogError("Config function failed.\n");
return CreateConfigCode(STATUS_CODE_NOT_OK);
}
config_setting_set_string(setting, value);
return CreateStatusCode(STATUS_CODE_OK);
}