hunting/utils/SharedData/src/SharedDataCode.c
2023-11-03 09:40:08 -07:00

64 lines
2.3 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 "SharedDataCode.h"
#include "ILog.h"
#include <string.h>
static const char *StatusCodeString[SHARED_DATA_CODE_END - STATUS_CODE_END + 1] = {
"SHARED_DATA_CODE_INIT_FAILED",
"SHARED_DATA_CODE_WRONG_PEER_PARAMETERS",
"SHARED_DATA_CODE_END"};
static const char *_PrintStringCode_(const StatusCode this)
{
if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= SHARED_DATA_CODE_END)
{
LogInfo("Status code = [ %s ]\n", StatusCodeString[this.mStatusCode - STATUS_CODE_END]);
return StatusCodeString[this.mStatusCode - STATUS_CODE_END];
}
LogError("Status code = [ %s ]\n", StatusCodeString[SHARED_DATA_CODE_END - STATUS_CODE_END]);
return StatusCodeString[SHARED_DATA_CODE_END - STATUS_CODE_END];
}
static const bool CodeEqual(const StatusCode code, const char *value)
{
if (STATUS_CODE_END >= code.mStatusCode || code.mStatusCode >= SHARED_DATA_CODE_END)
{
return false;
}
if (strlen(value) != strlen(StatusCodeString[code.mStatusCode - STATUS_CODE_END]))
{
return false;
}
if (memcmp(value, StatusCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0)
{
return true;
}
return false;
}
static StatusCode NewStatusCode(const long int code)
{
StatusCode result = {
_PrintStringCode_,
CodeEqual,
code};
return result;
}
const StatusCode CreateSharedDataCode(const long int code)
{
if (STATUS_CODE_END <= code && code <= SHARED_DATA_CODE_END)
{
return NewStatusCode(code);
}
LogError("undefined code.\n");
return NewStatusCode(SHARED_DATA_CODE_END);
}