embedded-framework/utils/ConfigBase/src/ConfigBase.cpp
2024-05-23 22:42:00 +08:00

180 lines
6.5 KiB
C++

/*
* 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(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);
}