(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

@@ -1,45 +1,67 @@
#include <Compound/var.h>
Status Var_Create(Var *inst, void *addr, size_t size, char *identity)
Status Var_Create(Var *inst, size_t size)
{
/* Skip when inst is unavailable. */
fails(inst, UnavailableInstance);
/* Skip when identity is unavailable. */
fails(identity, NullPointerAccounted);
/* Skip when identity does not pass the examine. */
state(!VarUtils_IsIdentityLegal(identity), IllegalVarIdentity);
inst->addr = addr;
inst->size = size;
*inst->identity = *identity;
*inst = (Var) {
.addr = malloc(size),
.size = size
};
if (inst->addr == NULL) {
return InsufficientMemory;
}
return NormalStatus;
}
// Status Var_Create(Var *inst, void *addr, size_t size, char *identity)
// {
// /* Skip when inst is unavailable. */
// fails(inst, UnavailableInstance);
// /* Skip when identity is unavailable. */
// fails(identity, NullPointerAccounted);
// /* Skip when identity does not pass the examine. */
// state(!VarUtils_IsIdentityLegal(identity), IllegalVarIdentity);
// inst->addr = addr;
// inst->size = size;
// *inst->identity = *identity;
// return NormalStatus;
// }
Status Var_CopyOf(Var *inst, Var *other)
{
/* Skip when inst or other is unavailable. */
fails(inst, UnavailableInstance);
fails(other, NullPointerAccounted);
fails(other, InvalidParameter);
/* Copy and assign. */
inst->addr = other->addr;
/* Copy members from other. Only has to apply size, no addr is needed. */
inst->addr = malloc(other->size);
inst->size = other->size;
*inst->identity = *other->identity;
return NormalStatus;
}
void Var_Delete(Var *inst)
{
/* Skip when inst or inst->addr is unavailable. */
svoid(inst == NULL || inst->addr == NULL);
if (inst == NULL) return;
free(inst->addr);
inst->addr = NULL;
inst->size = 0;
*inst->identity = 0;
}
// void Var_Delete(Var *inst)
// {
// /* Skip when inst or inst->addr is unavailable. */
// svoid(inst == NULL || inst->addr == NULL);
// inst->addr = NULL;
// inst->size = 0;
// *inst->identity = 0;
// }
void VarUtils_Swap(Var *v1, Var *v2)
{
/* Skip when v1 or v2 is unavailable. */
@@ -56,49 +78,46 @@ Status Var_Literalise(Var *inst, char *buff)
state(inst == NULL, UnavailableInstance);
/* Write into buffer. */
state(!sprintf(buff, VAR_LITERALISE_FORMAT, inst->identity,
inst->addr, inst->size),
state(!sprintf(buff, VAR_LITERALISE_FORMAT"\n", inst->addr, inst->size),
error(RuntimeError, "Sprintf returned 0 where it should never do."));
return NormalStatus;
}
bool Var_Equal(Var *a, Var *b)
bool Var_Equals(Var *a, Var *b)
{
/* Skip unavailable inst and invalid param. */
state((a == NULL || b == NULL), false);
return (a->addr == b->addr &&
a->size == b->size &&
(!strcmp(a->identity, b->identity)));
return (a->addr == b->addr && a->size == b->size);
}
bool VarUtils_IsIdentityLegal(char *identity)
{
/* Skip when identity is unavailable. */
state(identity == NULL, false);
// bool VarUtils_IsIdentityLegal(char *identity)
// {
// /* Skip when identity is unavailable. */
// state(identity == NULL, false);
const int len = strlen(identity);
// const int len = strlen(identity);
/* Skip when identity is empty. */
state(len == 0, false);
// /* Skip when identity is empty. */
// state(len == 0, false);
/* Skip when the first char is not within alphabet. */
state(ATRANGE('a', 'z', identity[0])
|| ATRANGE('A', 'Z', identity[0]), false);
// /* Skip when the first char is not within alphabet. */
// state(ATRANGE('a', 'z', identity[0])
// || ATRANGE('A', 'Z', identity[0]), false);
/* Skip when the length of identity is greater that VAR_IDENTITY_LENGTH. */
state(len > VAR_IDENTITY_LENGTH, false);
// /* Skip when the length of identity is greater that VAR_IDENTITY_LENGTH. */
// state(len > VAR_IDENTITY_LENGTH, false);
/* Skip when identity has space and illegal characters in it. */
const int illegal_len = strlen(VAR_IDENTITY_ILLEGAL_CHAR);
for (register int i = 0; i < len; i++) {
for (register int j = 0; j < illegal_len; j++) {
if (identity[i] == VAR_IDENTITY_ILLEGAL_CHAR[j]) {
return false;
}
}
}
// /* Skip when identity has space and illegal characters in it. */
// const int illegal_len = strlen(VAR_IDENTITY_ILLEGAL_CHAR);
// for (register int i = 0; i < len; i++) {
// for (register int j = 0; j < illegal_len; j++) {
// if (identity[i] == VAR_IDENTITY_ILLEGAL_CHAR[j]) {
// return false;
// }
// }
// }
return true;
}
// return true;
// }