hunting/utils/StatusCode/src/StatusCode.c
2024-02-16 04:31:40 -08:00

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