Signed-off-by: 张耀 <3213487792@qq.com>
This commit is contained in:
张耀 2024-07-27 09:12:47 +00:00 committed by Gitee
parent e0e30696cb
commit 62ba1ec99d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 858 additions and 0 deletions

View File

@ -0,0 +1,183 @@
/*
* 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 "ConfigBase.h"
#include "IConfigBase.h"
#include "ILog.h"
#include "StatusCode.h"
#include <memory>
#include <stdlib.h>
static bool ObjectCheck(void *object)
{
if (nullptr == object) {
LogError("nullptr object!\n");
return false;
}
if (*((const char **)(((char *)object) - sizeof(IConfigBaseHeader))) != GetConfigBaseModuleName()) {
LogError("Illegal object!\n");
return false;
}
return true;
}
void *OpenConfigFile(const char *fileName)
{
std::shared_ptr<IConfigBase> *configObject = NewConfigBase(fileName);
if (nullptr != configObject) {
if ((*configObject)->OpenConfigFile() == false) {
return nullptr;
}
}
return configObject;
}
StatusCode ConfigSaveFile(void *object)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSaveFile();
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
void CloseConfigFile(void *object)
{
if (ObjectCheck(object) == true) {
(*(std::shared_ptr<IConfigBase> *)object)->CloseConfigFile();
(*(std::shared_ptr<IConfigBase> *)object).reset();
free(((char *)object) - sizeof(IConfigBaseHeader)); // TODO: bug?
}
}
StatusCode ConfigGetInt(void *object, const char *name, int *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetInt(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetInt(void *object, const char *name, const int value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetInt(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigGetShort(void *object, const char *name, short *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetShort(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetShort(void *object, const char *name, const short value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetShort(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigGetLong(void *object, const char *name, long *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetLong(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetLong(void *object, const char *name, const long value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetLong(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigGetLLong(void *object, const char *name, long long *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetLLong(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetLLong(void *object, const char *name, const long long value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetLLong(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigGetChar(void *object, const char *name, char *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetChar(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetChar(void *object, const char *name, const char value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetChar(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigGetBool(void *object, const char *name, bool *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetBool(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetBool(void *object, const char *name, const bool value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetBool(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigGetFloat(void *object, const char *name, float *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetFloat(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetFloat(void *object, const char *name, const float value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetFloat(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigGetDouble(void *object, const char *name, double *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetDouble(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetDouble(void *object, const char *name, const double value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetDouble(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigGetString(void *object, const char *name, const char **value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetString(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
StatusCode ConfigSetString(void *object, const char *name, const char *value)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetString(name, value);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}

View File

@ -0,0 +1,55 @@
/*
* 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 "ConfigBaseCode.h"
#include "ConfigBase.h"
#include "ILog.h"
#include "StatusCode.h"
#include <string.h>
static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = {"CONFIG_CODE_PARAM_NOT_EXIST",
"CONFIG_CODE_END"};
static const char *PrintStringConfigCode(const StatusCode this)
{
const int CODE_INDEX = this.mStatusCode - STATUS_CODE_END;
if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= CONFIG_CODE_END) {
LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]);
return ConfigCodeString[CODE_INDEX];
}
LogError("Config code undefine.\n");
return "Config code undefine.\n";
}
static const bool CodeEqual(const StatusCode code, const char *value)
{
if (memcmp(value, ConfigCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0) {
return true;
}
return false;
}
static StatusCode NewConfigCode(const long int code)
{
StatusCode result = {PrintStringConfigCode, CodeEqual, code};
return result;
}
const StatusCode CreateConfigCode(const long int code)
{
// if (STATUS_CODE_OK <= code && code < STATUS_CODE_END)
// {
// return CreateStatusCode(code);
// }
if (STATUS_CODE_END <= code && code < CONFIG_CODE_END) {
return NewConfigCode(code);
}
LogError("undefined code.\n");
return CreateStatusCode(STATUS_CODE_END);
}

View File

@ -0,0 +1,35 @@
/*
* 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.
*/
//Copyright Statement and License: The beginning of the file contains a copyright statement and license information, indicating that this code is copyrighted by Fancy Code and follows the terms of Apache License, Version 2.0. This means that using this code must comply with the Apache 2.0 license regulations
#ifndef CONFIG_BASE_CODE_H
#define CONFIG_BASE_CODE_H
//#The three pieces of code, ifndef CONFIG_SBASE-CEDE.H, # define CONFIG_SBASE-CEDE.H, and # endif, are standard header file protectors (also known as inclusion guards) used to prevent header files from being duplicated.
#include "ConfigBase.h"
#include "StatusCode.h"
//These two lines of code contain two other header files that may define the Config Base class and Status Code enumeration or class, which will be used in the current header file.
#ifdef __cplusplus
extern "C" {
#endif
//It is to ensure that the code written in C language can be linked and used correctly when used in the C++environment. Extern "C" tells the C++compiler that this part of the code should be compiled and linked according to the rules of the C language to avoid the problem of name mangling in C++.
#ifndef CONFIG_OWNER
#error This is internal file, never include it.
// These two lines of code are a compile time check. If the VNet OWNER macro is not defined, the compiler will report an error and display the message 'This is internal file, never include it.'. This is usually used to prevent this header file from being directly included externally, possibly because it contains some definitions or declarations that should only be used internally within the library.
#endif
const StatusCode CreateConfigCode(const long int code);// This line of code declares a function called CreateConfig Code, which takes a parameter code of type long int and returns a value of type Status Code. This function may be used to create a configuration object or perform a configuration operation based on a given code (possibly some configuration identifier), and return a status code to indicate the success or failure of the operation.
#ifdef __cplusplus
}
#endif
#endif
//In summary, this header file defines a function declaration related to the configuration code, and ensures the correctness and security of the code through a series of conditional compilations and header file protectors.

View File

@ -0,0 +1,347 @@
/*
* 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 "ConfigBase.h"
#include "ConfigBaseCode.h"
#include "ILog.h"
#include "StatusCode.h"
#include <libconfig.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#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);
}

View File

@ -0,0 +1,53 @@
/*
* 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.
*/
#ifndef CONFIG_BASE_IMPL_H
#define CONFIG_BASE_IMPL_H
//Standard header file protection is used to prevent duplicate header files from being included.
#include "IConfigBase.h"//This is an interface definition file, and the Config Base Impl class implements all the pure virtual functions declared in this interface.
#include <libconfig.h>//This is the header file of the libconfig library, which provides an API for handling configuration files.
#include <string>//String processing class in the C++standard library.
class ConfigBaseImpl : public IConfigBase//The Config Base Impl class inherits from the FHIR figBase interface, which means it needs to implement all the pure virtual functions declared in the interface.
{
public:
ConfigBaseImpl(const std::string &fileName);
virtual ~ConfigBaseImpl() = default;
bool OpenConfigFile(void) override;//Open configuration file
void CloseConfigFile(void) override;//Close configuration file
StatusCode ConfigSaveFile(void) override;//Used to save configuration changes back to a file, but the return type is Status Code
StatusCode ConfigGetInt(const char *name, int *value) override;
StatusCode ConfigSetInt(const char *name, const int value) override;
StatusCode ConfigGetShort(const char *name, short *value) override;
StatusCode ConfigSetShort(const char *name, const short value) override;
StatusCode ConfigGetLong(const char *name, long *value) override;
StatusCode ConfigSetLong(const char *name, const long value) override;
StatusCode ConfigGetLLong(const char *name, long long *value) override;
StatusCode ConfigSetLLong(const char *name, const long long value) override;
StatusCode ConfigGetChar(const char *name, char *value) override;
StatusCode ConfigSetChar(const char *name, const char value) override;
StatusCode ConfigGetBool(const char *name, bool *value) override;
StatusCode ConfigSetBool(const char *name, const bool value) override;
StatusCode ConfigGetFloat(const char *name, float *value) override;
StatusCode ConfigSetFloat(const char *name, const float value) override;
StatusCode ConfigGetDouble(const char *name, double *value) override;
StatusCode ConfigSetDouble(const char *name, const double value) override;
StatusCode ConfigGetString(const char *name, const char **value) override;
StatusCode ConfigSetString(const char *name, const char *value) override;
//These functions provide interfaces for obtaining and setting various data types (such as integers, floating-point numbers, strings, etc.) from configuration files. They accept the name of a configuration item and a pointer to the value (for Get functions) or the value itself (for Set functions), and return a Status Code representing the operation result.
private:
const std::string mFileName;// The path to store configuration files.
config_t mCfg;//The confic_t type in the libconfig library is used to represent the internal structure of configuration files.
};
#endif
//In summary, this code defines a class for handling configuration files that provides rich interfaces to read and modify configuration items in the configuration file, achieved through the use of the libconfig library.

View File

@ -0,0 +1,123 @@
/*
* 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 "IConfigBase.h"
#include "ConfigBaseImpl.h"
#include "ILog.h"
#include "StatusCode.h"
#include <cstring>
#include <memory>
#include <stdlib.h>
bool IConfigBase::OpenConfigFile(void)
{
return false;
}
void IConfigBase::CloseConfigFile(void)
{
}
StatusCode IConfigBase::ConfigSaveFile(void)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetInt(const char *name, int *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetInt(const char *name, const int value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetShort(const char *name, short *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetShort(const char *name, const short value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetLong(const char *name, long *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetLong(const char *name, const long value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetLLong(const char *name, long long *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetLLong(const char *name, const long long value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetChar(const char *name, char *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetChar(const char *name, const char value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetBool(const char *name, bool *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetBool(const char *name, const bool value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetFloat(const char *name, float *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetFloat(const char *name, const float value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetDouble(const char *name, double *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetDouble(const char *name, const double value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigGetString(const char *name, const char **value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IConfigBase::ConfigSetString(const char *name, const char *value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
static const char *CONFIG_BASE_NAME = "config_base";
const char *GetConfigBaseModuleName(void)
{
return CONFIG_BASE_NAME;
}
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName)
{
LogInfo("Create the config base object.\n");
ConfigBase *impl = (ConfigBase *)malloc(sizeof(ConfigBase));
if (nullptr == impl) {
LogError("NewConfigBase::malloc failed.\n");
return nullptr;
}
ConfigBase tmp;
memcpy((void *)impl, (void *)&tmp, sizeof(ConfigBase));
impl->mHeader.mCheckName = CONFIG_BASE_NAME;
impl->mIConfigBase = std::make_shared<ConfigBaseImpl>(fileName);
return (std::shared_ptr<IConfigBase> *)(((char *)impl) + sizeof(IConfigBaseHeader));
}

View File

@ -0,0 +1,62 @@
/*
* 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.
*/
#ifndef I_CONFIG_BASE_H
#define I_CONFIG_BASE_H
//Header file protection (preventing header files from being duplicated).
#include "StatusCode.h"
#include <memory>
//Status Code. h (which may be a file that defines a status code enumeration or class to indicate the status of successful or failed operations) and<memory>(a part of the C++standard library that provides memory management features such as smart pointers).
class IConfigBase// FHIR gBase interface
{
public:
IConfigBase() = default;
virtual ~IConfigBase() = default;
virtual bool OpenConfigFile(void);
virtual void CloseConfigFile(void);
virtual StatusCode ConfigSaveFile(void);
virtual StatusCode ConfigGetInt(const char *name, int *value);
virtual StatusCode ConfigSetInt(const char *name, const int value);
virtual StatusCode ConfigGetShort(const char *name, short *value);
virtual StatusCode ConfigSetShort(const char *name, const short value);
virtual StatusCode ConfigGetLong(const char *name, long *value);
virtual StatusCode ConfigSetLong(const char *name, const long value);
virtual StatusCode ConfigGetLLong(const char *name, long long *value);
virtual StatusCode ConfigSetLLong(const char *name, const long long value);
virtual StatusCode ConfigGetChar(const char *name, char *value);
virtual StatusCode ConfigSetChar(const char *name, const char value);
virtual StatusCode ConfigGetBool(const char *name, bool *value);
virtual StatusCode ConfigSetBool(const char *name, const bool value);
virtual StatusCode ConfigGetFloat(const char *name, float *value);
virtual StatusCode ConfigSetFloat(const char *name, const float value);
virtual StatusCode ConfigGetDouble(const char *name, double *value);
virtual StatusCode ConfigSetDouble(const char *name, const double value);
virtual StatusCode ConfigGetString(const char *name, const char **value);
virtual StatusCode ConfigSetString(const char *name, const char *value);
};
//IKON Base is an abstract base class that defines interfaces related to configuration management. It includes default implementations of constructors and destructors (i.e. do not perform any operations), as well as a series of virtual functions for opening and closing configuration files, saving configurations, and obtaining and setting configuration values for various data types. The specific implementation of these virtual functions will be provided by derived classes.
typedef struct i_config_base_header//Defined a struct i_config.base_header containing a pointer mCheckName to a character, which may be used for some form of validation or recognition.
{
const char *mCheckName;
} IConfigBaseHeader;
typedef struct config_base//Defined a struct config.base that contains a member mHeader of type iKON figBaseHeader and a smart pointer mIConfig Base pointing to iKON figBase. This structure may be used to store and manage configuration instances in applications.
{
IConfigBaseHeader mHeader;
std::shared_ptr<IConfigBase> mIConfigBase;
} ConfigBase;
const char *GetConfigBaseModuleName(void);
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName);
//Declared two functions: FHIR nfigBaseModulus Name (which may be used to obtain the name of a configuration module) and NewConfig Base (which takes a file name as a parameter and returns a pointer to the FHIR figBase smart pointer for creating and returning a new configuration instance).
#endif
//This code defines a C++interface and related data structures for configuration management, as well as function declarations related to it. It utilizes the abstract base classes, virtual functions, smart pointers, and other features of C++to achieve flexible configuration management functionality.