删除文件 utils/ConfigBase/src

This commit is contained in:
张耀 2024-07-27 09:12:31 +00:00 committed by Gitee
parent 706f234728
commit e0e30696cb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 0 additions and 845 deletions

View File

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

View File

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

View File

@ -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

View File

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

View File

@ -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 <libconfig.h>
#include <string>
class ConfigBaseImpl : public IConfigBase
{
public:
ConfigBaseImpl(const std::string &fileName);
virtual ~ConfigBaseImpl() = default;
bool OpenConfigFile(void) override;
void CloseConfigFile(void) override;
StatusCode ConfigSaveFile(void) override;
StatusCode ConfigGetInt(const char *name, int *value) override;
StatusCode ConfigSetInt(const char *name, const int value) override;
StatusCode ConfigGetShort(const char *name, short *value) override;
StatusCode ConfigSetShort(const char *name, const short value) override;
StatusCode ConfigGetLong(const char *name, long *value) override;
StatusCode ConfigSetLong(const char *name, const long value) override;
StatusCode ConfigGetLLong(const char *name, long long *value) override;
StatusCode ConfigSetLLong(const char *name, const long long value) override;
StatusCode ConfigGetChar(const char *name, char *value) override;
StatusCode ConfigSetChar(const char *name, const char value) override;
StatusCode ConfigGetBool(const char *name, bool *value) override;
StatusCode ConfigSetBool(const char *name, const bool value) override;
StatusCode ConfigGetFloat(const char *name, float *value) override;
StatusCode ConfigSetFloat(const char *name, const float value) override;
StatusCode ConfigGetDouble(const char *name, double *value) override;
StatusCode ConfigSetDouble(const char *name, const double value) override;
StatusCode ConfigGetString(const char *name, const char **value) override;
StatusCode ConfigSetString(const char *name, const char *value) override;
private:
const std::string mFileName;
config_t mCfg;
};
#endif

View File

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

View File

@ -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 <memory>
class IConfigBase
{
public:
IConfigBase() = default;
virtual ~IConfigBase() = default;
virtual bool OpenConfigFile(void);
virtual void CloseConfigFile(void);
virtual StatusCode ConfigSaveFile(void);
virtual StatusCode ConfigGetInt(const char *name, int *value);
virtual StatusCode ConfigSetInt(const char *name, const int value);
virtual StatusCode ConfigGetShort(const char *name, short *value);
virtual StatusCode ConfigSetShort(const char *name, const short value);
virtual StatusCode ConfigGetLong(const char *name, long *value);
virtual StatusCode ConfigSetLong(const char *name, const long value);
virtual StatusCode ConfigGetLLong(const char *name, long long *value);
virtual StatusCode ConfigSetLLong(const char *name, const long long value);
virtual StatusCode ConfigGetChar(const char *name, char *value);
virtual StatusCode ConfigSetChar(const char *name, const char value);
virtual StatusCode ConfigGetBool(const char *name, bool *value);
virtual StatusCode ConfigSetBool(const char *name, const bool value);
virtual StatusCode ConfigGetFloat(const char *name, float *value);
virtual StatusCode ConfigSetFloat(const char *name, const float value);
virtual StatusCode ConfigGetDouble(const char *name, double *value);
virtual StatusCode ConfigSetDouble(const char *name, const double value);
virtual StatusCode ConfigGetString(const char *name, const char **value);
virtual StatusCode ConfigSetString(const char *name, const char *value);
};
typedef struct i_config_base_header
{
const char *mCheckName;
} IConfigBaseHeader;
typedef struct config_base
{
IConfigBaseHeader mHeader;
std::shared_ptr<IConfigBase> mIConfigBase;
} ConfigBase;
const char *GetConfigBaseModuleName(void);
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName);
#endif