mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
Merge branch 'master' of https://gitee.com/chenhaijian1107/embedded-framework
This commit is contained in:
commit
c1f5931454
84
Environmental.md
Normal file
84
Environmental.md
Normal file
|
@ -0,0 +1,84 @@
|
|||
# 前期需要下载的软件
|
||||
* VMware Workstration
|
||||
* git bash
|
||||
* vscode
|
||||
* Beyond Compare
|
||||
* MoBaXterm
|
||||
|
||||
# 前期快速上手
|
||||
* git的基本操作(我们主要使用git进行代码管理)
|
||||
* Linux的基本操作(主要是命令行操作,了解常用的命令,了解怎么使用命令行管理文件)
|
||||
//csdn相关文章写得很详尽,搭配上ai工具边用边学可以很快地熟悉常用的指令
|
||||
|
||||
|
||||
# 前期需要准备的基本环境
|
||||
1. 在VMware Workstation中安装Ubuntu,安装好gcc和g++编译器,并配置好网络(能正常上网)
|
||||
2. 学会vscode用ssh远程连接Ubuntu,下载CMake、CMake Tools、Baidu Comate(ai工具,对后续学习有比较大的帮助)等基本插件
|
||||
3. 创建一个gitee账号
|
||||
|
||||
# 编译过程
|
||||
1. 将gitee上的源码文件保存到虚拟机中
|
||||
2. 依照顺序执行以下命令
|
||||
|
||||
|
||||
* 安装libssl-dev软件包
|
||||
|
||||
```
|
||||
sudo apt-get update
|
||||
sudo apt-get install openssl
|
||||
sudo apt-get install libssl-dev
|
||||
```
|
||||
|
||||
* 安装LDAP软件包
|
||||
```
|
||||
sudo apt-get update
|
||||
sudo apt-get install libldap2-dev
|
||||
```
|
||||
|
||||
|
||||
* 安装cmake(llvm使用cmake编译,cmake版本要求 3.20以上,此处安装cmake-3.27.4)
|
||||
cmake源码压缩包所在的路径:embedded-framework/tools/cmake/cmake-3.27.4.tar.gz
|
||||
在embedded-framework/tools/cmake目录下执行以下指令来安装cmake-3.27.4:
|
||||
```
|
||||
tar zxvf cmake-3.27.4.tar.gz
|
||||
cd cmake-3.27.4
|
||||
sudo apt-get install openssl // 如果执行./bootstrap提示缺少ssl相关资源,执行此安装命令
|
||||
./bootstrap
|
||||
make
|
||||
sudo make install
|
||||
cmake --version //查看版本,确认安装完成
|
||||
```
|
||||
|
||||
* 安装llvm
|
||||
```
|
||||
// 下载源码
|
||||
git clone https://github.com/llvm/llvm-project.git
|
||||
cd llvm-project
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" ../llvm
|
||||
make -j8 //这个过程会有点久
|
||||
find ./ -name clang-tidy //查看是否有clang-tidy的文件
|
||||
clang-tidy --version //查看版本,确认编译完成
|
||||
```
|
||||
|
||||
3. 进行编译,在embedded-framework目录下执行以下命令:
|
||||
```
|
||||
make clean //CmakeLists.txt是生成Makefile文件的构建脚本,如果CmakeLists.txt被修改需要重新执行make clean清除之前的构建文件
|
||||
make cmake //构建源码,生成Makefile文件
|
||||
cd cmake-shell //在中间文件目录进行编译,把所有中间文件创建在此
|
||||
make //编译全部输出构建文件
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 问题汇总
|
||||
1. gcc编译器版本过低后续编译会出bug,亲测gcc 7.5.0版本不能用,可以使用gcc --version命令查看自己gcc的版本,版本过低需要更新版本,可以自行搜索怎么更新也可以参考https://blog.csdn.net/weixin_43354152/article/details/129247408
|
||||
|
||||
2. 下载llvm源码时git clone https://github.com/llvm/llvm-project.git 这个过程可能会经常报网络错误,多试几次,可以用网易uu加速器的学术资源加速可能会好下一点
|
||||
|
||||
3. 编译llvm过程可能会多次卡顿退出,可能的原因有:磁盘空间不足、编译器版本过低、缺少依赖等。可以尝试多线程编译(make -j8)
|
3
bug.md
Normal file
3
bug.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# 仓库中隐藏的bug
|
||||
1. “小”指针强制转换成“大”指针,可能造成内存的非法操作
|
||||
* embedded.framework/utils/confaBase /src/ConfigBaselmpl.cpp
|
|
@ -1,52 +1,56 @@
|
|||
/*
|
||||
* 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_H
|
||||
#define CONFIG_BASE_H
|
||||
#include "StatusCode.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
enum CONFIG_CODE
|
||||
{
|
||||
CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END,
|
||||
CONFIG_CODE_END
|
||||
};
|
||||
// StatusCode ConfigInit(void);
|
||||
// StatusCode ConfigUnInit(void);
|
||||
void *OpenConfigFile(const char *fileName);
|
||||
StatusCode ConfigSaveFile(void *object);
|
||||
void CloseConfigFile(void *object);
|
||||
StatusCode ConfigGetInt(void *object, const char *name, int *value);
|
||||
StatusCode ConfigSetInt(void *object, const char *name, const int value);
|
||||
StatusCode ConfigGetShort(void *object, const char *name, short *value);
|
||||
StatusCode ConfigSetShort(void *object, const char *name, const short value);
|
||||
StatusCode ConfigGetLong(void *object, const char *name, long *value);
|
||||
StatusCode ConfigSetLong(void *object, const char *name, const long value);
|
||||
StatusCode ConfigGetLLong(void *object, const char *name, long long *value);
|
||||
StatusCode ConfigSetLLong(void *object, const char *name, const long long value);
|
||||
StatusCode ConfigGetChar(void *object, const char *name, char *value);
|
||||
StatusCode ConfigSetChar(void *object, const char *name, const char value);
|
||||
StatusCode ConfigGetBool(void *object, const char *name, bool *value);
|
||||
StatusCode ConfigSetBool(void *object, const char *name, const bool value);
|
||||
StatusCode ConfigGetFloat(void *object, const char *name, float *value);
|
||||
StatusCode ConfigSetFloat(void *object, const char *name, const float value);
|
||||
StatusCode ConfigGetDouble(void *object, const char *name, double *value);
|
||||
StatusCode ConfigSetDouble(void *object, const char *name, const double value);
|
||||
StatusCode ConfigGetString(void *object, const char *name, const char **value);
|
||||
StatusCode ConfigSetString(void *object, const char *name, const char *value);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
/*
|
||||
* 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_H
|
||||
#define CONFIG_BASE_H
|
||||
#include "StatusCode.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" //The declaration is to allow the C++compiler to process code surrounded by extern "C" in the same way as the C language. This is necessary
|
||||
{
|
||||
#endif
|
||||
//#The three preprocessing instructions ifndef VNet BASE.H, # define VNet BASE.H, and # endif work together to ensure that the header file VNet BASE.H is only included once in any compilation unit. This is achieved by defining a unique macro (VNet BASE_H), skipping the file content if the macro has already been defined.
|
||||
enum CONFIG_CODE//Defined an enumeration type VNet CODE for possible configuration error codes. It starts with VNet CODE-IND (assuming VNet CODE-IND is a status code defined in Status Code. h, indicating the end of a set of status codes), and defines a VNet CODE-IND as the end flag for enumeration.
|
||||
{
|
||||
CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END,
|
||||
CONFIG_CODE_END
|
||||
};
|
||||
// StatusCode ConfigInit(void);
|
||||
// StatusCode ConfigUnInit(void);
|
||||
void *OpenConfigFile(const char *fileName);
|
||||
StatusCode ConfigSaveFile(void *object);
|
||||
void CloseConfigFile(void *object);
|
||||
StatusCode ConfigGetInt(void *object, const char *name, int *value);
|
||||
StatusCode ConfigSetInt(void *object, const char *name, const int value);
|
||||
StatusCode ConfigGetShort(void *object, const char *name, short *value);
|
||||
StatusCode ConfigSetShort(void *object, const char *name, const short value);
|
||||
StatusCode ConfigGetLong(void *object, const char *name, long *value);
|
||||
StatusCode ConfigSetLong(void *object, const char *name, const long value);
|
||||
StatusCode ConfigGetLLong(void *object, const char *name, long long *value);
|
||||
StatusCode ConfigSetLLong(void *object, const char *name, const long long value);
|
||||
StatusCode ConfigGetChar(void *object, const char *name, char *value);
|
||||
StatusCode ConfigSetChar(void *object, const char *name, const char value);
|
||||
StatusCode ConfigGetBool(void *object, const char *name, bool *value);
|
||||
StatusCode ConfigSetBool(void *object, const char *name, const bool value);
|
||||
StatusCode ConfigGetFloat(void *object, const char *name, float *value);
|
||||
StatusCode ConfigSetFloat(void *object, const char *name, const float value);
|
||||
StatusCode ConfigGetDouble(void *object, const char *name, double *value);
|
||||
StatusCode ConfigSetDouble(void *object, const char *name, const double value);
|
||||
StatusCode ConfigGetString(void *object, const char *name, const char **value);
|
||||
StatusCode ConfigSetString(void *object, const char *name, const char *value);
|
||||
//Provides a series of functions for opening, saving, closing configuration files, as well as obtaining and setting values for different data types (such as integer, short integer, long integer, long integer, character, boolean, floating-point, double precision floating-point, and string) in the configuration file. Most of these functions accept a void * object as the first parameter, which is typically a pointer to the configuration file context or object used to access and manipulate the configuration file. Other parameters include the name of the configuration item (const char * name) and a pointer to the variable used to store the retrieved value (such as int * value).
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
//Overall, this header file defines a configuration management interface that allows developers to read and write configuration file values in a type safe manner, while considering interoperability between C and C++.
|
34
utils/ConfigBase/readme.md
Normal file
34
utils/ConfigBase/readme.md
Normal file
|
@ -0,0 +1,34 @@
|
|||
# CONFIGBASE
|
||||
|
||||
#涉及的知识
|
||||
----------------------------------------------------------------
|
||||
1.CMake
|
||||
|
||||
2.C/C++
|
||||
|
||||
3.配置文件
|
||||
|
||||
4.状态码
|
||||
|
||||
5.libconfig库
|
||||
|
||||
### 各文件的作用
|
||||
*1.conffig_base.cmake:定义了一个名为config_owner的函数,该函数旨在为一个特定的CMake目标(比如一个可执行文件或库)的所有源文件添加一个新的编译定义(COMPILE_DEFINITIONS)。在这个上下文中,编译定义通常用于在编译时向源代码中注入宏定义,这些宏定义可以在源代码中被用作条件编译的开关。*
|
||||
|
||||
*2.ConfigBase.h:是一个C/C++兼容的头文件(CONFIG_BASE_H),它定义了一个配置管理的基础接口。这个接口提供了打开、保存、关闭配置文件以及获取和设置配置文件中不同类型数据的功能。*
|
||||
|
||||
*3.ConfigBase.cpp:实现了一个配置文件管理器的接口封装,它提供了一系列用于打开、关闭、保存配置文件以及获取和设置配置文件中的不同类型配置项的功能。*
|
||||
|
||||
*4.ConfigBaseCode.c:涉及到一个用于处理配置代码(ConfigCode)的系统,这些配置代码似乎是从一个更大的状态码(StatusCode)系统中派生出来的。代码依赖于几个自定义的类型和函数,包括StatusCode结构体、ILog接口(通过LogInfo和LogError宏或函数实现)、以及ConfigBase和ConfigBaseCode。*
|
||||
|
||||
*5.ConfigBaseCode.h:用于定义与配置代码相关的接口。它采用了C和C++的混合编程方式,并通过预处理器指令来控制代码的包含和编译。*
|
||||
|
||||
*6.ConfigBaselmpl.cpp:定义了一个名为ConfigBaseImpl的类,它用于处理基于文本的配置文件(如INI或类似格式的文件),使用libconfig库来实现这一功能。libconfig是一个用于处理结构化配置文件的C库,它支持读取和写入配置文件。ConfigBaseImpl类封装了libconfig的基本操作,如打开、关闭配置文件,以及读取和写入配置项的值。*
|
||||
|
||||
*7.ConfigBaselmpl.h:定义了一个名为 ConfigBaseImpl 的类,它实现了 IConfigBase 接口,用于处理配置文件。它使用了 libconfig 库来解析和生成配置文件。*
|
||||
|
||||
*8.lConfigBase.cpp:定义了一个名为 IConfigBase 的接口类,用于配置文件的操作,包括打开、关闭、读取和设置不同类型的配置值(如整数、短整数、长整数、长长整数、字符、布尔值、浮点数、双精度浮点数和字符串)。然而,这些函数的具体实现在 IConfigBase 类中都被设置为返回特定的状态码 STATUS_CODE_VIRTUAL_FUNCTION,这通常表示该函数是虚函数,需要在派生类中具体实现。*
|
||||
|
||||
*9.lConfigBase.h:定义了一个C++接口(IConfigBase)和一些相关的类型及函数声明,主要用于配置文件的操作。*
|
||||
|
||||
*10.CMakeLists.txt:是一个CMake脚本,用于配置和编译一个静态库项目(名为ConfigBase),并包含了一些额外的自定义目标和依赖管理。*
|
|
@ -1,183 +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);
|
||||
/*
|
||||
* 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);
|
||||
}
|
|
@ -1,55 +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);
|
||||
/*
|
||||
* 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);
|
||||
}
|
|
@ -1,29 +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.
|
||||
*/
|
||||
#ifndef CONFIG_BASE_CODE_H
|
||||
#define CONFIG_BASE_CODE_H
|
||||
#include "ConfigBase.h"
|
||||
#include "StatusCode.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#ifndef CONFIG_OWNER
|
||||
#error This is internal file, never include it.
|
||||
#endif
|
||||
const StatusCode CreateConfigCode(const long int code);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
/*
|
||||
* 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.
|
|
@ -1,347 +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);
|
||||
/*
|
||||
* 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);
|
||||
}
|
|
@ -1,51 +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
|
||||
#include "IConfigBase.h"
|
||||
#include <libconfig.h>
|
||||
#include <string>
|
||||
class ConfigBaseImpl : public IConfigBase
|
||||
{
|
||||
public:
|
||||
ConfigBaseImpl(const std::string &fileName);
|
||||
virtual ~ConfigBaseImpl() = default;
|
||||
bool OpenConfigFile(void) override;
|
||||
void CloseConfigFile(void) override;
|
||||
StatusCode ConfigSaveFile(void) override;
|
||||
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;
|
||||
|
||||
private:
|
||||
const std::string mFileName;
|
||||
config_t mCfg;
|
||||
};
|
||||
#endif
|
||||
/*
|
||||
* 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.
|
|
@ -1,123 +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));
|
||||
/*
|
||||
* 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));
|
||||
}
|
|
@ -1,57 +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
|
||||
#include "StatusCode.h"
|
||||
#include <memory>
|
||||
class IConfigBase
|
||||
{
|
||||
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);
|
||||
};
|
||||
typedef struct i_config_base_header
|
||||
{
|
||||
const char *mCheckName;
|
||||
} IConfigBaseHeader;
|
||||
typedef struct config_base
|
||||
{
|
||||
IConfigBaseHeader mHeader;
|
||||
std::shared_ptr<IConfigBase> mIConfigBase;
|
||||
} ConfigBase;
|
||||
const char *GetConfigBaseModuleName(void);
|
||||
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName);
|
||||
#endif
|
||||
/*
|
||||
* 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.
|
|
@ -52,9 +52,9 @@ public:
|
|||
void Closed(void);
|
||||
|
||||
private:
|
||||
std::mutex mMutex;
|
||||
hloop_t *mLoop;
|
||||
hio_t *mIo;
|
||||
std::mutex mMutex; ///A mutex lock used to synchronize access to shared resources.
|
||||
hloop_t *mLoop; ///Event loop, listening for io objects
|
||||
hio_t *mIo; ///Socket handle
|
||||
const TcpServerParam mParam;
|
||||
std::thread mTcpServerThread;
|
||||
std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients;
|
||||
|
|
|
@ -4,13 +4,11 @@
|
|||
# 涉及的知识
|
||||
* uart通信协议
|
||||
* 多线程
|
||||
* 系统调用函数的使用
|
||||
* C++类和对象
|
||||
* 函数指针
|
||||
|
||||
|
||||
# 各文件的作用
|
||||
* **UartDevice.h**:定义了一个名为uart_info的结构体存储UART设备的配置信息,定义了用于UART通信的函数接口,这些函数通常与UART硬件的底层实现相对应。
|
||||
* **UartDevice.cpp**:提供了一个跨平台的UART设备接口模块,使得上层应用可以通过统一的接口来操作不同的UART硬件设备。
|
||||
* **UartDeviceImpl.cpp**:提供了UART 设备操作接口,允许用户打开、发送和接收数据到 UART 设备,实现了 UART 设备的配置、数据发送和接收等功能。
|
||||
* **UartDeviceImpl.h**:定义了一个用于UART设备操作的类 UartDeviceImpl 和相关的结构体和函数,提供了一种通过接口和智能指针封装具体实现的方式来管理UART设备。
|
||||
* **UartDevice.h**:为上层应用提供了封装好的函数接口,用于与uart硬件设备进行交互。
|
||||
* **UartDevice.cpp**:UartDevice.h中函数接口的具体定义和封装。
|
||||
* **UartDeviceImpl.cpp/UartDeviceImpl.h**:这里面定义和声明的函数是对uart硬件设备更底层更具体的一些操作,它们会被提供给UartDevice.cpp做进一步的封装。
|
||||
|
|
Loading…
Reference in New Issue
Block a user