(FEA) More macros for Status

This commit is contained in:
William
2024-06-07 17:17:32 +08:00
parent 66c1d57188
commit 8775d75de8
14 changed files with 415 additions and 525 deletions

View File

@@ -63,6 +63,16 @@ typedef struct _Status {
struct _Status *prev;
} Status;
# define DEFSTATUS(i, v, d, c, p)\
static const Status i = {\
.identity = nameof(i),\
.value = v,\
.description = d,\
.characteristic = c,\
.loc = __GLOBAL__,\
.prev = (Status *)p\
}
/*
{value "description" characteristic prev}
@@ -123,7 +133,7 @@ typedef struct {
char *initiator;
time_t time;
ReportSendingPriority priority;
ReportSendingTaskStatus task_status;
ReportSendingTaskStatus taskprint_status;
FILE *dest; // The destination where the report is sending to.
} Report;
@@ -256,310 +266,139 @@ Status ReportSenderManager_RemoveTask(ReportSendingManager *inst,
// ---------------------ELEMENTARY-------------------------
/*
typedef struct _Status {
char *identity;
int value;
char *description;
int characteristic;
Location loc;
struct _Status *prev;
} Status;
*/
static const Status UnknownStatus = {
.identity = nameof(UnknownStatus),
.value = -1,
.description = "An unknown status.",
.characteristic = STATUS_UNKNOWN,
.loc = __GLOBAL__,
.prev = NULL
};
static const Status NormalStatus = {
.identity = nameof(NormalStatus),
.value = 0,
.description = "A normal status.",
.characteristic = STATUS_NORMAL,
.loc = __GLOBAL__,
.prev = NULL
};
static const Status ErrorStatus = {
.identity = nameof(ErrorStatus),
.value = 1,
.description = "An error status.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = NULL
};
DEFSTATUS(UnknownStatus, -1, "An unknown status.", STATUS_UNKNOWN, NULL);
DEFSTATUS(NormalStatus, 0, "A normal status.", STATUS_NORMAL, NULL);
DEFSTATUS(ErrorStatus, 1, "An error status.", STATUS_ERROR, NULL);
// ----------------------EXTENDED--------------------------
static const Status MemoryViolation = (Status){
.identity = nameof(MemoryViolation),
.value = 1,
.description = "Illegal access on certain memory address.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(MemoryViolation, 1,
"Illegal access on certain memory address.",
STATUS_ERROR, &ErrorStatus);
static const Status NullPointerAccounted = (Status){
.identity = nameof(NullPointerAccounted),
.value = 1,
.description = "An involving null pointer was not accepted.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&MemoryViolation
};
DEFSTATUS(NullPointerAccounted, 1,
"An involving null pointer was not accepted.",
STATUS_ERROR, &MemoryViolation);
static const Status InvalidObject = (Status){
.identity = nameof(InvalidObject),
.value = 1,
.description = "An invalid object was presented.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(InvalidObject, 1,
"An invalid object was presented.",
STATUS_ERROR, &ErrorStatus);
static const Status UnavailableObject = (Status){
.identity = nameof(UnavailableObject),
.value = 1,
.description = "An unavailable object was presented.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(UnavailableObject, 1,
"An unavailable object was presented.",
STATUS_ERROR, &ErrorStatus);
static const Status InstanceStillAlive = (Status){
.identity = nameof(InstanceStillAlive),
.value = 1,
.description = "Given instance was yet alive.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(InstanceStillAlive, 1,
"Given instance was yet alive.",
STATUS_ERROR, &ErrorStatus);
static const Status InstanceNotAlive = (Status){
.identity = nameof(InstanceNotAlive),
.value = 1,
.description = "Given instance for reallocation was not alive.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(InstanceNotAlive, 1,
"Given instance for reallocation was not alive.",
STATUS_ERROR, &ErrorStatus);
static const Status InvalidParameter = (Status){
.identity = nameof(InvalidParameter),
.value = 1,
.description = "An invalid parameter was presented.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&InvalidObject
};
DEFSTATUS(InvalidParameter, 1,
"An invalid parameter was presented.",
STATUS_ERROR, &InvalidObject);
static const Status InsufficientMemory = (Status){
.identity = nameof(InsufficientMemory),
.value = 1,
.description = "Not enough room for further memory allocations.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(InsufficientMemory, 1,
"Not enough room for further memory allocations.",
STATUS_ERROR, &ErrorStatus);
static const Status ArithmeticError = (Status){
.identity = nameof(ArithmeticError),
.value = 1,
.description = "An arithmetic error occurred.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(ArithmeticError, 1,
"An arithmetic error occurred.",
STATUS_ERROR, &ErrorStatus);
static const Status IntegerOverFlow = (Status){
.identity = nameof(IntegerOverFlow),
.value = 1,
.description = "An integer had overflowed.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ArithmeticError
};
DEFSTATUS(IntegerOverFlow, 1,
"An integer had overflowed.",
STATUS_ERROR, &ArithmeticError);
static const Status RuntimeError = (Status){
.identity = nameof(RuntimeError),
.value = 1,
.description = "A runtime error occurred.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(RuntimeError, 1,
"A runtime error occurred.",
STATUS_ERROR, &ErrorStatus);
static const Status ArrayLengthError = (Status){
.identity = nameof(ArrayLengthError),
.value = 1,
.description = "Given array length does not meet the requirement.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(ArrayLengthError, 1,
"Given array length does not meet the requirement.",
STATUS_ERROR, &ErrorStatus);
static const Status VariableFormatMismatch = (Status){
.identity = nameof(VariableFormatMismatch),
.value = 1,
.description = "Given format does not match with given subjects.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ErrorStatus
};
DEFSTATUS(VariableFormatMismatch, 1,
"Given format does not match with given subjects.",
STATUS_ERROR, &ErrorStatus);
DEFSTATUS(ImprecisionError, 1,
"Precision was not enough for handling the calculation.",
STATUS_ERROR, &RuntimeError);
static const Status ImprecisionError = (Status){
.identity = nameof(ImprecisionError),
.value = 1,
.description = "Precision was not enough for handling the calculation.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&RuntimeError
};
// ---------------------USER DEFINED-----------------------
static const Status UnavailableInstance = (Status){
.identity = nameof(UnavailableInstance),
.value = 1,
.description = "An unavailable instance was given for initialisation.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&NullPointerAccounted
};
DEFSTATUS(UnavailableInstance, 1,
"An unavailable instance was given for initialisation.",
STATUS_ERROR, &NullPointerAccounted);
static const Status RecreationOnInstanceStillAlive = (Status){
.identity = nameof(RecreationOnInstanceStillAlive),
.value = 1,
.description = "Given instance was still alive, yet, was sent for another "
"session of recreation.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&InstanceStillAlive\
};
DEFSTATUS(RecreationOnInstanceStillAlive, 1,
"Given instance was still alive, yet, was sent for another session of recreation.",
STATUS_ERROR, &InstanceStillAlive);
static const Status UnavailableParameter = (Status){
.identity = nameof(UnavailableParameter),
.value = 1,
.description = "An unavailable instance was given as a parameter.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&UnavailableInstance
};
DEFSTATUS(UnavailableParameter, 1,
"An unavailable instance was given as a parameter.",
STATUS_ERROR, &UnavailableInstance);
static const Status InvalidReportTask = (Status){
.identity = nameof(InvalidReportTask),
.value = 1,
.description = "An unavailable or illegal report task was given.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&InvalidParameter
};
DEFSTATUS(InvalidReportTask, 1,
"An unavailable or illegal report task was given.",
STATUS_ERROR, &InvalidParameter);
static const Status UnableToThrowError = (Status){
.identity = nameof(UnableToThrowError),
.value = 1,
.description = "Unable to report an exceptional situation.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&RuntimeError
};
DEFSTATUS(UnableToThrowError, 1,
"Unable to report an exceptional situation.",
STATUS_ERROR, &RuntimeError);
static const Status ReadWriteError = (Status){
.identity = nameof(ReadWriteError),
.value = 1,
.description = "Error occurred during IO session.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&RuntimeError
};
DEFSTATUS(ReadWriteError, 1,
"Error occurred during IO session.",
STATUS_ERROR, &RuntimeError);
static const Status FileNotFound = (Status){
.identity = nameof(FileNotFound),
.value = 1,
.description = "Target file was unavailable and unable to find.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ReadWriteError
};
DEFSTATUS(FileNotFound, 1,
"Target file was unavailable and unable to find.",
STATUS_ERROR, &ReadWriteError);
static const Status InvalidFileName = (Status){
.identity = nameof(InvalidFileName),
.value = 1,
.description = "Given file name was invalid.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ReadWriteError
};
DEFSTATUS(InvalidFileName, 1,
"Given file name was invalid.",
STATUS_ERROR, &ReadWriteError);
static const Status UnavailableFileName = (Status){
.identity = nameof(UnavailableFileName),
.value = 1,
.description = "Given file name was unavailable",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&UnavailableObject
};
DEFSTATUS(UnavailableFileName, 1,
"Given file name was unavailable.",
STATUS_ERROR, &UnavailableObject);
static const Status ReportThrown = (Status){
.identity = nameof(ReportThrown),
.value = 1,
.description = "This function has thrown a report, "
"following instructions aborted.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&RuntimeError\
};
DEFSTATUS(UnavailableFileAccessMode, 1,
"Given file accessing mode was unavailable.",
STATUS_ERROR, &UnavailableObject);
static const Status ReportMessageTooLong = (Status){
.identity = nameof(ReportMessageTooLong),
.value = 1,
.description = "Given message is too long.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ArrayLengthError
};
DEFSTATUS(InsufficientAccessPermission, 1,
"Given permission does not suffice to access.",
STATUS_ERROR, &ReadWriteError);
static const Status MaximumLengthExceeded = (Status){
.identity = nameof(MaximumLengthExceeded),
.value = 1,
.description = "Buffer was too long.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&ArrayLengthError
};
DEFSTATUS(ReportThrown, 1,
"This function has thrown a report, following instructions aborted.",
STATUS_ERROR, &RuntimeError);
static const Status MaximumLiteralisationLengthExceeded = (Status){
.identity = nameof(MaximumLiteralisationLengthExceeded),
.value = 1,
.description = "Literalisation was too long.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&MaximumLengthExceeded
};
DEFSTATUS(ReportMessageTooLong, 1,
"Given message is too long.",
STATUS_ERROR, &ArrayLengthError);
static const Status UnavailableBuffer = (Status){
.identity = nameof(UnavailableBuffer),
.value = 1,
.description = "Given buffer was unavailable.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&UnavailableInstance
};
DEFSTATUS(MaximumLengthExceeded, 1,
"Maximum length had exceeded",
STATUS_ERROR, &ArrayLengthError);
DEFSTATUS(MaximumLiteralisationLengthExceeded, 1,
"Literalisation was too long",
STATUS_ERROR, &MaximumLengthExceeded);
DEFSTATUS(UnavailableBuffer, 1,
"Given buffer was unavailable.",
STATUS_ERROR, &UnavailableInstance);
DEFSTATUS(InvalidLiteralisingBuffer, 1,
"Given buffer does not have a good integrity on its length.",
STATUS_ERROR, &InvalidObject);
static const Status InvalidLiteralisingBuffer = (Status){
.identity = nameof(InvalidLiteralisingBuffer),
.value = 1,
.description = "Given buffer does not have a good integrity on its length.",
.characteristic = STATUS_ERROR,
.loc = __GLOBAL__,
.prev = (Status *)&InvalidObject
};
// ========================================================

View File

@@ -1,39 +1,33 @@
#include <Compound/common.h>
#include <Compound/status.h>
#include <Compound/utils.h>
/*
typedef struct {
char *file;
int line;
char *func;
} Location;
*/
Status Location_Literalise(Location *inst, char *buff)
{
fails(inst, UnavailableInstance);
fails(buff, UnavailableBuffer);
fails(inst, apply(UnavailableInstance));
fails(buff, apply(UnavailableBuffer));
/* Literalise line. */
char line_buff[LITERALISATION_LENGTH_MAXIMUM];
Utils_LiteraliseInteger(inst->line, line_buff);
/* Concatenate every buff. */
const long total_len = strlen(inst->file) + strlen(line_buff)
+ strlen(inst->func)
const long total_len = strlen(strnil(inst->file)) + strlen(strnil(line_buff))
+ strlen(strnil(inst->func))
+ LOCATION_LITERALISE_FORMAT_LENGTH;
state(total_len > LITERALISATION_LENGTH_MAXIMUM,
MaximumLiteralisationLengthExceeded);
apply(MaximumLiteralisationLengthExceeded));
/* Copy and assign. */
return (Status) {
.value = !sprintf(buff, LOCATION_LITERALISE_FORMAT,
inst->file, inst->line, inst->func),
.description = NormalStatus.description,
.characteristic = NormalStatus.characteristic,
.loc = __HERE__,
.prev = NormalStatus.prev
};
// return value(UnknownStatus,
// !sprintf(buff, LOCATION_LITERALISE_FORMAT,
// inst->file, inst->line, inst->func));
const int written =
sprintf(buff, LOCATION_LITERALISE_FORMAT,inst->file,inst->line,inst->func);
// written in "value" is absolutely ZERO.
state(!written, apply(value(UnknownStatus, written)));
return apply(NormalStatus);
}
bool Location_Equal(Location *lc1, Location *lc2)
@@ -62,24 +56,36 @@ bool Status_Equal(Status *stat1, Status *stat2)
Status Status_Literalise(Status *inst, char *buff)
{
/* Skip unavailable instance and invalid parameter. */
fails(inst, UnavailableInstance);
fails(buff, UnavailableBuffer);
fails(inst, apply(UnavailableInstance));
fails(buff, apply(UnavailableBuffer));
/* Literalise loc. */
char loc_buff[LITERALISATION_LENGTH_MAXIMUM];
notok(Location_Literalise(&inst->loc, loc_buff), {
return error(_, "Failed on literalising the \"loc\" of a status.");
return _;
});
/* Styling output. */
const char *fmt;
if (inst->characteristic == STATUS_ERROR) {
// TODO(william): Replace following line with coloured-term-output function.
fmt = "\e[38;5;9m\e[4m\e[1m"STATUS_LITERALISE_FORMAT"\e[0m";
} else if (inst->characteristic == STATUS_UNKNOWN) {
// TODO(william): Replace following line with coloured-term-output function.
fmt = "\e[38;5;11m\e[4m"STATUS_LITERALISE_FORMAT"\e[0m";
} else {
// TODO(william): Replace following line with coloured-term-output function.
fmt = "\e[38;5;10m\e[2m"STATUS_LITERALISE_FORMAT"\e[0m";
}
/* Concatenate every buffer. */
state(!sprintf(buff, STATUS_LITERALISE_FORMAT,
state(!sprintf(buff, fmt,
inst->identity, inst->description,
(!inst->prev ? "(null)" : (inst->prev->identity)),
inst->value, inst->characteristic, loc_buff),
error(RuntimeError, "Returned 0 byte written on concatenating buffers "
"during literalisation of a status using \"sprintf\"."));
return NormalStatus;
apply(error(ReadWriteError, "No bytes were written into buffer.")));
return apply(NormalStatus);
}
bool StatusUtils_HasPrev(Status stat)
@@ -107,7 +113,7 @@ void StatusUtils_Dump(Status *inst, Status *store, int idx)
{
/* Skip when either stat or stat.prev is unavailable, or, idx is invalid. */
solve((!store || !StatusUtils_HasPrev(*inst) || idx < 0), return;);
svoid((!store || !StatusUtils_HasPrev(*inst) || idx < 0));
store[idx] = *inst;
@@ -131,10 +137,10 @@ Status Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator,
int priority)
{
/* Skip unavailable parameters. */
fails(inst, UnavailableInstance);
fails(stat, error(InvalidParameter, "Given stat was null."));
fails(initiator, error(InvalidParameter, "Given initiator was null."));
state(priority < 0, error(InvalidParameter, "Given priority was negative."));
fails(inst, apply(UnavailableInstance));
fails(stat, apply(error(InvalidParameter, "Given stat was null.")));
fails(initiator, apply(error(InvalidParameter, "Given initiator was null.")));
state(priority < 0, apply(error(InvalidParameter, "Given priority was negative.")));
/* Copy and assign. */
inst->status = *stat;
@@ -142,25 +148,26 @@ Status Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator,
(void)strcpy(inst->initiator, initiator);
inst->time = time(NULL);
inst->priority = priority;
inst->task_status = REPORT_SENDING_TASK_STATUS_PENDING;
inst->taskprint_status = REPORT_SENDING_TASK_STATUS_PENDING;
inst->dest = (dest == NULL ? stdout : dest);
return NormalStatus;
return apply(NormalStatus);
}
Status Report_CopyOf(Report *inst, Report *other)
{
fails(inst, UnavailableInstance);
fails(other, error(InvalidParameter, "Given report is unavailable."));
fails(inst, apply(UnavailableInstance));
fails(other, apply(error(InvalidParameter, "Given report is unavailable.")));
// Status status;
// char *initiator;
// time_t time;
// ReportSendingPriority priority;
// ReportSendingTaskStatus task_status;
// ReportSendingTaskStatus taskprint_status;
// FILE *dest;
inst->status = other->status;
return apply(NormalStatus);
}
void Report_Delete(Report *inst)
@@ -172,15 +179,15 @@ void Report_Delete(Report *inst)
inst->dest = NULL;
inst->priority = 0;
inst->status = (Status){};
inst->task_status = REPORT_SENDING_TASK_STATUS_NOTFOUND;
inst->taskprint_status = REPORT_SENDING_TASK_STATUS_NOTFOUND;
inst->time = 0;
inst = NULL;
}
Status Report_Literalise(Report *inst, char *buff)
{
fails(inst, UnavailableInstance);
fails(buff, UnavailableBuffer);
fails(inst, apply(UnavailableInstance));
fails(buff, apply(UnavailableBuffer));
// state(strlen(buff) != LITERALISATION_LENGTH_MAXIMUM,
// InvalidLiteralisingBuffer);
@@ -208,9 +215,9 @@ Status Report_Literalise(Report *inst, char *buff)
idx += Utils_LiteraliseInteger(inst->priority, priority_literalising);
/** fin **/
/** Task_status literalisation. **/
char task_status_literalising[LITERALISATION_LENGTH_MAXIMUM];
idx += Utils_LiteraliseInteger(inst->task_status, task_status_literalising);
/** Taskprint_status literalisation. **/
char taskprint_status_literalising[LITERALISATION_LENGTH_MAXIMUM];
idx += Utils_LiteraliseInteger(inst->taskprint_status, taskprint_status_literalising);
/** fin **/
/** Dest literalisation. **/
@@ -226,18 +233,18 @@ Status Report_Literalise(Report *inst, char *buff)
strcpy(report_literalising, status_literalising);
strcpy(report_literalising, time_literalising);
strcpy(report_literalising, priority_literalising);
strcpy(report_literalising, task_status_literalising);
strcpy(report_literalising, taskprint_status_literalising);
strcpy(report_literalising, dest_literalising);
strcpy(buff, report_literalising);
/* fin */
return NormalStatus;
return apply(NormalStatus);
}
Status ReportSender_Create(ReportSender *inst, Report *report)
{
fails(inst, UnavailableInstance);
fails(inst, apply(UnavailableInstance));
fails(report, error(UnavailableParameter, "Given report was unavailable."));
thrd_create(&inst->thread, &HANDLER, report);
@@ -248,7 +255,7 @@ Status ReportSender_Create(ReportSender *inst, Report *report)
inst->result = REPORT_SENDER_RESULT_PENDING;
inst->successful = false;
return NormalStatus;
return apply(NormalStatus);
}
Status ReportSender_Send(ReportSender *inst, ReportSendingTask task)
@@ -264,12 +271,12 @@ Status ReportSender_Send(ReportSender *inst, ReportSendingTask task)
// // TODO(william): HERE, Report_Literalise
// /* Write/Send data. */
// inst->report->task_status = REPORT_SENDING_TASK_STATUS_PROCEEDING;
// inst->report->taskprint_status = REPORT_SENDING_TASK_STATUS_PROCEEDING;
// if (!fprintf(dest, buff)) {
// }
// /* Sent successfully! Mark down properties. */
return NormalStatus;
return apply(NormalStatus);
}
// bool arguestarter_equal(ArgueStarter *inst1, ArgueStarter *inst2)