From d0b38294c61d6196a3ab300247a297de6ae73a94 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 08:41:50 +0800 Subject: [PATCH 01/19] remove environmental contruction --- Environmental.md | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Environmental.md diff --git a/Environmental.md b/Environmental.md new file mode 100644 index 00000000..a192e69f --- /dev/null +++ b/Environmental.md @@ -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) \ No newline at end of file From e5b8188fe5742ec8c2133aba4361265f25d9ba79 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 08:58:10 +0800 Subject: [PATCH 02/19] add bug.md --- bug.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 bug.md diff --git a/bug.md b/bug.md new file mode 100644 index 00000000..792a1bd2 --- /dev/null +++ b/bug.md @@ -0,0 +1,3 @@ +# 仓库中隐藏的bug +1. “小”指针强制转换成“大”指针,可能造成内存的非法操作 +* embedded.framework/utils/confaBase /src/ConfigBaselmpl.cpp \ No newline at end of file From c81e031a80d6d4e331b94651810d3460f2fc1fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 05:25:35 +0000 Subject: [PATCH 03/19] configbasee README.md. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- readme.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 00000000..e2e131f6 --- /dev/null +++ b/readme.md @@ -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),并包含了一些额外的自定义目标和依赖管理。* \ No newline at end of file From b343bb5ecf7d901c5ea923d6835b56bcb490e367 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 05:56:42 +0000 Subject: [PATCH 04/19] Revert "configbasee README.md." This reverts commit c81e031a80d6d4e331b94651810d3460f2fc1fe0. --- readme.md | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 readme.md diff --git a/readme.md b/readme.md deleted file mode 100644 index e2e131f6..00000000 --- a/readme.md +++ /dev/null @@ -1,34 +0,0 @@ -# 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),并包含了一些额外的自定义目标和依赖管理。* \ No newline at end of file From f6d84acc304363f86bbd9ad10745d39424a57429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 08:11:53 +0000 Subject: [PATCH 05/19] configbasee README.md. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/readme.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 utils/ConfigBase/readme.md diff --git a/utils/ConfigBase/readme.md b/utils/ConfigBase/readme.md new file mode 100644 index 00000000..e2e131f6 --- /dev/null +++ b/utils/ConfigBase/readme.md @@ -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),并包含了一些额外的自定义目标和依赖管理。* \ No newline at end of file From cacb6dcbf82c7c1847c54bb4cdd00a393ff9caad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 08:40:25 +0000 Subject: [PATCH 06/19] configbasee MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase.h | 56 ++++++++++++++++++++++++++++++++++++++ utils/ConfigBaseCode.h | 35 ++++++++++++++++++++++++ utils/ConfigBaseImpl.h | 52 +++++++++++++++++++++++++++++++++++ utils/IConfigBase.h | 62 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 utils/ConfigBase.h create mode 100644 utils/ConfigBaseCode.h create mode 100644 utils/ConfigBaseImpl.h create mode 100644 utils/IConfigBase.h diff --git a/utils/ConfigBase.h b/utils/ConfigBase.h new file mode 100644 index 00000000..576ffd9d --- /dev/null +++ b/utils/ConfigBase.h @@ -0,0 +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" //声明是为了让C++编译器按照C语言的方式来处理被extern "C"包围的代码。这是必要的 +{ +#endif +//#ifndef CONFIG_BASE_H、#define CONFIG_BASE_H 和 #endif 这三个预处理指令一起工作,确保头文件CONFIG_BASE_H在任何编译单元中只被包含(include)一次。这是通过定义一个唯一的宏(CONFIG_BASE_H)来实现的,如果宏已定义,则跳过文件内容。 +enum CONFIG_CODE//定义了一个枚举类型CONFIG_CODE,用于可能的配置错误代码。它开始于STATUS_CODE_END(这里假设STATUS_CODE_END是在StatusCode.h中定义的某个状态码,表示一组状态码的结束),并定义了一个CONFIG_CODE_END作为枚举的结束标志。 +{ + 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); +//提供了一系列函数,用于打开、保存、关闭配置文件,以及获取和设置配置文件中的不同数据类型(如整型、短整型、长整型、长长整型、字符、布尔值、浮点数、双精度浮点数和字符串)的值。这些函数大多数都接受一个void *object作为第一个参数,这通常是一个指向配置文件上下文或对象的指针,用于访问和操作配置文件。其他参数包括配置项的名称(const char *name)和用于存储获取值的变量的指针(如int *value)。 +#ifdef __cplusplus +} +#endif +#endif +//总的来说,这个头文件定义了一个配置管理的接口,允许开发者以类型安全的方式读取和写入配置文件的值,同时考虑到了C和C++之间的互操作性。 \ No newline at end of file diff --git a/utils/ConfigBaseCode.h b/utils/ConfigBaseCode.h new file mode 100644 index 00000000..45ff9217 --- /dev/null +++ b/utils/ConfigBaseCode.h @@ -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. + */ +//版权声明和许可:文件开头包含了一段版权声明和许可信息,指出这段代码是由Fancy Code版权所有,并遵循Apache License, Version 2.0的条款。这意味着使用这段代码必须遵守Apache 2.0许可证的规定 +#ifndef CONFIG_BASE_CODE_H +#define CONFIG_BASE_CODE_H +//#ifndef CONFIG_BASE_CODE_H、#define CONFIG_BASE_CODE_H、#endif 这三段代码是标准的头文件保护符(也称为包含卫士),用于防止头文件被重复包含。 +#include "ConfigBase.h" +#include "StatusCode.h" +//这两行代码包含了其他两个头文件,这些文件可能定义了ConfigBase类和StatusCode枚举或类,这些在当前的头文件中会被使用。 +#ifdef __cplusplus +extern "C" { +#endif +//是为了在C++环境中使用时,确保C语言编写的代码能够被正确地链接和使用。extern "C"告诉C++编译器,这部分代码应该按照C语言的规则来编译和链接,以避免C++的名称修饰(Name Mangling)问题。 +#ifndef CONFIG_OWNER + #error This is internal file, never include it. + // 这两行代码是一个编译时检查。如果CONFIG_OWNER宏未定义,编译器会报错,并显示“This is internal file, never include it.”这条信息。这通常用于防止这个头文件被外部直接包含,可能是因为它包含了一些只应该在库内部使用的定义或声明。 +#endif +const StatusCode CreateConfigCode(const long int code);// 这行代码声明了一个函数CreateConfigCode,它接受一个long int类型的参数code,并返回一个StatusCode类型的值。这个函数可能用于根据给定的代码(可能是某种配置标识符)来创建一个配置对象或执行某种配置操作,并返回一个状态码来表示操作的成功与否。 +#ifdef __cplusplus +} +#endif +#endif +//总之,这个头文件定义了一个与配置代码相关的函数声明,并通过一系列的条件编译和头文件保护符来确保代码的正确性和安全性。 \ No newline at end of file diff --git a/utils/ConfigBaseImpl.h b/utils/ConfigBaseImpl.h new file mode 100644 index 00000000..79b03c34 --- /dev/null +++ b/utils/ConfigBaseImpl.h @@ -0,0 +1,52 @@ +/* + * 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"//这是一个接口定义文件,ConfigBaseImpl类实现了这个接口中声明的所有纯虚函数。 +#include //这是libconfig库的头文件,提供了处理配置文件的API。 +#include //C++标准库中的字符串处理类。 +class ConfigBaseImpl : public IConfigBase//ConfigBaseImpl类继承自IConfigBase接口,这意味着它需要实现接口中声明的所有纯虚函数。 +{ +public: + ConfigBaseImpl(const std::string &fileName); + virtual ~ConfigBaseImpl() = default; + bool OpenConfigFile(void) override;//打开配置文件 + void CloseConfigFile(void) override;//关闭配置文件 + StatusCode ConfigSaveFile(void) override;//用于将配置更改保存回文件,但返回类型是StatusCode + 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; +//这些函数提供了从配置文件中获取和设置各种数据类型(如整数、浮点数、字符串等)的接口。它们接受一个配置项的名称和一个指向值的指针(对于Get函数)或值本身(对于Set函数),并返回一个StatusCode表示操作结果。 +private: + const std::string mFileName;// 存储配置文件的路径。 + config_t mCfg;//libconfig库中的config_t类型,用于表示配置文件的内部结构。 +}; +#endif \ No newline at end of file diff --git a/utils/IConfigBase.h b/utils/IConfigBase.h new file mode 100644 index 00000000..ab2caeb0 --- /dev/null +++ b/utils/IConfigBase.h @@ -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 +//头文件保护(防止头文件被重复包含)。 +#include "StatusCode.h" +#include +//StatusCode.h(可能是一个定义状态码枚举或类的文件,用于表示操作成功或失败的状态)和(C++标准库中的一部分,提供智能指针等内存管理功能)。 +class IConfigBase// 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); +}; +//IConfigBase是一个抽象基类,定义了与配置管理相关的接口。它包含了构造函数和析构函数的默认实现(即不执行任何操作),以及一系列虚函数,用于打开和关闭配置文件、保存配置以及获取和设置各种数据类型的配置值。这些虚函数的具体实现将由派生类提供。 +typedef struct i_config_base_header//定义了一个结构体i_config_base_header,包含一个指向字符的指针mCheckName,可能用于某种形式的验证或识别。 +{ + const char *mCheckName; +} IConfigBaseHeader; +typedef struct config_base//定义了一个结构体config_base,它包含一个IConfigBaseHeader类型的成员mHeader和一个指向IConfigBase的智能指针mIConfigBase。这个结构体可能用于在应用程序中存储和管理配置实例。 +{ + IConfigBaseHeader mHeader; + std::shared_ptr mIConfigBase; +} ConfigBase; +const char *GetConfigBaseModuleName(void); +std::shared_ptr *NewConfigBase(const char *fileName); +//声明了两个函数:GetConfigBaseModuleName(可能用于获取配置模块的名称)和NewConfigBase(接受一个文件名作为参数,并返回一个指向IConfigBase智能指针的指针,用于创建和返回一个新的配置实例)。 +#endif +//这段代码定义了一个用于配置管理的C++接口和相关数据结构,以及与之相关的函数声明。它使用了C++的抽象基类、虚函数、智能指针等特性来实现灵活的配置管理功能。 \ No newline at end of file From 6d4bbe46c11dc1780ca7ac432ab8895a7822fa49 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 08:44:09 +0000 Subject: [PATCH 07/19] Revert "configbasee " This reverts commit cacb6dcbf82c7c1847c54bb4cdd00a393ff9caad. --- utils/ConfigBase.h | 56 -------------------------------------- utils/ConfigBaseCode.h | 35 ------------------------ utils/ConfigBaseImpl.h | 52 ----------------------------------- utils/IConfigBase.h | 62 ------------------------------------------ 4 files changed, 205 deletions(-) delete mode 100644 utils/ConfigBase.h delete mode 100644 utils/ConfigBaseCode.h delete mode 100644 utils/ConfigBaseImpl.h delete mode 100644 utils/IConfigBase.h diff --git a/utils/ConfigBase.h b/utils/ConfigBase.h deleted file mode 100644 index 576ffd9d..00000000 --- a/utils/ConfigBase.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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" //声明是为了让C++编译器按照C语言的方式来处理被extern "C"包围的代码。这是必要的 -{ -#endif -//#ifndef CONFIG_BASE_H、#define CONFIG_BASE_H 和 #endif 这三个预处理指令一起工作,确保头文件CONFIG_BASE_H在任何编译单元中只被包含(include)一次。这是通过定义一个唯一的宏(CONFIG_BASE_H)来实现的,如果宏已定义,则跳过文件内容。 -enum CONFIG_CODE//定义了一个枚举类型CONFIG_CODE,用于可能的配置错误代码。它开始于STATUS_CODE_END(这里假设STATUS_CODE_END是在StatusCode.h中定义的某个状态码,表示一组状态码的结束),并定义了一个CONFIG_CODE_END作为枚举的结束标志。 -{ - 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); -//提供了一系列函数,用于打开、保存、关闭配置文件,以及获取和设置配置文件中的不同数据类型(如整型、短整型、长整型、长长整型、字符、布尔值、浮点数、双精度浮点数和字符串)的值。这些函数大多数都接受一个void *object作为第一个参数,这通常是一个指向配置文件上下文或对象的指针,用于访问和操作配置文件。其他参数包括配置项的名称(const char *name)和用于存储获取值的变量的指针(如int *value)。 -#ifdef __cplusplus -} -#endif -#endif -//总的来说,这个头文件定义了一个配置管理的接口,允许开发者以类型安全的方式读取和写入配置文件的值,同时考虑到了C和C++之间的互操作性。 \ No newline at end of file diff --git a/utils/ConfigBaseCode.h b/utils/ConfigBaseCode.h deleted file mode 100644 index 45ff9217..00000000 --- a/utils/ConfigBaseCode.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ -//版权声明和许可:文件开头包含了一段版权声明和许可信息,指出这段代码是由Fancy Code版权所有,并遵循Apache License, Version 2.0的条款。这意味着使用这段代码必须遵守Apache 2.0许可证的规定 -#ifndef CONFIG_BASE_CODE_H -#define CONFIG_BASE_CODE_H -//#ifndef CONFIG_BASE_CODE_H、#define CONFIG_BASE_CODE_H、#endif 这三段代码是标准的头文件保护符(也称为包含卫士),用于防止头文件被重复包含。 -#include "ConfigBase.h" -#include "StatusCode.h" -//这两行代码包含了其他两个头文件,这些文件可能定义了ConfigBase类和StatusCode枚举或类,这些在当前的头文件中会被使用。 -#ifdef __cplusplus -extern "C" { -#endif -//是为了在C++环境中使用时,确保C语言编写的代码能够被正确地链接和使用。extern "C"告诉C++编译器,这部分代码应该按照C语言的规则来编译和链接,以避免C++的名称修饰(Name Mangling)问题。 -#ifndef CONFIG_OWNER - #error This is internal file, never include it. - // 这两行代码是一个编译时检查。如果CONFIG_OWNER宏未定义,编译器会报错,并显示“This is internal file, never include it.”这条信息。这通常用于防止这个头文件被外部直接包含,可能是因为它包含了一些只应该在库内部使用的定义或声明。 -#endif -const StatusCode CreateConfigCode(const long int code);// 这行代码声明了一个函数CreateConfigCode,它接受一个long int类型的参数code,并返回一个StatusCode类型的值。这个函数可能用于根据给定的代码(可能是某种配置标识符)来创建一个配置对象或执行某种配置操作,并返回一个状态码来表示操作的成功与否。 -#ifdef __cplusplus -} -#endif -#endif -//总之,这个头文件定义了一个与配置代码相关的函数声明,并通过一系列的条件编译和头文件保护符来确保代码的正确性和安全性。 \ No newline at end of file diff --git a/utils/ConfigBaseImpl.h b/utils/ConfigBaseImpl.h deleted file mode 100644 index 79b03c34..00000000 --- a/utils/ConfigBaseImpl.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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"//这是一个接口定义文件,ConfigBaseImpl类实现了这个接口中声明的所有纯虚函数。 -#include //这是libconfig库的头文件,提供了处理配置文件的API。 -#include //C++标准库中的字符串处理类。 -class ConfigBaseImpl : public IConfigBase//ConfigBaseImpl类继承自IConfigBase接口,这意味着它需要实现接口中声明的所有纯虚函数。 -{ -public: - ConfigBaseImpl(const std::string &fileName); - virtual ~ConfigBaseImpl() = default; - bool OpenConfigFile(void) override;//打开配置文件 - void CloseConfigFile(void) override;//关闭配置文件 - StatusCode ConfigSaveFile(void) override;//用于将配置更改保存回文件,但返回类型是StatusCode - 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; -//这些函数提供了从配置文件中获取和设置各种数据类型(如整数、浮点数、字符串等)的接口。它们接受一个配置项的名称和一个指向值的指针(对于Get函数)或值本身(对于Set函数),并返回一个StatusCode表示操作结果。 -private: - const std::string mFileName;// 存储配置文件的路径。 - config_t mCfg;//libconfig库中的config_t类型,用于表示配置文件的内部结构。 -}; -#endif \ No newline at end of file diff --git a/utils/IConfigBase.h b/utils/IConfigBase.h deleted file mode 100644 index ab2caeb0..00000000 --- a/utils/IConfigBase.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 -//StatusCode.h(可能是一个定义状态码枚举或类的文件,用于表示操作成功或失败的状态)和(C++标准库中的一部分,提供智能指针等内存管理功能)。 -class IConfigBase// 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); -}; -//IConfigBase是一个抽象基类,定义了与配置管理相关的接口。它包含了构造函数和析构函数的默认实现(即不执行任何操作),以及一系列虚函数,用于打开和关闭配置文件、保存配置以及获取和设置各种数据类型的配置值。这些虚函数的具体实现将由派生类提供。 -typedef struct i_config_base_header//定义了一个结构体i_config_base_header,包含一个指向字符的指针mCheckName,可能用于某种形式的验证或识别。 -{ - const char *mCheckName; -} IConfigBaseHeader; -typedef struct config_base//定义了一个结构体config_base,它包含一个IConfigBaseHeader类型的成员mHeader和一个指向IConfigBase的智能指针mIConfigBase。这个结构体可能用于在应用程序中存储和管理配置实例。 -{ - IConfigBaseHeader mHeader; - std::shared_ptr mIConfigBase; -} ConfigBase; -const char *GetConfigBaseModuleName(void); -std::shared_ptr *NewConfigBase(const char *fileName); -//声明了两个函数:GetConfigBaseModuleName(可能用于获取配置模块的名称)和NewConfigBase(接受一个文件名作为参数,并返回一个指向IConfigBase智能指针的指针,用于创建和返回一个新的配置实例)。 -#endif -//这段代码定义了一个用于配置管理的C++接口和相关数据结构,以及与之相关的函数声明。它使用了C++的抽象基类、虚函数、智能指针等特性来实现灵活的配置管理功能。 \ No newline at end of file From d432adea526fbb387a23acd5dcd58a6514380a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 08:58:52 +0000 Subject: [PATCH 08/19] configbasee MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/ConfigBase.h | 56 ++++++++++++++++++++++++++++ utils/ConfigBase/ConfigBaseCode.h | 35 +++++++++++++++++ utils/ConfigBase/ConfigBaseImpl.h | 53 ++++++++++++++++++++++++++ utils/ConfigBase/IConfigBase.h | 62 +++++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 utils/ConfigBase/ConfigBase.h create mode 100644 utils/ConfigBase/ConfigBaseCode.h create mode 100644 utils/ConfigBase/ConfigBaseImpl.h create mode 100644 utils/ConfigBase/IConfigBase.h diff --git a/utils/ConfigBase/ConfigBase.h b/utils/ConfigBase/ConfigBase.h new file mode 100644 index 00000000..e001ed41 --- /dev/null +++ b/utils/ConfigBase/ConfigBase.h @@ -0,0 +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" //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++. \ No newline at end of file diff --git a/utils/ConfigBase/ConfigBaseCode.h b/utils/ConfigBase/ConfigBaseCode.h new file mode 100644 index 00000000..030aac9a --- /dev/null +++ b/utils/ConfigBase/ConfigBaseCode.h @@ -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. \ No newline at end of file diff --git a/utils/ConfigBase/ConfigBaseImpl.h b/utils/ConfigBase/ConfigBaseImpl.h new file mode 100644 index 00000000..e5ac3ae0 --- /dev/null +++ b/utils/ConfigBase/ConfigBaseImpl.h @@ -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 //This is the header file of the libconfig library, which provides an API for handling configuration files. +#include //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. \ No newline at end of file diff --git a/utils/ConfigBase/IConfigBase.h b/utils/ConfigBase/IConfigBase.h new file mode 100644 index 00000000..a69b0596 --- /dev/null +++ b/utils/ConfigBase/IConfigBase.h @@ -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 +//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(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 mIConfigBase; +} ConfigBase; +const char *GetConfigBaseModuleName(void); +std::shared_ptr *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. \ No newline at end of file From ea213cada64afafa73a44db61572bc7c6d0e90f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 08:59:16 +0000 Subject: [PATCH 09/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/ConfigBase.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/ConfigBase.h | 56 ----------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 utils/ConfigBase/ConfigBase.h diff --git a/utils/ConfigBase/ConfigBase.h b/utils/ConfigBase/ConfigBase.h deleted file mode 100644 index e001ed41..00000000 --- a/utils/ConfigBase/ConfigBase.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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++. \ No newline at end of file From f0ff1c2a75759f8d5080730505d7edcae06fbc9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:07:01 +0000 Subject: [PATCH 10/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/ConfigBaseCode.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/ConfigBaseCode.h | 35 ------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 utils/ConfigBase/ConfigBaseCode.h diff --git a/utils/ConfigBase/ConfigBaseCode.h b/utils/ConfigBase/ConfigBaseCode.h deleted file mode 100644 index 030aac9a..00000000 --- a/utils/ConfigBase/ConfigBaseCode.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. \ No newline at end of file From bef62c33927e204546e35aced34c0d643b05e6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:07:07 +0000 Subject: [PATCH 11/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/ConfigBaseImpl.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/ConfigBaseImpl.h | 53 ------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 utils/ConfigBase/ConfigBaseImpl.h diff --git a/utils/ConfigBase/ConfigBaseImpl.h b/utils/ConfigBase/ConfigBaseImpl.h deleted file mode 100644 index e5ac3ae0..00000000 --- a/utils/ConfigBase/ConfigBaseImpl.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 //This is the header file of the libconfig library, which provides an API for handling configuration files. -#include //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. \ No newline at end of file From 7a9c8357ada7998e7a89c7e67921018415a614a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:07:21 +0000 Subject: [PATCH 12/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/IConfigBase.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/IConfigBase.h | 62 ---------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 utils/ConfigBase/IConfigBase.h diff --git a/utils/ConfigBase/IConfigBase.h b/utils/ConfigBase/IConfigBase.h deleted file mode 100644 index a69b0596..00000000 --- a/utils/ConfigBase/IConfigBase.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 -//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(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 mIConfigBase; -} ConfigBase; -const char *GetConfigBaseModuleName(void); -std::shared_ptr *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. \ No newline at end of file From e56cc6bf1af81a3264f6cd94ab24ef59e2371ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:08:38 +0000 Subject: [PATCH 13/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/include/ConfigBase.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/include/ConfigBase.h | 52 --------------------------- 1 file changed, 52 deletions(-) delete mode 100644 utils/ConfigBase/include/ConfigBase.h diff --git a/utils/ConfigBase/include/ConfigBase.h b/utils/ConfigBase/include/ConfigBase.h deleted file mode 100644 index 0f2a330a..00000000 --- a/utils/ConfigBase/include/ConfigBase.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 \ No newline at end of file From 706f23472870aaabc1c37c970f988673d78f0976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:11:35 +0000 Subject: [PATCH 14/19] 12 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/include/ConfigBase.h | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 utils/ConfigBase/include/ConfigBase.h diff --git a/utils/ConfigBase/include/ConfigBase.h b/utils/ConfigBase/include/ConfigBase.h new file mode 100644 index 00000000..e001ed41 --- /dev/null +++ b/utils/ConfigBase/include/ConfigBase.h @@ -0,0 +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" //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++. \ No newline at end of file From e0e30696cb32b79e909f67122c5b480ea43755e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:12:31 +0000 Subject: [PATCH 15/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/src?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/src/ConfigBase.cpp | 183 ------------- utils/ConfigBase/src/ConfigBaseCode.c | 55 ---- utils/ConfigBase/src/ConfigBaseCode.h | 29 -- utils/ConfigBase/src/ConfigBaseImpl.cpp | 347 ------------------------ utils/ConfigBase/src/ConfigBaseImpl.h | 51 ---- utils/ConfigBase/src/IConfigBase.cpp | 123 --------- utils/ConfigBase/src/IConfigBase.h | 57 ---- 7 files changed, 845 deletions(-) delete mode 100644 utils/ConfigBase/src/ConfigBase.cpp delete mode 100644 utils/ConfigBase/src/ConfigBaseCode.c delete mode 100644 utils/ConfigBase/src/ConfigBaseCode.h delete mode 100644 utils/ConfigBase/src/ConfigBaseImpl.cpp delete mode 100644 utils/ConfigBase/src/ConfigBaseImpl.h delete mode 100644 utils/ConfigBase/src/IConfigBase.cpp delete mode 100644 utils/ConfigBase/src/IConfigBase.h diff --git a/utils/ConfigBase/src/ConfigBase.cpp b/utils/ConfigBase/src/ConfigBase.cpp deleted file mode 100644 index e9a16365..00000000 --- a/utils/ConfigBase/src/ConfigBase.cpp +++ /dev/null @@ -1,183 +0,0 @@ -/* - * 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 -#include -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 *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 *)object)->ConfigSaveFile(); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -void CloseConfigFile(void *object) -{ - if (ObjectCheck(object) == true) { - (*(std::shared_ptr *)object)->CloseConfigFile(); - (*(std::shared_ptr *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)object)->ConfigSetString(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseCode.c b/utils/ConfigBase/src/ConfigBaseCode.c deleted file mode 100644 index 3eca51ae..00000000 --- a/utils/ConfigBase/src/ConfigBaseCode.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 -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); -} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseCode.h b/utils/ConfigBase/src/ConfigBaseCode.h deleted file mode 100644 index 4b070a1d..00000000 --- a/utils/ConfigBase/src/ConfigBaseCode.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseImpl.cpp b/utils/ConfigBase/src/ConfigBaseImpl.cpp deleted file mode 100644 index 09fdffd3..00000000 --- a/utils/ConfigBase/src/ConfigBaseImpl.cpp +++ /dev/null @@ -1,347 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include -#include - -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); -} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseImpl.h b/utils/ConfigBase/src/ConfigBaseImpl.h deleted file mode 100644 index 58c35df5..00000000 --- a/utils/ConfigBase/src/ConfigBaseImpl.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 -#include -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 \ No newline at end of file diff --git a/utils/ConfigBase/src/IConfigBase.cpp b/utils/ConfigBase/src/IConfigBase.cpp deleted file mode 100644 index de94df75..00000000 --- a/utils/ConfigBase/src/IConfigBase.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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 -#include -#include -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 *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(fileName); - return (std::shared_ptr *)(((char *)impl) + sizeof(IConfigBaseHeader)); -} \ No newline at end of file diff --git a/utils/ConfigBase/src/IConfigBase.h b/utils/ConfigBase/src/IConfigBase.h deleted file mode 100644 index 0fec945a..00000000 --- a/utils/ConfigBase/src/IConfigBase.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 -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 mIConfigBase; -} ConfigBase; -const char *GetConfigBaseModuleName(void); -std::shared_ptr *NewConfigBase(const char *fileName); -#endif \ No newline at end of file From 62ba1ec99d2e1f0adad87a16b930440a46e302b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:12:47 +0000 Subject: [PATCH 16/19] 11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/src/ConfigBase.cpp | 183 +++++++++++++ utils/ConfigBase/src/ConfigBaseCode.c | 55 ++++ utils/ConfigBase/src/ConfigBaseCode.h | 35 +++ utils/ConfigBase/src/ConfigBaseImpl.cpp | 347 ++++++++++++++++++++++++ utils/ConfigBase/src/ConfigBaseImpl.h | 53 ++++ utils/ConfigBase/src/IConfigBase.cpp | 123 +++++++++ utils/ConfigBase/src/IConfigBase.h | 62 +++++ 7 files changed, 858 insertions(+) create mode 100644 utils/ConfigBase/src/ConfigBase.cpp create mode 100644 utils/ConfigBase/src/ConfigBaseCode.c create mode 100644 utils/ConfigBase/src/ConfigBaseCode.h create mode 100644 utils/ConfigBase/src/ConfigBaseImpl.cpp create mode 100644 utils/ConfigBase/src/ConfigBaseImpl.h create mode 100644 utils/ConfigBase/src/IConfigBase.cpp create mode 100644 utils/ConfigBase/src/IConfigBase.h diff --git a/utils/ConfigBase/src/ConfigBase.cpp b/utils/ConfigBase/src/ConfigBase.cpp new file mode 100644 index 00000000..e7f19927 --- /dev/null +++ b/utils/ConfigBase/src/ConfigBase.cpp @@ -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 +#include +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 *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 *)object)->ConfigSaveFile(); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +void CloseConfigFile(void *object) +{ + if (ObjectCheck(object) == true) { + (*(std::shared_ptr *)object)->CloseConfigFile(); + (*(std::shared_ptr *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)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 *)object)->ConfigSetString(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseCode.c b/utils/ConfigBase/src/ConfigBaseCode.c new file mode 100644 index 00000000..02b0f434 --- /dev/null +++ b/utils/ConfigBase/src/ConfigBaseCode.c @@ -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 +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); +} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseCode.h b/utils/ConfigBase/src/ConfigBaseCode.h new file mode 100644 index 00000000..030aac9a --- /dev/null +++ b/utils/ConfigBase/src/ConfigBaseCode.h @@ -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. \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseImpl.cpp b/utils/ConfigBase/src/ConfigBaseImpl.cpp new file mode 100644 index 00000000..c0e91c53 --- /dev/null +++ b/utils/ConfigBase/src/ConfigBaseImpl.cpp @@ -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 +#include +#include +#include +#include +#include +#include + +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); +} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseImpl.h b/utils/ConfigBase/src/ConfigBaseImpl.h new file mode 100644 index 00000000..e5ac3ae0 --- /dev/null +++ b/utils/ConfigBase/src/ConfigBaseImpl.h @@ -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 //This is the header file of the libconfig library, which provides an API for handling configuration files. +#include //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. \ No newline at end of file diff --git a/utils/ConfigBase/src/IConfigBase.cpp b/utils/ConfigBase/src/IConfigBase.cpp new file mode 100644 index 00000000..09447bd5 --- /dev/null +++ b/utils/ConfigBase/src/IConfigBase.cpp @@ -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 +#include +#include +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 *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(fileName); + return (std::shared_ptr *)(((char *)impl) + sizeof(IConfigBaseHeader)); +} \ No newline at end of file diff --git a/utils/ConfigBase/src/IConfigBase.h b/utils/ConfigBase/src/IConfigBase.h new file mode 100644 index 00000000..a69b0596 --- /dev/null +++ b/utils/ConfigBase/src/IConfigBase.h @@ -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 +//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(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 mIConfigBase; +} ConfigBase; +const char *GetConfigBaseModuleName(void); +std::shared_ptr *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. \ No newline at end of file From a795e42b1291c256038aaf4c635c6a7f9363ff16 Mon Sep 17 00:00:00 2001 From: ssslleep <2720057338@qq.com> Date: Sat, 27 Jul 2024 09:33:16 +0000 Subject: [PATCH 17/19] update utils/UartDevice/readme.md. Signed-off-by: ssslleep <2720057338@qq.com> --- utils/UartDevice/readme.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/utils/UartDevice/readme.md b/utils/UartDevice/readme.md index 2480b329..09c8174e 100644 --- a/utils/UartDevice/readme.md +++ b/utils/UartDevice/readme.md @@ -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设备。 \ No newline at end of file +* **UartDevice.h**:为上层应用提供了封装好的函数接口,用于与uart硬件设备进行交互。 +* **UartDevice.cpp**:UartDevice.h中函数接口的具体定义和封装。 +* **UartDeviceImpl.cpp/UartDeviceImpl.h**:对uart硬件设备更底层更具体的一些操作,这里面定义和声明的函数会提供给UartDevice.cpp做进一步的封装。 From bf6c7f7b381e1f74600bd2a14279fbed9537df84 Mon Sep 17 00:00:00 2001 From: ssslleep <2720057338@qq.com> Date: Sat, 27 Jul 2024 09:35:49 +0000 Subject: [PATCH 18/19] update utils/UartDevice/readme.md. Signed-off-by: ssslleep <2720057338@qq.com> --- utils/UartDevice/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/UartDevice/readme.md b/utils/UartDevice/readme.md index 09c8174e..3d492a85 100644 --- a/utils/UartDevice/readme.md +++ b/utils/UartDevice/readme.md @@ -11,4 +11,4 @@ # 各文件的作用 * **UartDevice.h**:为上层应用提供了封装好的函数接口,用于与uart硬件设备进行交互。 * **UartDevice.cpp**:UartDevice.h中函数接口的具体定义和封装。 -* **UartDeviceImpl.cpp/UartDeviceImpl.h**:对uart硬件设备更底层更具体的一些操作,这里面定义和声明的函数会提供给UartDevice.cpp做进一步的封装。 +* **UartDeviceImpl.cpp/UartDeviceImpl.h**:这里面定义和声明的函数是对uart硬件设备更底层更具体的一些操作,它们会被提供给UartDevice.cpp做进一步的封装。 From 5b31f9fbde07d0137c5fa0d418fda0be6c79e6f8 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 20:59:44 +0800 Subject: [PATCH 19/19] add some annotate --- utils/TcpModule/src/TcpServerImpl.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/TcpModule/src/TcpServerImpl.h b/utils/TcpModule/src/TcpServerImpl.h index 007f63fb..44dd08a4 100644 --- a/utils/TcpModule/src/TcpServerImpl.h +++ b/utils/TcpModule/src/TcpServerImpl.h @@ -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 *> mClients;