From 8775d75de8ab74978d614b180f68af7f52fdae2b Mon Sep 17 00:00:00 2001 From: William Date: Fri, 7 Jun 2024 17:17:32 +0800 Subject: [PATCH] (FEA) More macros for Status --- Array/src/array.c | 53 +++--- CMakeLists.txt | 4 +- MemMan/src/memman.c | 37 ++-- Stack/src/stack.c | 2 + Status/include/status.h | 383 ++++++++++++---------------------------- Status/src/status.c | 113 ++++++------ String/include/string.h | 2 +- String/src/string.c | 22 ++- Var/src/var.c | 33 ++-- catlog.c | 83 +++++---- catlog.h | 29 ++- common.h | 150 ++++++++-------- namescope.c | 26 ++- platform.h | 3 + 14 files changed, 415 insertions(+), 525 deletions(-) mode change 100755 => 100644 catlog.c mode change 100755 => 100644 catlog.h diff --git a/Array/src/array.c b/Array/src/array.c index b5a8197..5542f3e 100644 --- a/Array/src/array.c +++ b/Array/src/array.c @@ -4,9 +4,9 @@ Status Array_Create(Array *inst, int len, size_t size) { /* Skip unavailable inst and invalid param. */ - fails(inst, UnavailableInstance); - state((len < 0), InvalidArrayLength); - solve((!len), { inst->len = 0; inst->members = NULL; return NormalStatus; }) + fails(inst, apply(UnavailableInstance)); + state((len < 0), apply(InvalidArrayLength)); + solve((!len), { inst->len = 0; inst->members = NULL; return apply(NormalStatus); }) inst->len = len; inst->members = calloc(len, sizeof(Var)); @@ -40,22 +40,22 @@ Status Array_Create(Array *inst, int len, size_t size) /* Release array itself. */ free(inst->members); - return InsufficientMemory; + return apply(InsufficientMemory); } - return NormalStatus; + return apply(NormalStatus); } Status Array_CopyOf(Array *inst, Array *other) { // /* Skip unavailable inst and invalid param. */ - // fails(inst, UnavailableInstance); + // fails(inst, apply(UnavailableInstance)); // fails(other, error(InvalidParameter, "Given other was unavailable.")); // /* Assign value for len. */ // inst->len = other->len; - // if (inst->members == NULL) return NormalStatus; + // if (inst->members == NULL) return apply(NormalStatus); // match(RuntimeError, Array_Create(inst, other->len), "Failed on recreating " // "array."); @@ -64,7 +64,7 @@ Status Array_CopyOf(Array *inst, Array *other) // inst[i] = other[i]; // } - // return NormalStatus; + // return apply(NormalStatus); @@ -86,40 +86,41 @@ Status Array_CopyOf(Array *inst, Array *other) Status Array_Delete(Array *inst) { /* Skip unavailable inst and invalid param. */ - fails(inst, UnavailableInstance); - solve((inst->members == NULL), return NormalStatus); + fails(inst, apply(UnavailableInstance)); + solve((inst->members == NULL), return apply(NormalStatus)); inst->len = 0; free(inst->members); inst->members = NULL; - return NormalStatus; + return apply(NormalStatus); } Status Array_GetIdx(Array *inst, Var *store, int index) { /* Skip unavailable inst and invalid param. */ - fails(inst, UnavailableInstance); - fails(store, error(InvalidParameter, "Given reference to store was " - "unavailable.")); - state((index < 0 || index >= inst->len), ArrayIndexOutOfBound); + fails(inst, apply(UnavailableInstance)); + fails(store, + apply(error(InvalidParameter, "Given reference to store was unavailable."))); + state((index < 0 || index >= inst->len), apply(ArrayIndexOutOfBound)); *store = inst->members[index]; - return NormalStatus; + return apply(NormalStatus); } Status Array_SetIdx(Array *inst, Var *source, int index) { /* Skip unavailable inst and invalid param. */ - fails(inst, UnavailableInstance); - fails(source, error(InvalidParameter, "Given reference to source was " - "unavailable.")); - state((index < 0 || index >= inst->len), ArrayIndexOutOfBound); + fails(inst, apply(UnavailableInstance)); + fails(source, + apply( + error(InvalidParameter, "Given reference to source was unavailable."))); + state((index < 0 || index >= inst->len), apply(ArrayIndexOutOfBound)); inst->members[index] = *source; - return NormalStatus; + return apply(NormalStatus); } bool Array_Equals(Array *a, Array *b) @@ -141,14 +142,16 @@ bool Array_Equals(Array *a, Array *b) Status ArrayUtils_Fill(Array *inst, Var *elem, int off, int len) { - fails(inst, UnavailableInstance); - fails(elem, error(InvalidParameter, "Given reference to elem was unavailable.")); - state((off + len > inst->len) || (off < 0) || (len < 0), ArrayIndexOutOfBound); + fails(inst, apply(UnavailableInstance)); + fails(elem, + apply(error(InvalidParameter, "Given reference to elem was unavailable."))); + state((off + len > inst->len) || (off < 0) || (len < 0), + apply(ArrayIndexOutOfBound)); /* Copy elem into each specified members from inst with off and len. */ for (register int i = off; i < (off + len); i++) { inst->members[i] = *elem; } - return NormalStatus; + return apply(NormalStatus); } diff --git a/CMakeLists.txt b/CMakeLists.txt index b2fa4f8..da5935c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,10 @@ cmake_minimum_required (VERSION 3.5) project (Compound) +add_compile_options(-g -std=c99 -Wall -Wextra -Wformat) + set(SHARED_SOURCE + MemMan/src/memman.c Status/src/status.c Utils/src/utils.c Var/src/var.c @@ -12,7 +15,6 @@ set(LIBCOMPOUND_SOURCE ${SHARED_SOURCE}) add_library(compound SHARED ${LIBCOMPOUND_SOURCE}) -add_compile_options(-g -std=c99 -Wall -Wextra -Wformat) LINK_LIBRARIES(m) # add_executable(CompoundTest test.c diff --git a/MemMan/src/memman.c b/MemMan/src/memman.c index e872983..a299aca 100644 --- a/MemMan/src/memman.c +++ b/MemMan/src/memman.c @@ -2,7 +2,7 @@ Status Memory_Create(Memory *inst, size_t size) { - fails(inst, UnavailableInstance); + fails(inst, apply(UnavailableInstance)); *inst = (Memory) { .addr = NULL, @@ -11,57 +11,58 @@ Status Memory_Create(Memory *inst, size_t size) .alive = false }; - return NormalStatus; + return apply(NormalStatus); } Status Memory_Allocate(Memory *inst) { - fails(inst, UnavailableInstance); - state(inst->alive, InstanceStillAlive); + fails(inst, apply(UnavailableInstance)); + state(inst->alive, apply(InstanceStillAlive)); /* When failed on allocating. */ - state(!(inst->addr = malloc(inst->size)), InsufficientMemory); + state(!(inst->addr = malloc(inst->size)), apply(InsufficientMemory)); inst->alive = true; - return NormalStatus; + return apply(NormalStatus); } Status Memory_Reallocate(Memory *inst, size_t size) { - fails(inst, UnavailableBuffer); - state(!inst->alive, InstanceNotAlive); + fails(inst, apply(UnavailableBuffer)); + state(!inst->alive, apply(InstanceNotAlive)); /* When failed on reallocating. */ state(!(inst->addr = realloc(inst->addr, size)), - error(InsufficientMemory, "Unsuccessful reallocation was received.")) + apply(error(InsufficientMemory, "Unsuccessful reallocation was received."))); - return NormalStatus; + return apply(NormalStatus); } Status Memory_Release(Memory *inst) { - fails(inst, UnavailableInstance); - state(!inst->alive, error(InstanceNotAlive, "Cannot release a non-alive " - "instance.")); + fails(inst, apply(UnavailableInstance)); + state(!inst->alive, + apply(error(InstanceNotAlive, "Cannot release a non-alive instance."))); free(inst->addr); inst->alive = false; - return NormalStatus; + return apply(NormalStatus); } Status Memory_Delete(Memory *inst) { - fails(inst, UnavailableInstance); - state(inst->alive, error(InstanceStillAlive, "Cannot deinitialise a instance " - "still alive.")); + fails(inst, apply(UnavailableInstance)); + state(inst->alive, + apply( + error(InstanceStillAlive, "Cannot deinitialise a instance still alive."))); inst->addr = NULL; inst->priority = 0; inst->size = 0; inst = NULL; - return NormalStatus; + return apply(NormalStatus); } bool Memory_Equals(Memory *inst, Memory *other) diff --git a/Stack/src/stack.c b/Stack/src/stack.c index 3843a09..d4bd1ee 100644 --- a/Stack/src/stack.c +++ b/Stack/src/stack.c @@ -21,6 +21,8 @@ Status Stack_Create(Stack *inst, int len) // }; // } // } + + return apply(NormalStatus); } Status Stack_CopyOf(Stack *inst, Stack *other); Status Stack_Push(Stack *inst, Var *item); diff --git a/Status/include/status.h b/Status/include/status.h index da14a2d..76809bb 100644 --- a/Status/include/status.h +++ b/Status/include/status.h @@ -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 -}; // ======================================================== diff --git a/Status/src/status.c b/Status/src/status.c index 438cf34..df91541 100644 --- a/Status/src/status.c +++ b/Status/src/status.c @@ -1,39 +1,33 @@ -#include #include -#include -/* -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) diff --git a/String/include/string.h b/String/include/string.h index 9b64abc..6916c32 100644 --- a/String/include/string.h +++ b/String/include/string.h @@ -73,7 +73,7 @@ Status StringUtils_ToUnsignedComplexLongLongInteger(String *inst, unsigned _Comp Status StringUtils_ToAddress(String *inst, void **store); Status StringUtils_ToCharBuff(String *inst, char const *buff, int base); Status StringUtils_ToWideCharBuff(String *inst, wchar_t const *wbuff, int base); -Status StringUtils_Format(String *inst, const String *__restrict fmt, ...); +Status StringUtils_Format(String *inst, const String *restrict fmt, ...); Status StringUtils_Tokenise(String *inst, const String *delim, String *store); Status String_Encode(String *inst, StringEncoding encoding) throws(UnsupportedEncoding EncodingError DecodingError); diff --git a/String/src/string.c b/String/src/string.c index 2751cd8..18161ca 100644 --- a/String/src/string.c +++ b/String/src/string.c @@ -5,32 +5,42 @@ Status String_Create(String *inst, int len) /* Create an array has length len + 1, for termination character in string. */ ensure(Array_Create(inst, len + 1, sizeof(int)), "Failed to create a string."); - return NormalStatus; + return apply(NormalStatus); } Status String_CopyOf(String *inst, String *other) { - fails(inst, UnavailableInstance); + fails(inst, apply(UnavailableInstance)); + + return apply(NormalStatus); } Status String_Delete(String *inst) { - fails(inst, UnavailableInstance); + fails(inst, apply(UnavailableInstance)); + + return apply(NormalStatus); } Status String_GetAt(String *inst, Char *store, int index) { - fails(inst, UnavailableInstance); + fails(inst, apply(UnavailableInstance)); + + return apply(NormalStatus); } Status String_SetAt(String *inst, Char *source, int index) { - fails(inst, UnavailableInstance); + fails(inst, apply(UnavailableInstance)); + + return apply(NormalStatus); } Status String_Literalise(String *inst, String *store) { - fails(inst, UnavailableInstance); + fails(inst, apply(UnavailableInstance)); + + return apply(NormalStatus); } int StringUtils_Compare(String *a, String *b) diff --git a/Var/src/var.c b/Var/src/var.c index bceddb2..a4e9cda 100644 --- a/Var/src/var.c +++ b/Var/src/var.c @@ -2,22 +2,22 @@ Status Var_Create(Var *inst, size_t size) { - fails(inst, UnavailableInstance); - state(inst->alive, InstanceStillAlive); - state(!size, normal(NormalStatus, "Exited with given parameter" - "size as ZERO.")); + fails(inst, apply(UnavailableInstance)); + state(inst->alive, apply(InstanceStillAlive)); + state(!size, + apply(normal(NormalStatus, "Exited with given parameter size as ZERO."))); - state(((inst->addr = malloc(size)) == NULL), InsufficientMemory); + state(((inst->addr = malloc(size)) == NULL), apply(InsufficientMemory)); inst->size = size; inst->alive = true; - return NormalStatus; + return apply(NormalStatus); } // Status Var_Create(Var *inst, void *addr, size_t size, char *identity) // { // /* Skip when inst is unavailable. */ -// fails(inst, UnavailableInstance); +// fails(inst, apply(UnavailableInstance)); // /* Skip when identity is unavailable. */ // fails(identity, NullPointerAccounted); // /* Skip when identity does not pass the examine. */ @@ -27,22 +27,22 @@ Status Var_Create(Var *inst, size_t size) // inst->size = size; // *inst->identity = *identity; -// return NormalStatus; +// return apply(NormalStatus); // } Status Var_CopyOf(Var *inst, Var *other) { /* Skip when inst or other is unavailable. */ - fails(inst, UnavailableInstance); - state(inst->alive, InstanceStillAlive); - fails(other, InvalidParameter); + fails(inst, apply(UnavailableInstance)); + state(inst->alive, apply(InstanceStillAlive)); + fails(other, apply(InvalidParameter)); /* Copy members from other. Only has to apply size, no addr is needed. */ - state(!((inst->addr = malloc(other->size))), InsufficientMemory); + state(!((inst->addr = malloc(other->size))), apply(InsufficientMemory)); inst->size = other->size; inst->alive = true; - return NormalStatus; + return apply(NormalStatus); } void Var_Delete(Var *inst) @@ -77,13 +77,14 @@ void VarUtils_Swap(Var *v1, Var *v2) Status Var_Literalise(Var *inst, char *buff) { /* Skip when inst is unavailable. */ - state(!inst, UnavailableInstance); + state(!inst, apply(UnavailableInstance)); /* Write into buffer. */ state(!sprintf(buff, VAR_LITERALISE_FORMAT"\n", inst->addr, inst->size), - error(RuntimeError, "Sprintf returned 0 where it should never do.")); + apply( + error(RuntimeError, "Sprintf returned 0 where it should never do."))); - return NormalStatus; + return apply(NormalStatus); } bool Var_Equals(Var *a, Var *b) diff --git a/catlog.c b/catlog.c old mode 100755 new mode 100644 index 9e57acf..1fc47c1 --- a/catlog.c +++ b/catlog.c @@ -1,30 +1,32 @@ -#include +#include "Status/include/status.h" +#include #include +#include Status CatlogMsg_Create(CatlogMsg *inst, CatlogLevel level, char *initiator, char *msg) { /* Skip unavailable instances and parameters. */ - fails(inst, UnavailableInstance); - state((initiator == NULL || msg == NULL), InvalidParameter); + fails(inst, apply(UnavailableInstance)); + state((initiator == NULL || msg == NULL), apply(InvalidParameter)); inst->time = time(NULL); inst->level = level; inst->initiator = initiator; inst->content = msg; - return NormalStatus; + return apply(NormalStatus); } Status CatlogMsg_CopyOf(CatlogMsg *inst, CatlogMsg *other) { /* Skip unavailable instances and parameters. */ - fails(inst, UnavailableInstance); - fails(other, InvalidParameter); + fails(inst, apply(UnavailableInstance)); + fails(other, apply(InvalidParameter)); *inst = *other; - return NormalStatus; + return apply(NormalStatus); } bool CatlogMsg_Equals(CatlogMsg *inst, CatlogMsg *other) @@ -43,23 +45,23 @@ bool CatlogMsg_Equals(CatlogMsg *inst, CatlogMsg *other) Status CatlogSender_Create(CatlogSender *inst, CatlogMsg *msg, FILE *dst) { /* Skip unavailable instances and parameters. */ - fails(inst, UnavailableInstance); - fails(msg, InvalidParameter); + fails(inst, apply(UnavailableInstance)); + fails(msg, apply(InvalidParameter)); - /* Copy and assign, with detections. */ + /* Copy and assign. */ inst->msg = *msg; - inst->dst = (dst == NULL ? (stdout) : dst); + inst->dst = (!dst ? stdout : dst); inst->successful = false; - inst->elapsed = (struct timespec){.tv_sec = 0, .tv_nsec = 0}; + inst->elapsed = (struct timespec){ .tv_sec = 0, .tv_nsec = 0 }; - return NormalStatus; + return apply(NormalStatus); } Status CatlogSender_CopyOf(CatlogSender *inst, CatlogSender *other) { /* Skip unavailable instances and parameters. */ - fails(inst, UnavailableInstance); - fails(other, InvalidParameter); + fails(inst, apply(UnavailableInstance)); + fails(other, apply(InvalidParameter)); /* Copy and assign */ inst->msg = other->msg; @@ -67,7 +69,7 @@ Status CatlogSender_CopyOf(CatlogSender *inst, CatlogSender *other) inst->successful = other->successful; inst->elapsed = other->elapsed; - return NormalStatus; + return apply(NormalStatus); } bool CatlogSender_Equals(CatlogSender *inst, CatlogSender *other) @@ -84,35 +86,38 @@ bool CatlogSender_Equals(CatlogSender *inst, CatlogSender *other) ); } -Status CatlogSender_Send(CatlogSender *inst, char *filepath, bool append) - throws(ReadWriteError) +Status CatlogSender_Send(CatlogSender *inst) { /* Skip unavailable instances and parameters. */ - fails(inst, UnavailableInstance); - fails(filepath, UnavailableFileName); + fails(inst, apply(UnavailableInstance)); - /* Open file. */ - // ensure(CatlogUtils_OpenFile(inst->dst, (append ? "a" : "w")), - // "Unable to open file."); - (void)CatlogUtils_OpenFile(inst->dst, filepath, (append ? "a" : "w")); + const int written = fprintf(inst->dst, "%s", inst->msg.content); /* Write msg. */ - return unknown(NormalStatus, "", !fprintf(inst->dst, "%s", inst->msg.content)); + state(!written, error(ReadWriteError, "No bytes were written into buffer.")); + + return apply(NormalStatus); } -Status CatlogUtils_CalcElapsed(struct timespec t1, struct timespec t2); - -Status CatlogUtils_OpenFile(FILE *store, char *filepath, - const char const *__restrict mode) +Status CatlogUtils_OpenFile(FILE **fileptr, const char *filepath, + const char const *restrict mode) { - /* No need to open a system output stream. */ - if (!strcmp(filepath, "stdin") || - !strcmp(filepath, "stdout") || - !strcmp(filepath, "stderr")) { - return NormalStatus; - } - - store = fopen(filepath, mode); - - return NormalStatus; + /* Skip unavailable instances and parameters. */ + fails(fileptr, apply(UnavailableBuffer)); + fails(filepath, apply(UnavailableFileName)); + fails(mode, apply(UnavailableFileAccessMode)); + + /* Open the file. Return CatCannotOpenFile once failed. */ + state(!(*fileptr = fopen(filepath, mode)), apply(CatCannotOpenFile)); + + return apply(NormalStatus); +} + +Status CatlogUtils_CloseFile(FILE **fileptr) +{ + /* Skip if either the fileptr or the *fileptr is unavailable. */ + state(!fileptr || !*fileptr, apply(UnavailableParameter)); + + /* Return returning code of fclose, sealed with "value". */ + return apply(value(UnknownStatus, fclose(*fileptr))); } diff --git a/catlog.h b/catlog.h old mode 100755 new mode 100644 index 82d366e..8e2e5c2 --- a/catlog.h +++ b/catlog.h @@ -15,6 +15,26 @@ typedef enum { CATLOG_LEVEL_NONE } CatlogLevel; +DEFSTATUS(CatCannotSendMessage, 1, + "Cat had trouble while sending your message.", + STATUS_ERROR, &ReadWriteError); + +DEFSTATUS(CatMessageTooLong, 1, + "Cat cannot finish reading your loooong message before deadline.", + STATUS_ERROR, &MaximumLengthExceeded); + +DEFSTATUS(CatCannotFindFile, 1, + "Cat cannot have your file found.", + STATUS_ERROR, &FileNotFound); + +DEFSTATUS(CatHasInsufficientPermission, 1, + "Cat cannot access your file with its given permission.", + STATUS_ERROR, &InsufficientAccessPermission); + +DEFSTATUS(CatCannotOpenFile, 1, + "Cat cannot open your file because fopen returned NULL.", + STATUS_ERROR, &ReadWriteError); + typedef struct { time_t time; CatlogLevel level; @@ -36,11 +56,10 @@ typedef struct { Status CatlogSender_Create(CatlogSender *inst, CatlogMsg *msg, FILE *dst); Status CatlogSender_CopyOf(CatlogSender *inst, CatlogSender *other); +Status CatlogSender_Send(CatlogSender *inst); bool CatlogSender_Equals(CatlogSender *inst, CatlogSender *other); -Status CatlogSender_Send(CatlogSender *inst, char *filepath, bool append) - throws(ReadWriteError); -Status CatlogUtils_CalcElapsed(struct timespec t1, struct timespec t2); -Status CatlogUtils_OpenFile(FILE *store, char *filepath, - const char const *__restrict mode); +Status CatlogUtils_OpenFile(FILE **fileptr, const char *filepath, + const char const *restrict mode); +Status CatlogUtils_CloseFile(FILE **fileptr); #endif /* COMPOUND_CATLOG_H */ diff --git a/common.h b/common.h index 3500018..8ff4405 100644 --- a/common.h +++ b/common.h @@ -5,45 +5,27 @@ # include # include -/** - * @brief Return $n as the return value, once $o is NULL - * @return given $n as the return value - * @note "fails" stands for "Fails on Null Check" - * @note 'o' stands for "Object" - * @note 'n' stands for "Numeric on Return" - */ -# define fails(o, n) { if ((o) == NULL) return (n); } +/* Get the literal. */ +# define nameof(obj) #obj -/** - * @brief Return $e as the return value, once $v equals $e - * @return given $e as the return value - * @note "trans" stands for "Transits Error Code to Caller" - * @note 'v' stands for "Value" - * @note 'e' stands for "Error Code" - */ +/* Return $n as the return value, once $o is NULL. */ +# define fails(o, n) { if (!o) return (n); } + +/* Return $e as the return value, once $v equals $e. */ # define trans(v, e) { if ((v) == (e)) return (e); } -/** - * @brief Evaluate given statement while the ptr to $s is not NULL - * @return given $n as the return value - * @note "state" stands for "Statement Evaluation" - * @note 's' stands for "Statement" - * @note 'n' stands for "Numeric on Return" - */ +/* Evaluate given statement while the ptr to $s is not NULL. */ # define state(s, n) { if ((s)) return (n); } -/** - * @brief Evaluate given statement while the ptr to $s is not NULL - * @return nothing. - * @note "svoid" stands for "Statement Evaluation in Void" - * @note 's' stands for "Statement" - */ +/* Evaluate given statement while the ptr to $s is not NULL. */ # define svoid(s) { if ((s)) return; } +/* Another way to handle if statements more cleanly. */ +# define solve(s, b) { if (s) b } /* Create a new UnknownStatus on the fly. */ # define unknown(e, c, v) ((Status) {\ - .identity = nameof(UnknownStatus),\ + .identity = e.identity,\ .value = v,\ .description = c,\ .characteristic = STATUS_UNKNOWN,\ @@ -53,7 +35,7 @@ /* Create a new NormalStatus on the fly. */ # define normal(e, c) ((Status) {\ - .identity = nameof(NormalStatus),\ + .identity = e.identity,\ .value = 0,\ .description = c,\ .characteristic = STATUS_NORMAL,\ @@ -63,7 +45,7 @@ /* Create a new ErrorStatus on the fly. */ # define error(e, c) ((Status) {\ - .identity = nameof(ErrorStatus),\ + .identity = e.identity,\ .value = e.value,\ .description = c,\ .characteristic = STATUS_ERROR,\ @@ -71,38 +53,56 @@ .prev = e.prev\ }) -/* Extend the Status chain by giving 'p' for "predecessor" - and 'e' for "Eval-Status". */ +/* Extend the Status chain by giving 'p' for "predecessor" and 'e' for "Eval-Status". */ # define extend(p, e) ((Status) {\ .identity = e.identity,\ .value = p.value,\ .description = e.description,\ .characteristic = p.characteristic,\ - .loc = e.loc,\ + .loc = __HERE__,\ .prev = &p\ }) -/** @brief Create a report in place. - * @return A instance of Status Report customised. - * @note "stamp" stands for "Report Stamp" - * @note 'e' stands for "Exception" - * @note 'c' stands for "Char String of Originator" - */ +# define value(e, v) ((Status) {\ + .identity = e.identity,\ + .value = v,\ + .description = e.description,\ + .characteristic = e.characteristic,\ + .loc = __HERE__,\ + .prev = (Status *)e.prev\ +}) + +# define apply(e) ((Status) {\ + .identity = e.identity,\ + .value = e.value,\ + .description = e.description,\ + .characteristic = e.characteristic,\ + .loc = __HERE__,\ + .prev = (Status *)e.prev\ +}) + +/* Create a report in place with $e for base and $c for initiator literal. */ # define stamp(e, c) ((Report) {\ .status = e,\ .initiator = c,\ .time = time(NULL),\ .priority = REPORT_SENDING_PRIORITY_NORMAL,\ - .task_status = REPORT_SENDING_TASK_STATUS_PENDING\ + .taskprint_status = REPORT_SENDING_TASK_STATUS_PENDING\ }) -/** - * @brief Another way to handle if statements more cleanly. - * @note "solve" stands for "Solve with Statements." - * @note 's' stands for "Statement" - * @note 'b' stands for "Block of Forks" - */ -# define solve(s, b) if (s) b +# define cat(s) {\ + CatlogMsg msg;\ + CatlogMsg_Create(&msg, CATLOG_LEVEL_DEBUG, "CAT", s);\ + CatlogSender sender;\ + CatlogSender_Create(&sender, &msg, stderr);\ + CatlogSender_Send(&sender);\ +} + +# define ok(s, b) { Status _ = s; if (StatusUtils_IsOkay(_)) b } + +# define notok(s, b) { Status _ = s; if (!StatusUtils_IsOkay(_)) b } + +# define seek(s, b) { Status _ = s; b } // /** // * @brief Forcibly return desired value $v once $s is not $k. @@ -116,8 +116,33 @@ // # define sforce(s, k, v) solve((!Status_Equal(s, k)), v) -/* Get the literal. */ -# define nameof(obj) #obj +// # define print_status(s) {\ +// Status _ = s;\ +// char buff[LITERALISATION_LENGTH_MAXIMUM];\ +// notok(Status_Literalise(&_, buff), {\ +// (void)printf("Failed to literalise a status\n");\ +// });\ +// (void)printf("%s\n", buff);\ +// } + +# define print_status(s) {\ + char buff[LITERALISATION_LENGTH_MAXIMUM];\ + (void)Status_Literalise(&s, buff);\ + (void)fprintf(stderr, "%s\n", buff);\ +} + +# define print_statusdump(s) {\ + Status _ = s;\ + const int dump_len = StatusUtils_Depth(&_);\ + Status dump[dump_len];\ + StatusUtils_Dump(&_, dump, 0);\ + for (register int i = 0; i < dump_len; i++) {\ + (void)printf("%d/%d\n", i, dump_len - 1);\ + print_status(dump[i]);\ + }\ +} + +# define strnil(s) (!s ? ("(null)") : s) # define type(T) @@ -126,31 +151,6 @@ # define String(T) String -# define cat(s) {\ - CatlogMsg msg;\ - CatlogMsg_Create(&msg, CATLOG_LEVEL_DEBUG, "CAT", s);\ - CatlogSender sender;\ - CatlogSender_Create(&sender, &msg, stderr);\ - CatlogSender_Send(&sender, "stdout", false);\ -} - -# define _status(s) {\ - const Status _ = s;\ - char buff[LITERALISATION_LENGTH_MAXIMUM];\ - (void)Status_Literalise((Status *)&_, buff);\ - (void)printf("%s\n", buff);\ -} - -# define ok(s, b) {\ - const Status _ = s;\ - if (StatusUtils_IsOkay(_)) b\ -} - -# define notok(s, b) {\ - const Status _ = s;\ - if (!StatusUtils_IsOkay(_)) b\ -} - typedef enum { COMMON_ERROR_NULLPTR = 1, COMMON_ERROR_INVALID_ARGUMENT, diff --git a/namescope.c b/namescope.c index d19195b..58dbe95 100644 --- a/namescope.c +++ b/namescope.c @@ -2,38 +2,38 @@ Status NameScope_Create(NameScope *inst) { - fails(inst, UnavailableInstance); + fails(inst, apply(UnavailableInstance)); /* Create instances for members from inst. */ state(StatusUtils_IsOkay(NameScope_EmptyName(&inst->latest)), - error(RuntimeError, "Failed to initialise latest from NameScope")); + apply(error(RuntimeError, "Failed to initialise latest from NameScope"))); state(StatusUtils_IsOkay(NameScope_EmptyName(&inst->idx)), - error(RuntimeError, "Failed to initialise idx from NameScope")); + apply(error(RuntimeError, "Failed to initialise idx from NameScope"))); state(StatusUtils_IsOkay(NameScope_EmptyName(inst->occupied)), - error(RuntimeError, "Failed to initialise occupied from NameScope")); + apply(error(RuntimeError, "Failed to initialise occupied from NameScope"))); - return NormalStatus; + return apply(NormalStatus); } Status NameScope_CopyOf(NameScope *inst, NameScope *other) { - fails(inst, UnavailableInstance); - fails(other, UnavailableParameter); + fails(inst, apply(UnavailableInstance)); + fails(other, apply(UnavailableParameter)); /* Copy and assign. */ other->latest = inst->latest; other->idx = inst->idx; - const Name len = ` -` -NameScopeUtils_CalcNameArrayLength(&other->occupied); + const Name len = {}; + + NameScopeUtils_CalcNameArrayLength(&other->occupied); for (Name i = (Name){0}; (NameScope_CompareName(i, len) < 0);) { // TODO(william): HERE /* i++ */ state((!StatusUtils_IsOkay(NameScope_CountUp(&i))), - error(RuntimeError, "Error occurred during calculations of Name.")); + apply(error(RuntimeError, "Error occurred during calculations of Name."))); } } @@ -49,9 +49,7 @@ Status NameScope_FormatTrim(Name *inst); Status NameScope_FormatInflate(Name *inst); -Name ` -` -NameScopeUtils_CalcNameArrayLength(Name **arr); +// Status NameScopeUtils_CalcNameArrayLength(Name **arr); bool NameScope_IsAvailable(NameScope *inst, Name idx); diff --git a/platform.h b/platform.h index c1c3b1a..085cba6 100644 --- a/platform.h +++ b/platform.h @@ -11,4 +11,7 @@ # error Platform not supported. Please issue this on github.com/Wilhelm-Lee/Compound --William # endif +// # if defined _WIN16 || defined _WIN32 || defined _WIN64 +// # define COMPOUND_STDIN_PATH + #endif /* COMPOUND_PLATFORM_H */