(MOD) Refined Var, Array, CatlogMsg, CatlogSender, Status

This is the very first version that pass the compiler.  Though it has lots things were made for testing, such as commenting problematic source code to avoid irrelevant errors.  For test.c, everything is working fine.  Congrats!
This commit is contained in:
William
2024-05-20 04:47:39 +08:00
parent e73f3af436
commit e2f8dceda7
23 changed files with 656 additions and 393 deletions

View File

@@ -42,7 +42,7 @@ DECLARATION:
*PLEASE NOTE, AN EMPTY DESCRIPTION ALONG WITH A NULL DESCRIPTION IS
REGARDED AS "MEANINGLESS ONE" WHICH DOES NOT DOING ANYTHING EFFECTIVE FOR
ANYONE. DESCRIPTIONS CAN BE VALIDATED WITH FUNCTION `status_isvalid`.
ANYONE. DESCRIPTIONS CAN BE VALIDATED WITH FUNCTION `StatusUtils_IsValid`.
iii. characteristic: int
@@ -60,10 +60,11 @@ DECLARATION:
IN SOME CASES, THIS MEMBER COULD POINT AT IT SELF, MEANING IT COULD MAKE
A RECURSIVE SITUATION WHERE IT REQUIRE EXTRA EXAMINATIONS BEFORE GOING TO
THE ADDRESS IT POINTS AT. THESE PROBLEMS CAN BE AVOIDED WITH FUNCTION
`status_recursive` CALLED AHEAD.
`StatusUtils_IsRecursive` CALLED AHEAD.
*GO SEE status_dump FOR DETAILS ABOUT DUMPING CALLING STACKS.
*GO SEE status_recursive FOR DETERRING RECURSIVE "PREV".
*GO SEE StatusUtils_Dump
StatusUtils_Dump FOR DETAILS ABOUT DUMPING CALLING STACKS.
*GO SEE StatusUtils_IsRecursive FOR DETERRING RECURSIVE "PREV".
SYNONYM:
Status funcname1(void *param);

View File

@@ -8,6 +8,7 @@
# include <threads.h>
# include <time.h>
# include <stdio.h>
# include <math.h>
# include <Compound/common.h>
# include <Compound/utils.h>
@@ -20,7 +21,6 @@ typedef enum {
STATUS_ERROR = 1
} StatusCharacteristics;
typedef enum {
/* Settlement. */
ARGUE_RESULT_FINALISED = 0b01,
@@ -100,17 +100,17 @@ typedef enum {
/* "Report" recollects essential informations, included but not limited to
Status and others for making an report for debugging and such. */
typedef struct {
Status stat;
char *originator;
Status status;
char *initiator;
time_t time;
ReportSendingPriority priority;
ReportSendingTaskStatus status;
ReportSendingTaskStatus task_status;
FILE *dest; // The destination where the report is sending to.
} Report;
/*
TIME [PRIORITY] STATUSNAME (ORIGINATOR): STATUS.DESCRIPTION
DATETIME [PRIORITY] STATUSNAME (ORIGINATOR): STATUS.DESCRIPTION
at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC
at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC
at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC
@@ -126,23 +126,23 @@ Fri 10 May 03:02:37 CST 2024 [EXCEPTIONAL] InvalidParameter (Nullity): Given buf
*/
# define REPORT_LITERALISE_HEADER_FORMAT "%ld [%s] %s (%s): %s"
# define REPORT_LITERALISE_HEADER_FORMAT "%s [%s] %s (%s): %s"
# define REPORT_LITERALISE_CHAINS_FORMAT " at %s:%d, %s"
# define REPORT_LITERALISE_CHAINS_EXCLAIM_FORMAT "!!!!at %s:%d, %s"
# define REPORT_LITERALISE_HEADER_FORMAT_LENGTH(buff, PRIORITY, STATUSNAME, \
ORIGINATOR, DESCRIPTION) \
{ \
const time_t now = time(NULL); \
(void)strflen(buff, 28, DATETIME_FORMAT, localtime(&now)); \
*length = strlen(buff); \
}
// # define REPORT_LITERALISE_HEADER_FORMAT_LENGTH(buff, PRIORITY, STATUSNAME, \
// ORIGINATOR, DESCRIPTION) \
// { \
// const time_t now = time(NULL); \
// (void)strflen(buff, 28, DATETIME_FORMAT, localtime(&now)); \
// *length = strlen(buff); \
// }
# define REPORT_LITERALISE_CHAINS_FORMAT_LENGTH(FILEPATH, LINE, FUNCNAME) \
(strlen(FILEPATH) + utils_calc_digits(LINE) + \
strlen(FUNCNAME) + 10) // Does not count '\0'
# define REPORT_LITERALISE_CHAINS_EXCLAIM_FORMAT_LENGTH \
REPORT_LITERALISE_CHAINS_FORMAT_LENGTH
// # define REPORT_LITERALISE_CHAINS_FORMAT_LENGTH(FILEPATH, LINE, FUNCNAME) \
// (strlen(FILEPATH) + utils_calc_digits(LINE) + \
// strlen(FUNCNAME) + 10) // Does not count '\0'
// # define REPORT_LITERALISE_CHAINS_EXCLAIM_FORMAT_LENGTH \
// REPORT_LITERALISE_CHAINS_FORMAT_LENGTH
typedef enum {
REPORT_SENDER_RESULT_FINISHED,
@@ -200,21 +200,21 @@ typedef struct {
# define STATUS_BUFFER_MAXIMUM_LENGTH UINT32_MAX
bool Location_Equal(Location lc1, Location lc2);
bool Status_Equal(Status stat1, Status stat2);
bool Location_Equals(Location lc1, Location lc2);
Status Status_Literalise(Status *inst, char *buff);
void StatusUtils_Dump(Status *stat, Status *statbuff, int idx);
bool StatusUtils_HasPrev(Status *stat);
bool StatusUtils_IsOkay(Status stat);
bool StatusUtils_IsValid(Status stat);
bool StatusUtils_IsRecursive(Status stat);
int StatusUtils_Depth(Status *stat);
bool Status_Equals(Status stat1, Status stat2);
void StatusUtils_Dump(Status *inst, Status *store, int idx);
bool StatusUtils_HasPrev(Status *inst);
bool StatusUtils_IsOkay(Status inst);
bool StatusUtils_IsValid(Status inst);
bool StatusUtils_IsRecursive(Status inst);
int StatusUtils_Depth(Status *inst);
Status
Report_Create(Report *inst, Status *stat, FILE *dest, char *originator,
Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator,
int priority);
bool
Report_Equal(Report repo1, Report repo2);
Report_Equals(Report repo1, Report repo2);
Status
Report_Literalise(Report *inst, char *buff);
@@ -223,8 +223,8 @@ Status
ReportSender_Create(ReportSender *inst, Report *report);
Status
ReportSender_Send(ReportSender *inst, ReportSendingTask task);
ReportSendingTaskStatus
ReportSender_GetStatus(ReportSender *inst);
// ReportSendingTaskStatus
// ReportSender_GetStatus(ReportSender *inst);
ReportSendingTaskID
ReportSenderManager_AppendTask(ReportSendingManager *inst,
@@ -286,10 +286,22 @@ static Status NullPointerAccounted = {
.prev = &MemoryViolation
};
static Status InvalidObject = {
.description = "An invalid object was presented.",
.characteristic = STATUS_ERROR,
.prev = &ErrorStatus
};
static Status UnavailableObject = {
.description = "An unavailable object was presented.",
.characteristic = STATUS_ERROR,
.prev = &ErrorStatus
};
static Status InvalidParameter = {
.description = "An invalid parameter was presented.",
.characteristic = STATUS_ERROR,
.prev = &ErrorStatus
.prev = &InvalidObject
};
static Status InsufficientMemory = {
@@ -328,7 +340,7 @@ static Status ImprecisionError = {
.prev = &RuntimeError
};
// ---------------USER DEFINED | RUNTIME-------------------
// ---------------------USER DEFINED-----------------------
static Status UnavailableInstance = {
.description = "An unavailable instance was given for initialisation.",
@@ -372,6 +384,12 @@ static Status InvalidFileName = {
.prev = &ReadWriteError
};
static Status UnavailableFileName = {
.description = "Given file name was unavailable",
.characteristic = STATUS_ERROR,
.prev = &UnavailableObject
};
static Status ReportThrown = {
.description = "This function has thrown a report, "
"following instructions aborted.",
@@ -379,12 +397,36 @@ static Status ReportThrown = {
.prev = &RuntimeError
};
static Status ReportMessageLengthTooLong = {
static Status ReportMessageTooLong = {
.description = "Given message is too long.",
.characteristic = STATUS_ERROR,
.prev = &ArrayLengthError
};
static Status MaximumLengthExceeded = {
.description = "Buffer was too long.",
.characteristic = STATUS_ERROR,
.prev = &ArrayLengthError
};
static Status MaximumLiteralisationLengthExceeded = {
.description = "Literalisation was too long.",
.characteristic = STATUS_ERROR,
.prev = &MaximumLengthExceeded
};
static Status UnavailableBuffer = {
.description = "Given buffer was unavailable.",
.characteristic = STATUS_ERROR,
.prev = &UnavailableInstance
};
static Status InvalidLiteralisingBuffer = {
.description = "Given buffer does not have a good integrity on its length.",
.characteristic = STATUS_ERROR,
.prev = &InvalidObject
};
// ========================================================
/* Throw the report created with $e if $e is abnormal, commented with $c. */
@@ -417,25 +459,6 @@ static Status ReportMessageLengthTooLong = {
ReportSendingTaskID __throw(Report report, Location loc);
Report catch(ReportSendingTaskID taskid);
static int HANDLER(void *report)
{
/* Throw UnableToThrowError when param is unavailable. */
if (report == NULL) {
/* Create report on this. */
Report e;
Report_Create(
&e,
&error(UnableToThrowError, "Cannot perform throwing. Aborted."),
stderr, nameof(DEFAULT_ARGUE_STARTER),
REPORT_SENDING_PRIORITY_FATAL);
/* Perform throwing. */
(void)throw(e); // Throw the report alone.
return 1;
}
(void)throw(*(Report *)report); // Lonely _throw, no catch will company.
return 0;
}
int HANDLER(void *report);
#endif /* COMPOUND_STATUS_H */

View File

@@ -1,42 +1,15 @@
#include <Compound/common.h>
#include <Compound/status.h>
#include <Compound/var.h>
#include <Compound/utils.h>
bool status_issuelocation_equal(Location lc1, Location lc2) {
bool Location_Equal(Location lc1, Location lc2)
{
return ((!strcmp(lc1.file, lc2.file)) && (lc1.line == lc2.line) &&
(!strcmp(lc1.func, lc2.func)));
}
bool status_hasprev(Status stat) {
/* Skip when stat is unavailable for accessing. */
state(Status_Equal(stat, (Status){}), false);
return (stat.prev != NULL);
}
bool status_isokay(Status stat) { return (!stat.characteristic); }
bool status_isvalid(Status stat) {
return (!strcmp(stat.description, "") && stat.characteristic >= 0 &&
stat.prev != NULL);
}
bool status_recursive(Status stat) {
return (stat.prev != NULL && stat.prev == &stat);
}
void status_dump(Status *inst, Status *statbuff, int idx) {
/* Skip when either stat or stat.prev is unavailable, or, idx is invalid. */
solve((statbuff == NULL || !status_hasprev(*inst) || idx < 0), return;);
statbuff[idx] = *inst;
(void)printf("status_dump: Index %d has assigned with %p\n", idx, &inst);
status_dump(inst->prev, statbuff, ++idx);
}
bool Status_Equal(Status stat1, Status stat2) {
bool Status_Equals(Status stat1, Status stat2)
{
/* Skip when both stat1 and stat2 are empty. */
state((stat1.value == 0 && stat2.value == 0 && stat1.description == 0x0 &&
@@ -48,22 +21,88 @@ bool Status_Equal(Status stat1, Status stat2) {
return ((stat1.value == stat2.value) &&
(!strcmp(stat1.description, stat2.description)) &&
(stat1.characteristic == stat2.characteristic) &&
(Status_Equal(*stat1.prev, *stat2.prev)));
(Status_Equals(*stat1.prev, *stat2.prev)));
}
Status Status_Literalise(Status *inst, char *buff)
{
/* Skip unavailable or invalid parameters. */
fails(inst, UnavailableInstance);
fails(buff, UnavailableBuffer);
// state(strlen(buff) != LITERALISATION_LENGTH_MAXIMUM,
// InvalidLiteralisingBuffer);
/* Set up idx for counting final literalisation length to ensure the
string copying is index access safe. */
int idx = 0;
const int status_dump_buffer_len = StatusUtils_Depth(inst);
Status status_dump_buffer[status_dump_buffer_len];
/* Literalise every status that flattened on status_dump_buffer. */
for (register int i = 0; i < status_dump_buffer_len; i++) {
char status_literalising_buffer[LITERALISATION_LENGTH_MAXIMUM];
(void)Status_Literalise(&status_dump_buffer[i], status_literalising_buffer);
/* Append to buff. */
/* Prevent buffer-out-of-bound access. */
const int status_literalising_buffer_len = strlen(status_literalising_buffer);
if (idx + status_literalising_buffer_len >= LITERALISATION_LENGTH_MAXIMUM) {
buff = NULL;
return MaximumLiteralisationLengthExceeded;
}
idx += status_literalising_buffer_len;
(void)strcat(buff, status_literalising_buffer);
}
return NormalStatus;
}
int StatusUtils_Depth(Status *stat) {
bool StatusUtils_HasPrev(Status *stat)
{
/* Skip when stat is unavailable for accessing. */
state(Status_Equals(*stat, (Status){}), false);
return (stat->prev != NULL);
}
bool StatusUtils_IsOkay(Status stat)
{
return (!stat.characteristic);
}
bool StatusUtils_IsValid(Status stat)
{
return (!strcmp(stat.description, "") && stat.characteristic >= 0 &&
stat.prev != NULL);
}
bool StatusUtils_IsRecursive(Status stat)
{
return (stat.prev != NULL && stat.prev == &stat);
}
void StatusUtils_Dump(Status *inst, Status *store, int idx)
{
/* Skip when either stat or stat.prev is unavailable, or, idx is invalid. */
solve((store == NULL || !StatusUtils_HasPrev(inst) || idx < 0), return;);
store[idx] = *inst;
StatusUtils_Dump(inst->prev, store, ++idx);
}
int StatusUtils_Depth(Status *stat)
{
/* Skip unavailable stat. */
state((stat == NULL), -1);
Status *p = stat; // Include this layer of Status.
int cnt = 1;
while (p != NULL) {
if (status_recursive(*p) || !status_hasprev(*stat)) break;
if (StatusUtils_IsRecursive(*p) || !StatusUtils_HasPrev(stat)) break;
p = p->prev;
cnt += 1;
@@ -72,20 +111,21 @@ int StatusUtils_Depth(Status *stat) {
return cnt;
}
Status report_create(Report *inst, Status *stat, FILE *dest, char *originator,
int priority) {
Status Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator,
int priority)
{
/* Skip unavailable parameters. */
fails(inst, UnavailableInstance);
fails(stat, error(InvalidParameter, "Given originator was null."));
fails(originator, error(InvalidParameter, "Given originator was null."));
fails(stat, error(InvalidParameter, "Given initiator was null."));
fails(initiator, error(InvalidParameter, "Given initiator was null."));
state(priority < 0, error(InvalidParameter, "Given priority was negative."));
/* Copy and assign. */
inst->stat = *stat;
inst->originator = originator;
inst->status = *stat;
inst->initiator = initiator;
inst->time = time(NULL);
inst->priority = priority;
inst->status = REPORT_SENDING_TASK_STATUS_PENDING;
inst->task_status = REPORT_SENDING_TASK_STATUS_PENDING;
inst->dest = (dest == NULL ? stderr : dest);
return NormalStatus;
@@ -93,39 +133,64 @@ Status report_create(Report *inst, Status *stat, FILE *dest, char *originator,
Status Report_Literalise(Report *inst, char *buff)
{
/* Skip when inst or buff is unavailable. */
fails(inst, UnavailableInstance);
fails(buff, error(InvalidParameter, "Given buffer was unavailable."));
/* By calculating the depth of status, we can calculate the exact length of
literalisation of report. */
const int depth = StatusUtils_Depth(&inst->stat);
fails(buff, UnavailableBuffer);
// state(strlen(buff) != LITERALISATION_LENGTH_MAXIMUM,
// InvalidLiteralisingBuffer);
/* Dump all the status. */
Status stats[depth];
StatusUtils_Dump(&inst->stat, stats, 0);
/* Report literalisation. */
int idx = 0;
char report_literalising[LITERALISATION_LENGTH_MAXIMUM];
/* Literalise by iterating. */
int report_literalised_length = 0;
for (register int i = 0; i < depth; i++) {
/* Literalise individual status. */
char buff[strlen(stats[i].description) + 1 + ];
(void)Status_Literalise(&stats[i], buff); // Ignore the returning.
// Otherwise, if we throw the report once the returning status is abnormal,
// this function would be called again by the throw function.
/* Accumulate length of report literalisation. */
report_literalised_length += strlen(buff);
/** Status literalisation. **/
char status_literalising[LITERALISATION_LENGTH_MAXIMUM];
(void)Status_Literalise(&inst->status, status_literalising);
idx += strlen(status_literalising);
/** fin **/
/** Initiator literalisation. **/
idx += strlen(inst->initiator);
/** fin **/
/** Time literalisation. **/
char time_literalising[LITERALISATION_LENGTH_MAXIMUM];
idx += Utils_LiteraliseInteger(inst->time, time_literalising);
/** fin **/
/** Priority literalisation. **/
char priority_literalising[LITERALISATION_LENGTH_MAXIMUM];
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);
/** fin **/
/** Dest literalisation. **/
char dest_literalising[LITERALISATION_LENGTH_MAXIMUM];
idx += Utils_LiteraliseInteger((long long int)inst->dest, dest_literalising);
/** fin **/
if (idx >= LITERALISATION_LENGTH_MAXIMUM) {
buff = NULL;
return MaximumLiteralisationLengthExceeded;
}
strcpy(report_literalising, status_literalising);
strcpy(report_literalising, time_literalising);
strcpy(report_literalising, priority_literalising);
strcpy(report_literalising, task_status_literalising);
strcpy(report_literalising, dest_literalising);
/* Create report literalisation buffer according to
report_literalised_length. */
char report_literalised_buffer[report_literalised_length];
strcpy(buff, report_literalising);
/* fin */
/* */
return NormalStatus;
}
Status ReportSender_Create(ReportSender *inst, Report *report) {
Status ReportSender_Create(ReportSender *inst, Report *report)
{
fails(inst, UnavailableInstance);
fails(report, error(UnavailableParameter, "Given report was unavailable."));
@@ -138,23 +203,25 @@ Status ReportSender_Create(ReportSender *inst, Report *report) {
return NormalStatus;
}
Status ReportSender_Send(ReportSender *inst, ReportSendingTask task) {
/* Skip when inst or task is unavailable. */
fails(inst,
error(UnavailableInstance, "Report sender was given unavailable."));
fails(task, InvalidReportTask);
Status ReportSender_Send(ReportSender *inst, ReportSendingTask task)
{
// /* Skip when inst or task is unavailable. */
// fails(inst,
// error(UnavailableInstance, "Report sender was given unavailable."));
// fails(task, InvalidReportTask);
/* Assign for dest. */
const FILE *dest = (inst->report->dest == NULL ? stdout : inst->report->dest);
// char buff[];
// TODO(william): HERE, Report_Literalise
// /* Assign for dest. */
// const FILE *dest = (inst->report->dest == NULL ? stdout : inst->report->dest);
// // char buff[];
// // TODO(william): HERE, Report_Literalise
/* Write/Send data. */
inst->report->status = REPORT_SENDING_TASK_STATUS_PROCEEDING;
if (!fprintf(dest, buff)) {
}
// /* Write/Send data. */
// inst->report->task_status = REPORT_SENDING_TASK_STATUS_PROCEEDING;
// if (!fprintf(dest, buff)) {
// }
/* Sent successfully! Mark down properties. */
// /* Sent successfully! Mark down properties. */
return NormalStatus;
}
// bool arguestarter_equal(ArgueStarter *inst1, ArgueStarter *inst2)
@@ -166,40 +233,65 @@ Status ReportSender_Send(ReportSender *inst, ReportSendingTask task) {
// || (inst1->external_param == inst2->external_param);
// }
ReportSendingTaskID _throw(Report report, Location loc) {
// /* Create new a instance of ReportSender. */
ReportSendingTaskID _throw(Report report, Location loc)
{
// // /* Create new a instance of ReportSender. */
// // ReportSender sender;
// // ReportSender_Create(&sender, stderr);
// // /* Send message. */
// // /* Initialise sender's thread. */
// // thrd_t sending;
// // /* Skip on failing on creating thread. */
// // if (!thrd_create(&sending, starter, NULL)) {
// // /* Conclude the session of sender. */
// // report.status = REPORT_SENDING_TASK_STATUS_FINISHED,
// // report.result = (ARGUE_RESULT_FINALISED | ARGUE_RESULT_NEGATIVE);
// // sender.result = REPORT_SENDER_RESULT_FINISHED;
// // sender.successful = false;
// // return -1;
// // }
// // /* Perform sending. */
// // ReportSender_Send(&sender, NULL);
// /* Initialise sender. */
// ReportSender sender;
// ReportSender_Create(&sender, stderr);
// /* Return with -1 when initialisation failed. */
// state(!(StatusUtils_IsOkay(ReportSender_Create(&sender, &report))), -1);
// /* Inject location information. Could be more elegant, though. */
// sender.report->status.loc = loc;
// /* Send. */ /* Return -1 when failed on sending. */
// state(!StatusUtils_IsOkay(ReportSender_Send(&sender, HANDLER)), -1);
return 0;
}
// /* Send message. */
// /* Initialise sender's thread. */
// thrd_t sending;
// /* Skip on failing on creating thread. */
// if (!thrd_create(&sending, starter, NULL)) {
int HANDLER(void *report)
{
// /* Throw UnableToThrowError when param is unavailable. */
// if (report == NULL) {
// /* Create report on this. */
// Report e;
// Report_Create(
// &e,
// &error(UnableToThrowError, "Cannot perform throwing. Aborted."),
// stderr, nameof(DEFAULT_ARGUE_STARTER),
// REPORT_SENDING_PRIORITY_FATAL);
// /* Conclude the session of sender. */
// report.status = REPORT_SENDING_TASK_STATUS_FINISHED,
// report.result = (ARGUE_RESULT_FINALISED | ARGUE_RESULT_NEGATIVE);
// sender.result = REPORT_SENDER_RESULT_FINISHED;
// sender.successful = false;
// return -1;
// /* Perform throwing. */
// (void)throw(e); // Throw the report alone.
// return 1;
// }
// /* Perform sending. */
// ReportSender_Send(&sender, NULL);
/* Initialise sender. */
ReportSender sender;
/* Return with -1 when initialisation failed. */
state(!(status_isokay(ReportSender_Create(&sender, &report))), -1);
/* Inject location information. Could be more elegant, though. */
sender.report->stat.loc = loc;
/* Send. */ /* Return -1 when failed on sending. */
state(!status_isokay(ReportSender_Send(&sender, HANDLER)), -1);
// (void)throw(*(Report *)report); // Lonely throw, no catch will company.
// return 0;
return 0;
}