Improve:ConfigBase module.
This commit is contained in:
parent
e5a2d7cfd9
commit
b8cd52e849
|
@ -8,7 +8,7 @@ include_directories(
|
|||
./include
|
||||
${UTILS_SOURCE_PATH}/StatusCode/include
|
||||
${UTILS_SOURCE_PATH}/Log/include
|
||||
${UTILS_SOURCE_PATH}/Config/include
|
||||
${UTILS_SOURCE_PATH}/ConfigBase/include
|
||||
)
|
||||
#do not rely on any other library
|
||||
#link_directories(
|
||||
|
|
|
@ -96,89 +96,30 @@ class IIpcConfig
|
|||
public:
|
||||
IIpcConfig() = default;
|
||||
virtual ~IIpcConfig() = default;
|
||||
virtual const StatusCode ConfigFileSave(void)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
static std::shared_ptr<IIpcConfig> &GetInstance(std::shared_ptr<IIpcConfig> *impl = nullptr);
|
||||
virtual const StatusCode Init(void)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
virtual const StatusCode UnInit(void)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
virtual const int GetInt(const IpcConfigKey &key)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
virtual void SetInt(const IpcConfigKey &key, const int &value)
|
||||
{
|
||||
}
|
||||
virtual const short GetShort(const IpcConfigKey &key)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
virtual void SetShort(const IpcConfigKey &key, const short &value)
|
||||
{
|
||||
}
|
||||
virtual const long GetLong(const IpcConfigKey &key)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
virtual void SetLong(const IpcConfigKey &key, const long &value)
|
||||
{
|
||||
}
|
||||
virtual const long long GetLLong(const IpcConfigKey &key)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
virtual void SetLLong(const IpcConfigKey &key, const long long &value)
|
||||
{
|
||||
}
|
||||
virtual const char GetChar(const IpcConfigKey &key)
|
||||
{
|
||||
return '\0';
|
||||
}
|
||||
virtual void SetChar(const IpcConfigKey &key, const char &value)
|
||||
{
|
||||
}
|
||||
virtual const float GetFloat(const IpcConfigKey &key)
|
||||
{
|
||||
return -1.0;
|
||||
}
|
||||
virtual void SetFloat(const IpcConfigKey &key, const float &value)
|
||||
{
|
||||
}
|
||||
virtual const double GetDouble(const IpcConfigKey &key)
|
||||
{
|
||||
return -1.0;
|
||||
}
|
||||
virtual void SetDouble(const IpcConfigKey &key, const double &value)
|
||||
{
|
||||
}
|
||||
virtual const long double GetLongDouble(const IpcConfigKey &key)
|
||||
{
|
||||
return -1.0;
|
||||
}
|
||||
virtual void SetLongDouble(const IpcConfigKey &key, const long double &value)
|
||||
{
|
||||
}
|
||||
virtual const bool GetBool(const IpcConfigKey &key)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
virtual void SetBool(const IpcConfigKey &key, const bool &value)
|
||||
{
|
||||
}
|
||||
virtual const std::string GetString(const IpcConfigKey &key)
|
||||
{
|
||||
return "undefine";
|
||||
}
|
||||
virtual void SetString(const IpcConfigKey &key, const std::string string)
|
||||
{
|
||||
}
|
||||
virtual const StatusCode ConfigFileSave(void);
|
||||
virtual const StatusCode Init(void);
|
||||
virtual const StatusCode UnInit(void);
|
||||
virtual const int GetInt(const IpcConfigKey &key);
|
||||
virtual void SetInt(const IpcConfigKey &key, const int &value);
|
||||
virtual const short GetShort(const IpcConfigKey &key);
|
||||
virtual void SetShort(const IpcConfigKey &key, const short &value);
|
||||
virtual const long GetLong(const IpcConfigKey &key);
|
||||
virtual void SetLong(const IpcConfigKey &key, const long &value);
|
||||
virtual const long long GetLLong(const IpcConfigKey &key);
|
||||
virtual void SetLLong(const IpcConfigKey &key, const long long &value);
|
||||
virtual const char GetChar(const IpcConfigKey &key);
|
||||
virtual void SetChar(const IpcConfigKey &key, const char &value);
|
||||
virtual const float GetFloat(const IpcConfigKey &key);
|
||||
virtual void SetFloat(const IpcConfigKey &key, const float &value);
|
||||
virtual const double GetDouble(const IpcConfigKey &key);
|
||||
virtual void SetDouble(const IpcConfigKey &key, const double &value);
|
||||
virtual const long double GetLongDouble(const IpcConfigKey &key);
|
||||
virtual void SetLongDouble(const IpcConfigKey &key, const long double &value);
|
||||
virtual const bool GetBool(const IpcConfigKey &key);
|
||||
virtual void SetBool(const IpcConfigKey &key, const bool &value);
|
||||
virtual const std::string GetString(const IpcConfigKey &key);
|
||||
virtual void SetString(const IpcConfigKey &key, const std::string string);
|
||||
};
|
||||
bool CreateIpcConfig(void);
|
||||
#endif
|
|
@ -27,4 +27,86 @@ std::shared_ptr<IIpcConfig> &IIpcConfig::GetInstance(std::shared_ptr<IIpcConfig>
|
|||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
const StatusCode IIpcConfig::ConfigFileSave(void)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
const StatusCode IIpcConfig::Init(void)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
const StatusCode IIpcConfig::UnInit(void)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
const int IIpcConfig::GetInt(const IpcConfigKey &key)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
void IIpcConfig::SetInt(const IpcConfigKey &key, const int &value)
|
||||
{
|
||||
}
|
||||
const short IIpcConfig::GetShort(const IpcConfigKey &key)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
void IIpcConfig::SetShort(const IpcConfigKey &key, const short &value)
|
||||
{
|
||||
}
|
||||
const long IIpcConfig::GetLong(const IpcConfigKey &key)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
void IIpcConfig::SetLong(const IpcConfigKey &key, const long &value)
|
||||
{
|
||||
}
|
||||
const long long IIpcConfig::GetLLong(const IpcConfigKey &key)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
void IIpcConfig::SetLLong(const IpcConfigKey &key, const long long &value)
|
||||
{
|
||||
}
|
||||
const char IIpcConfig::GetChar(const IpcConfigKey &key)
|
||||
{
|
||||
return '\0';
|
||||
}
|
||||
void IIpcConfig::SetChar(const IpcConfigKey &key, const char &value)
|
||||
{
|
||||
}
|
||||
const float IIpcConfig::GetFloat(const IpcConfigKey &key)
|
||||
{
|
||||
return -1.0;
|
||||
}
|
||||
void IIpcConfig::SetFloat(const IpcConfigKey &key, const float &value)
|
||||
{
|
||||
}
|
||||
const double IIpcConfig::GetDouble(const IpcConfigKey &key)
|
||||
{
|
||||
return -1.0;
|
||||
}
|
||||
void IIpcConfig::SetDouble(const IpcConfigKey &key, const double &value)
|
||||
{
|
||||
}
|
||||
const long double IIpcConfig::GetLongDouble(const IpcConfigKey &key)
|
||||
{
|
||||
return -1.0;
|
||||
}
|
||||
void IIpcConfig::SetLongDouble(const IpcConfigKey &key, const long double &value)
|
||||
{
|
||||
}
|
||||
const bool IIpcConfig::GetBool(const IpcConfigKey &key)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
void IIpcConfig::SetBool(const IpcConfigKey &key, const bool &value)
|
||||
{
|
||||
}
|
||||
const std::string IIpcConfig::GetString(const IpcConfigKey &key)
|
||||
{
|
||||
return "undefine";
|
||||
}
|
||||
void IIpcConfig::SetString(const IpcConfigKey &key, const std::string string)
|
||||
{
|
||||
}
|
|
@ -143,7 +143,7 @@ void IpcConfig::SetInt(const IpcConfigKey &key, const int &value)
|
|||
iter = mCfgMapInt.find(key);
|
||||
if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = value;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetInt(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -168,7 +168,7 @@ void IpcConfig::SetShort(const IpcConfigKey &key, const short &value)
|
|||
iter = mCfgMapShort.find(key);
|
||||
if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = value;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetShort(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -193,7 +193,7 @@ void IpcConfig::SetLong(const IpcConfigKey &key, const long &value)
|
|||
iter = mCfgMapLong.find(key);
|
||||
if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = value;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetLong(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -218,7 +218,7 @@ void IpcConfig::SetLLong(const IpcConfigKey &key, const long long &value)
|
|||
iter = mCfgMapLLong.find(key);
|
||||
if (iter != mCfgMapLLong.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = value;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetLLong(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -243,7 +243,7 @@ void IpcConfig::SetChar(const IpcConfigKey &key, const char &character)
|
|||
iter = mCfgMapChar.find(key);
|
||||
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = character;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetChar(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -268,7 +268,7 @@ void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value)
|
|||
iter = mCfgMapFloat.find(key);
|
||||
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = value;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetFloat(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -293,7 +293,7 @@ void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value)
|
|||
iter = mCfgMapDouble.find(key);
|
||||
if (iter != mCfgMapDouble.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = value;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetDouble(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -341,7 +341,7 @@ void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value)
|
|||
iter = mCfgMapBool.find(key);
|
||||
if (iter != mCfgMapBool.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = value;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetBool(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -367,7 +367,7 @@ void IpcConfig::SetString(const IpcConfigKey &key, const std::string string)
|
|||
iter = mCfgMapString.find(key);
|
||||
if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) {
|
||||
strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[]
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
const char *name = iter->second.begin()->first.c_str();
|
||||
ConfigSetString(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
|
@ -402,7 +402,7 @@ void IpcConfig::ReadAllConfigParameters(void)
|
|||
ConfigSetInt(mCfg, "burst_photo_interval", mAllData.burstPhotoInterval);
|
||||
}
|
||||
|
||||
const char *imageSizeString = NULL;
|
||||
const char *imageSizeString = nullptr;
|
||||
StatusCode imageSizeCode = ConfigGetString(mCfg, "image_size", &(imageSizeString));
|
||||
if (StatusCodeEqual(imageSizeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
||||
LogWarning("image_size doesn't exist, will make it as default.\n");
|
||||
|
@ -412,7 +412,12 @@ void IpcConfig::ReadAllConfigParameters(void)
|
|||
ConfigSetString(mCfg, "image_size", mAllData.imageSize);
|
||||
}
|
||||
else {
|
||||
strncpy(mAllData.imageSize, imageSizeString, sizeof(mAllData.imageSize));
|
||||
if (nullptr != imageSizeString) {
|
||||
strncpy(mAllData.imageSize, imageSizeString, sizeof(mAllData.imageSize));
|
||||
}
|
||||
else {
|
||||
LogError("image_size get failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
StatusCode videoSizeCode = ConfigGetInt(mCfg, "video_size", &(mAllData.videoSize));
|
||||
|
@ -474,7 +479,7 @@ void IpcConfig::ReadAllConfigParameters(void)
|
|||
ConfigSetBool(mCfg, "formatting_SD_card", mAllData.formattingSDCard);
|
||||
}
|
||||
|
||||
const char *darkModeString = NULL;
|
||||
const char *darkModeString = nullptr;
|
||||
StatusCode darkModeCode = ConfigGetString(mCfg, "dark_mode", &(darkModeString));
|
||||
if (StatusCodeEqual(darkModeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
||||
LogWarning("dark_mode doesn't exist, will make it as default.\n");
|
||||
|
@ -484,10 +489,15 @@ void IpcConfig::ReadAllConfigParameters(void)
|
|||
ConfigSetString(mCfg, "dark_mode", mAllData.darkMode);
|
||||
}
|
||||
else {
|
||||
strncpy(mAllData.darkMode, darkModeString, sizeof(mAllData.darkMode));
|
||||
if (nullptr != darkModeString) {
|
||||
strncpy(mAllData.darkMode, darkModeString, sizeof(mAllData.darkMode));
|
||||
}
|
||||
else {
|
||||
LogError("dark_mode get failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
const char *workIntervalString = NULL;
|
||||
const char *workIntervalString = nullptr;
|
||||
StatusCode workIntervalCode = ConfigGetString(mCfg, "work_interval", &(workIntervalString));
|
||||
if (StatusCodeEqual(workIntervalCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
||||
LogWarning("work_interval doesn't exist, will make it as default.\n");
|
||||
|
@ -497,7 +507,12 @@ void IpcConfig::ReadAllConfigParameters(void)
|
|||
ConfigSetString(mCfg, "work_interval", mAllData.workingInterval);
|
||||
}
|
||||
else {
|
||||
strncpy(mAllData.workingInterval, workIntervalString, sizeof(mAllData.workingInterval));
|
||||
if (nullptr != workIntervalString) {
|
||||
strncpy(mAllData.workingInterval, workIntervalString, sizeof(mAllData.workingInterval));
|
||||
}
|
||||
else {
|
||||
LogError("work_interval get failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
StatusCode shortCode = ConfigGetShort(mCfg, "test_short", &(mAllData.testShort));
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
*/
|
||||
#ifndef IPCCONFIG_H
|
||||
#define IPCCONFIG_H
|
||||
#include "Config.h"
|
||||
#include "ConfigBase.h"
|
||||
#include "IIpcConfig.h"
|
||||
#include "StatusCode.h"
|
||||
#include <map>
|
||||
|
@ -84,7 +84,7 @@ private:
|
|||
|
||||
private:
|
||||
bool mCfgChanged;
|
||||
VConfig *mCfg;
|
||||
void *mCfg;
|
||||
Config_s mAllData;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>> mCfgMapInt;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>> mCfgMapShort;
|
||||
|
|
|
@ -18,10 +18,6 @@ link_directories(
|
|||
${EXTERNAL_LIBS_OUTPUT_PATH}
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
aux_source_directory(. SRC_FILES_MAIN)
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
|
||||
# cmake_minimum_required(VERSION 2.8.0)
|
||||
add_subdirectory(Config)
|
||||
add_subdirectory(ConfigBase)
|
||||
add_subdirectory(StatusCode)
|
||||
add_subdirectory(Log)
|
||||
add_subdirectory(SharedData)
|
||||
|
|
|
@ -14,8 +14,6 @@ include_directories(
|
|||
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
|
||||
# )
|
||||
|
||||
|
||||
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
|
||||
set(TARGET_NAME ConfigBase)
|
||||
|
|
79
utils/ConfigBase/CMakeLists.txt
Normal file
79
utils/ConfigBase/CMakeLists.txt
Normal file
|
@ -0,0 +1,79 @@
|
|||
|
||||
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
||||
include(build/config_base.cmake)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
|
||||
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
||||
|
||||
include_directories(
|
||||
./src
|
||||
./include
|
||||
${UTILS_SOURCE_PATH}/StatusCode/include
|
||||
${UTILS_SOURCE_PATH}/Log/include
|
||||
${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib
|
||||
)
|
||||
# link_directories(
|
||||
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
|
||||
# )
|
||||
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
|
||||
set(TARGET_NAME ConfigBase)
|
||||
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
|
||||
target_link_libraries(${TARGET_NAME} StatusCode Log libconfig.a)
|
||||
|
||||
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
|
||||
add_custom_target(
|
||||
ConfigBase_code_check
|
||||
COMMAND ${CLANG_TIDY_EXE}
|
||||
-checks='${CLANG_TIDY_CHECKS}'
|
||||
--header-filter=.*
|
||||
--system-headers=false
|
||||
${SRC_FILES}
|
||||
${CLANG_TIDY_CONFIG}
|
||||
-p ${PLATFORM_PATH}/cmake-shell
|
||||
WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/ConfigBase
|
||||
)
|
||||
add_custom_command(
|
||||
TARGET ${TARGET_NAME}
|
||||
PRE_BUILD
|
||||
COMMAND make ConfigBase_code_check
|
||||
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
|
||||
)
|
||||
file(GLOB_RECURSE HEADER_FILES *.h)
|
||||
add_custom_target(
|
||||
ConfigBase_code_format
|
||||
COMMAND ${CLANG_FORMAT_EXE}
|
||||
-style=file
|
||||
-i ${SRC_FILES} ${HEADER_FILES}
|
||||
WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/ConfigBase
|
||||
)
|
||||
add_custom_command(
|
||||
TARGET ${TARGET_NAME}
|
||||
PRE_BUILD
|
||||
COMMAND make ConfigBase_code_check
|
||||
COMMAND make ConfigBase_code_format
|
||||
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
|
||||
)
|
||||
endif()
|
||||
|
||||
# build libconfig before make libConfigBase.a
|
||||
add_custom_command(
|
||||
# OUTPUT ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs/libconfig.a
|
||||
OUTPUT ${EXTERNAL_LIBS_OUTPUT_PATH}/libconfig.a
|
||||
COMMAND echo "Build libconfig-1.7.3. COMPILE_HOST = ${COMPILE_HOST}"
|
||||
# COMMAND tar zxvf libconfig-1.7.3.tar.gz
|
||||
COMMAND sh build_libconfig.sh ${TARGET_PLATFORM} ${COMPILE_HOST}
|
||||
COMMAND mv ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs/libconfig.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libconfig.a
|
||||
WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/libconfig/
|
||||
)
|
||||
add_custom_target(
|
||||
libconfig.a
|
||||
# DEPENDS ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs/libconfig.a
|
||||
DEPENDS ${EXTERNAL_LIBS_OUTPUT_PATH}/libconfig.a
|
||||
)
|
||||
|
||||
define_file_name(${TARGET_NAME})
|
||||
config_owner(${TARGET_NAME})
|
||||
|
||||
file(GLOB_RECURSE INSTALL_HEADER_FILES include/*.h)
|
||||
install(FILES ${INSTALL_HEADER_FILES} DESTINATION include)
|
12
utils/ConfigBase/build/config_base.cmake
Normal file
12
utils/ConfigBase/build/config_base.cmake
Normal file
|
@ -0,0 +1,12 @@
|
|||
function(config_owner target)
|
||||
get_target_property(source_files "${target}" SOURCES)
|
||||
foreach(source_file ${source_files})
|
||||
get_property(defs SOURCE "${source_file}"
|
||||
PROPERTY COMPILE_DEFINITIONS)
|
||||
get_filename_component(file_name "${source_file}" NAME)
|
||||
list(APPEND defs "CONFIG_OWNER")
|
||||
set_property(
|
||||
SOURCE "${source_file}"
|
||||
PROPERTY COMPILE_DEFINITIONS ${defs})
|
||||
endforeach()
|
||||
endfunction()
|
52
utils/ConfigBase/include/ConfigBase.h
Normal file
52
utils/ConfigBase/include/ConfigBase.h
Normal file
|
@ -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_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
|
178
utils/ConfigBase/src/ConfigBase.cpp
Normal file
178
utils/ConfigBase/src/ConfigBase.cpp
Normal file
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
* 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"
|
||||
static bool ObjectCheck(void *object)
|
||||
{
|
||||
if (nullptr == object) {
|
||||
LogError("nullptr object!\n");
|
||||
return false;
|
||||
}
|
||||
if (*((const char **)(((char *)object) - sizeof(IConfigBase))) != GetConfigBaseModuleName()) {
|
||||
LogError("Illegal object!\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void *OpenConfigFile(const char *fileName)
|
||||
{
|
||||
std::shared_ptr<IConfigBase> *configObject = NewConfigBase(fileName);
|
||||
if (nullptr != configObject) {
|
||||
(*configObject)->OpenConfigFile();
|
||||
}
|
||||
return configObject;
|
||||
}
|
||||
StatusCode ConfigSaveFile(void *object)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSaveFile();
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
void CloseConfigFile(void *object)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
(*(std::shared_ptr<IConfigBase> *)object)->CloseConfigFile();
|
||||
(*(std::shared_ptr<IConfigBase> *)object).reset();
|
||||
free(((char *)object) - sizeof(IConfigBase)); // TODO: bug?
|
||||
}
|
||||
}
|
||||
StatusCode ConfigGetInt(void *object, const char *name, int *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetInt(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetInt(void *object, const char *name, const int value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetInt(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigGetShort(void *object, const char *name, short *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetShort(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetShort(void *object, const char *name, const short value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetShort(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigGetLong(void *object, const char *name, long *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetLong(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetLong(void *object, const char *name, const long value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetLong(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigGetLLong(void *object, const char *name, long long *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetLLong(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetLLong(void *object, const char *name, const long long value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetLLong(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigGetChar(void *object, const char *name, char *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetChar(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetChar(void *object, const char *name, const char value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetChar(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigGetBool(void *object, const char *name, bool *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetBool(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetBool(void *object, const char *name, const bool value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetBool(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigGetFloat(void *object, const char *name, float *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetFloat(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetFloat(void *object, const char *name, const float value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetFloat(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigGetDouble(void *object, const char *name, double *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetDouble(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetDouble(void *object, const char *name, const double value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetDouble(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigGetString(void *object, const char *name, const char **value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigGetString(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
StatusCode ConfigSetString(void *object, const char *name, const char *value)
|
||||
{
|
||||
if (ObjectCheck(object) == true) {
|
||||
return (*(std::shared_ptr<IConfigBase> *)object)->ConfigSetString(name, value);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
53
utils/ConfigBase/src/ConfigBaseCode.c
Normal file
53
utils/ConfigBase/src/ConfigBaseCode.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "ConfigBaseCode.h"
|
||||
#include "ILog.h"
|
||||
#include <string.h>
|
||||
static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = {"CONFIG_CODE_PARAM_NOT_EXIST",
|
||||
"CONFIG_CODE_END"};
|
||||
static const char *PrintStringConfigCode(const StatusCode this)
|
||||
{
|
||||
const int CODE_INDEX = this.mStatusCode - STATUS_CODE_END;
|
||||
if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= CONFIG_CODE_END) {
|
||||
LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]);
|
||||
return ConfigCodeString[CODE_INDEX];
|
||||
}
|
||||
LogError("Config code undefine.\n");
|
||||
return "Config code undefine.\n";
|
||||
}
|
||||
static const bool CodeEqual(const StatusCode code, const char *value)
|
||||
{
|
||||
if (memcmp(value, ConfigCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static StatusCode NewConfigCode(const long int code)
|
||||
{
|
||||
StatusCode result = {PrintStringConfigCode, CodeEqual, code};
|
||||
return result;
|
||||
}
|
||||
const StatusCode CreateConfigCode(const long int code)
|
||||
{
|
||||
// if (STATUS_CODE_OK <= code && code < STATUS_CODE_END)
|
||||
// {
|
||||
// return CreateStatusCode(code);
|
||||
// }
|
||||
if (STATUS_CODE_END <= code && code < CONFIG_CODE_END) {
|
||||
return NewConfigCode(code);
|
||||
}
|
||||
LogError("undefined code.\n");
|
||||
return CreateStatusCode(STATUS_CODE_END);
|
||||
}
|
29
utils/ConfigBase/src/ConfigBaseCode.h
Normal file
29
utils/ConfigBase/src/ConfigBaseCode.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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
|
339
utils/ConfigBase/src/ConfigBaseImpl.cpp
Normal file
339
utils/ConfigBase/src/ConfigBaseImpl.cpp
Normal file
|
@ -0,0 +1,339 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "ConfigBaseImpl.h"
|
||||
#include "ConfigBaseCode.h"
|
||||
#include "ILog.h"
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <unistd.h>
|
||||
|
||||
constexpr int INVALID_RESULT = -1;
|
||||
#define CHECK_SHORT_LIMIT(value) (value > SHRT_MAX ? false : (value < SHRT_MIN ? false : true))
|
||||
#define CHECK_LONG_LIMIT(value) (value > LONG_MAX ? false : (value < LONG_MIN ? false : true))
|
||||
#define CHECK_CHAR_LIMIT(value) (value > CHAR_MAX ? false : (value < CHAR_MIN ? false : true))
|
||||
#define CHECK_FLOAT_LIMIT(value) (fabs(value - ((float)value)) < 0.000001 ? false : true)
|
||||
|
||||
ConfigBaseImpl::ConfigBaseImpl(const std::string &fileName) : mFileName(fileName)
|
||||
{
|
||||
}
|
||||
void ConfigBaseImpl::OpenConfigFile(void)
|
||||
{
|
||||
config_init(&cfg);
|
||||
config_set_options(&cfg,
|
||||
(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(&cfg, mFileName.c_str())) {
|
||||
LogError("Read file failed[%s].\n", mFileName.c_str());
|
||||
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
LogInfo("Config file doesn't exist.\n");
|
||||
/* Write out the new configuration. */
|
||||
if (!config_write_file(&cfg, mFileName.c_str())) {
|
||||
fprintf(stderr, "Error while writing file.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
void ConfigBaseImpl::CloseConfigFile(void)
|
||||
{
|
||||
config_destroy(&cfg);
|
||||
}
|
||||
StatusCode ConfigBaseImpl::ConfigSaveFile(void)
|
||||
{
|
||||
LogInfo("Save file[%s].\n", mFileName.c_str());
|
||||
if (!config_write_file(&cfg, 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(&cfg, 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(&cfg);
|
||||
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(&cfg, 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(&cfg);
|
||||
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(&cfg, 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(&cfg);
|
||||
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(&cfg, 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(&cfg);
|
||||
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(&cfg, 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(&cfg);
|
||||
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(&cfg, 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(&cfg);
|
||||
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(&cfg, 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(&cfg);
|
||||
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(&cfg, 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(&cfg);
|
||||
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(&cfg, 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(&cfg);
|
||||
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);
|
||||
}
|
50
utils/ConfigBase/src/ConfigBaseImpl.h
Normal file
50
utils/ConfigBase/src/ConfigBaseImpl.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CONFIG_BASE_IMPL_H
|
||||
#define CONFIG_BASE_IMPL_H
|
||||
#include "IConfigBase.h"
|
||||
#include <libconfig.h>
|
||||
class ConfigBaseImpl : public IConfigBase
|
||||
{
|
||||
public:
|
||||
ConfigBaseImpl(const std::string &fileName);
|
||||
virtual ~ConfigBaseImpl() = default;
|
||||
void 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 cfg;
|
||||
};
|
||||
#endif
|
119
utils/ConfigBase/src/IConfigBase.cpp
Normal file
119
utils/ConfigBase/src/IConfigBase.cpp
Normal file
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* 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 <cstring>
|
||||
void IConfigBase::OpenConfigFile(void)
|
||||
{
|
||||
}
|
||||
void IConfigBase::CloseConfigFile(void)
|
||||
{
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSaveFile(void)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetInt(const char *name, int *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetInt(const char *name, const int value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetShort(const char *name, short *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetShort(const char *name, const short value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetLong(const char *name, long *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetLong(const char *name, const long value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetLLong(const char *name, long long *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetLLong(const char *name, const long long value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetChar(const char *name, char *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetChar(const char *name, const char value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetBool(const char *name, bool *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetBool(const char *name, const bool value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetFloat(const char *name, float *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetFloat(const char *name, const float value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetDouble(const char *name, double *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetDouble(const char *name, const double value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigGetString(const char *name, const char **value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode IConfigBase::ConfigSetString(const char *name, const char *value)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
static const char *CONFIG_BASE_NAME = "config_base";
|
||||
const char *GetConfigBaseModuleName(void)
|
||||
{
|
||||
return CONFIG_BASE_NAME;
|
||||
}
|
||||
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName)
|
||||
{
|
||||
LogInfo("Create the uart device object.\n");
|
||||
ConfigBase *impl = (ConfigBase *)malloc(sizeof(ConfigBase));
|
||||
if (nullptr == impl) {
|
||||
LogError("NewConfigBase::malloc failed.\n");
|
||||
return nullptr;
|
||||
}
|
||||
ConfigBase tmp;
|
||||
memcpy((void *)impl, (void *)&tmp, sizeof(ConfigBase));
|
||||
impl->mHeader.mCheckName = CONFIG_BASE_NAME;
|
||||
impl->mIConfigBase = std::make_shared<ConfigBaseImpl>(fileName);
|
||||
return (std::shared_ptr<IConfigBase> *)(((char *)impl) + sizeof(IConfigBaseHeader));
|
||||
}
|
57
utils/ConfigBase/src/IConfigBase.h
Normal file
57
utils/ConfigBase/src/IConfigBase.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef I_CONFIG_BASE_H
|
||||
#define I_CONFIG_BASE_H
|
||||
#include "StatusCode.h"
|
||||
#include <memory>
|
||||
class IConfigBase
|
||||
{
|
||||
public:
|
||||
IConfigBase() = default;
|
||||
virtual ~IConfigBase() = default;
|
||||
virtual void OpenConfigFile(void);
|
||||
virtual void CloseConfigFile(void);
|
||||
virtual StatusCode ConfigSaveFile(void);
|
||||
virtual StatusCode ConfigGetInt(const char *name, int *value);
|
||||
virtual StatusCode ConfigSetInt(const char *name, const int value);
|
||||
virtual StatusCode ConfigGetShort(const char *name, short *value);
|
||||
virtual StatusCode ConfigSetShort(const char *name, const short value);
|
||||
virtual StatusCode ConfigGetLong(const char *name, long *value);
|
||||
virtual StatusCode ConfigSetLong(const char *name, const long value);
|
||||
virtual StatusCode ConfigGetLLong(const char *name, long long *value);
|
||||
virtual StatusCode ConfigSetLLong(const char *name, const long long value);
|
||||
virtual StatusCode ConfigGetChar(const char *name, char *value);
|
||||
virtual StatusCode ConfigSetChar(const char *name, const char value);
|
||||
virtual StatusCode ConfigGetBool(const char *name, bool *value);
|
||||
virtual StatusCode ConfigSetBool(const char *name, const bool value);
|
||||
virtual StatusCode ConfigGetFloat(const char *name, float *value);
|
||||
virtual StatusCode ConfigSetFloat(const char *name, const float value);
|
||||
virtual StatusCode ConfigGetDouble(const char *name, double *value);
|
||||
virtual StatusCode ConfigSetDouble(const char *name, const double value);
|
||||
virtual StatusCode ConfigGetString(const char *name, const char **value);
|
||||
virtual StatusCode ConfigSetString(const char *name, const char *value);
|
||||
};
|
||||
typedef struct i_config_base_header
|
||||
{
|
||||
const char *mCheckName;
|
||||
} IConfigBaseHeader;
|
||||
typedef struct config_base
|
||||
{
|
||||
IConfigBaseHeader mHeader;
|
||||
std::shared_ptr<IConfigBase> mIConfigBase;
|
||||
} ConfigBase;
|
||||
const char *GetConfigBaseModuleName(void);
|
||||
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName);
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user