(MOD) Implemented Array_Create, Array_CopyOf and Array_Delete
(MOD) Replaced project compiler from "gcc" with "clang" due to out-of-order execution keep happening after running with Array. (MOD) Removed certain functions from Utils due to insufficiencies of usage. (MOD) Defined 1 more macro "fail". (MOD) Fixed 1 bug from "var.c": Struct member "alive" was not under supervision from the entire programme cycle. (MOD) Removed 1 useless usage for "Utils_LiteraliseInteger". (MOD) Removed 1 useless block of commented code.
This commit is contained in:
@@ -6,51 +6,28 @@
|
||||
typedef struct {
|
||||
int len;
|
||||
Var *members;
|
||||
bool alive;
|
||||
} Array;
|
||||
|
||||
# define ArrayIndexOutOfBound = (Status){\
|
||||
.identity = nameof(ArrayIndexOutOfBound),\
|
||||
.value = 1,\
|
||||
.description = "Given index was accessing illegal address.",\
|
||||
.characteristic = STATUS_ERROR,\
|
||||
.prev = &MemoryViolation\
|
||||
}
|
||||
DEFSTATUS(ArrayIndexOutOfBound, 1, "Given index was accessing illegal address.", STATUS_ERROR, &MemoryViolation);
|
||||
DEFSTATUS(InvalidArrayLength, 1, "Given length is invalid.", STATUS_ERROR, &ErrorStatus);
|
||||
|
||||
# define InvalidArrayLength (Status){\
|
||||
.value = 1,\
|
||||
.description = "Given length is invalid.",\
|
||||
.characteristic = STATUS_ERROR,\
|
||||
.prev = &ErrorStatus\
|
||||
}
|
||||
|
||||
/* Elementary. */
|
||||
Status Array_Create(Array *inst, int len, size_t size)
|
||||
throws(InsufficientMemory InvalidArrayLength);
|
||||
Status Array_Create(Array *inst, int len, size_t size);
|
||||
Status Array_CopyOf(Array *inst, Array *other);
|
||||
Status Array_Delete(Array *inst);
|
||||
void Array_Delete(Array *inst);
|
||||
Status Array_GetIdx(Array *inst, Var *store, int index);
|
||||
throws(ArrayIndexOutOfBound);
|
||||
Status Array_SetIdx(Array *inst, Var *source, int index);
|
||||
throws(ArrayIndexOutOfBound);
|
||||
bool Array_Equals(Array *arr1, Array *arr2);
|
||||
|
||||
/* Extensional. */
|
||||
Status ArrayUtils_Insert(Array *inst, Var *item, int index);
|
||||
throws(ArrayIndexOutOfBound);
|
||||
Status ArrayUtils_InsertArray(Array *inst, Array *items, int index);
|
||||
throws(ArrayIndexOutOfBound);
|
||||
Status ArrayUtils_Remove(Array *inst, int index);
|
||||
throws(ArrayIndexOutOfBound);
|
||||
Status ArrayUtils_RemoveArray(Array *inst, int off, int len);
|
||||
throws(ArrayIndexOutOfBound InvalidArrayLength);
|
||||
Status ArrayUtils_Subarray(Array *inst, Array *store, int off, int len);
|
||||
throws(ArrayIndexOutOfBound InvalidArrayLength);
|
||||
Status ArrayUtils_Fill(Array *inst, Var *elem, int off, int len);
|
||||
throws(ArrayIndexOutOfBound InvalidArrayLength);
|
||||
Status ArrayUtils_Search(Array *inst, Var *item, int *store);
|
||||
Status ArrayUtils_SearchArray(Array *inst, Array *items, int *store);
|
||||
Status ArrayUtils_Split(Array *inst, Array *fore, Array *rear, int index);
|
||||
throws(ArrayIndexOutOfBound);
|
||||
Status ArrayUtils_Revert(Array *inst);
|
||||
bool ArrayUtils_IsEmpty(Array *inst);
|
||||
bool ArrayUtils_IsBlank(Array *inst);
|
||||
|
@@ -3,155 +3,146 @@
|
||||
|
||||
Status Array_Create(Array *inst, int len, size_t size)
|
||||
{
|
||||
/* Skip unavailable inst and invalid param. */
|
||||
nonull(inst, apply(UnavailableInstance));
|
||||
state((len < 0), apply(InvalidArrayLength));
|
||||
solve((!len), { inst->len = 0; inst->members = NULL; return apply(NormalStatus); })
|
||||
/* Skip the living instances. */
|
||||
state(inst && inst->alive, apply(InstanceStillAlive));
|
||||
|
||||
inst->len = len;
|
||||
inst->members = calloc(len, sizeof(Var));
|
||||
/* Allocate for members from the inst. */
|
||||
state(!(inst->members = calloc(len, sizeof(Var))), apply(InsufficientMemory));
|
||||
|
||||
/* Create for each item from members. */
|
||||
int erridx = -1;
|
||||
Status errstat = EMPTY;
|
||||
for (register int i = 0; i < len; i++) {
|
||||
// TODO(william): Throw InsufficientMemory at following line.
|
||||
// DONE(william): ensure(Var_Create(&inst->members[i], size), "Failed to create a new var.");
|
||||
|
||||
notok((Var_Create(&inst->members[i], size)), {
|
||||
#ifdef __DEBUG__
|
||||
cat("Var_Create failed!\n")
|
||||
#endif
|
||||
notok(Var_Create(&inst->members[i], size), {
|
||||
erridx = i;
|
||||
errstat = apply(_);
|
||||
break;
|
||||
} else {
|
||||
#ifdef __DEBUG__
|
||||
cat("Var_Create success!\n")
|
||||
#endif
|
||||
})
|
||||
}
|
||||
|
||||
/* Review on erridx. Release data that allocated. */
|
||||
if (erridx != -1) {
|
||||
for (register int i = erridx; i >= 0; i--) {
|
||||
/* Got problem during allocations. */
|
||||
if (erridx >= 0) {
|
||||
/* Release members allocated backwardly. */
|
||||
for (register int i = erridx - 1; i >= 0; i--) {
|
||||
Var_Delete(&inst->members[i]);
|
||||
#ifdef __DEBUG__
|
||||
cat("Deleted var from InsufficientMemory from Array_Create!")
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Release array itself. */
|
||||
/* Release the array inst. */
|
||||
free(inst->members);
|
||||
|
||||
return apply(InsufficientMemory);
|
||||
return errstat;
|
||||
}
|
||||
|
||||
/* Assign rest of the struct members. */
|
||||
inst->len = len;
|
||||
inst->alive = true;
|
||||
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
Status Array_CopyOf(Array *inst, Array *other)
|
||||
{
|
||||
// /* Skip unavailable inst and invalid param. */
|
||||
// nonull(inst, apply(UnavailableInstance));
|
||||
// nonull(other, error(InvalidParameter, "Given other was unavailable."));
|
||||
/* Skip unavailble parameters. */
|
||||
nonull(other,
|
||||
apply(annot(UnavailableInstance,
|
||||
"Given object for copying to inst was unavailable.")));
|
||||
|
||||
// /* Assign value for len. */
|
||||
// inst->len = other->len;
|
||||
/* Skip invalid parameters and instances. */
|
||||
state(inst && inst->alive,
|
||||
apply(annot(InstanceStillAlive,
|
||||
"Given inst for being copied was still alive.")));
|
||||
|
||||
// if (inst->members == NULL) return apply(NormalStatus);
|
||||
// match(RuntimeError, Array_Create(inst, other->len), "Failed on recreating "
|
||||
// "array.");
|
||||
state(!other->alive,
|
||||
apply(annot(InstanceNotAlive,
|
||||
"Given object for copying to inst was not alive.")));
|
||||
|
||||
// /* Copy and assign for each member from other to inst. */
|
||||
// for (register int i = 0; i < inst->len; i++) {
|
||||
// inst[i] = other[i];
|
||||
// }
|
||||
state(!other->len,
|
||||
apply(annot(InvalidArrayLength,
|
||||
"Given object for copying to inst has length of ZERO.")));
|
||||
|
||||
// return apply(NormalStatus);
|
||||
/* Allocate for members from the inst. */
|
||||
state(!(inst->members = calloc(other->len, sizeof(Var))),
|
||||
apply(InsufficientMemory));
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
if (other == NULL) return 1;
|
||||
|
||||
String_Create(inst, other->len);
|
||||
/* Create for each item from members. */
|
||||
int erridx = -1;
|
||||
Status errstat = EMPTY;
|
||||
for (register int i = 0; i < other->len; i++) {
|
||||
inst->arr[i] = other->arr[i];
|
||||
notok(Var_Create(&inst->members[i], other->members[0].size), {
|
||||
erridx = i;
|
||||
errstat = apply(_);
|
||||
break;
|
||||
})
|
||||
|
||||
notok(Var_CopyOf(&inst->members[i], &other->members[i]), {
|
||||
erridx = i;
|
||||
errstat = apply(_);
|
||||
break;
|
||||
})
|
||||
}
|
||||
|
||||
return 0;
|
||||
/* Got problem during allocations. */
|
||||
if (erridx >= 0) {
|
||||
/* Release members allocated backwardly. */
|
||||
for (register int i = erridx - 1; i >= 0; i--) {
|
||||
Var_Delete(&inst->members[i]);
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
Status Array_Delete(Array *inst)
|
||||
{
|
||||
/* Skip unavailable inst and invalid param. */
|
||||
nonull(inst, apply(UnavailableInstance));
|
||||
solve((inst->members == NULL), return apply(NormalStatus));
|
||||
|
||||
inst->len = 0;
|
||||
/* Release the array inst. */
|
||||
free(inst->members);
|
||||
inst->members = NULL;
|
||||
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
Status Array_GetIdx(Array *inst, Var *store, int index)
|
||||
{
|
||||
/* Skip unavailable inst and invalid param. */
|
||||
nonull(inst, apply(UnavailableInstance));
|
||||
nonull(store,
|
||||
apply(error(InvalidParameter, "Given reference to store was unavailable.")));
|
||||
state((index < 0 || index >= inst->len), apply(ArrayIndexOutOfBound));
|
||||
|
||||
*store = inst->members[index];
|
||||
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
Status Array_SetIdx(Array *inst, Var *source, int index)
|
||||
{
|
||||
/* Skip unavailable inst and invalid param. */
|
||||
nonull(inst, apply(UnavailableInstance));
|
||||
nonull(source,
|
||||
apply(
|
||||
error(InvalidParameter, "Given reference to source was unavailable.")));
|
||||
state((index < 0 || index >= inst->len), apply(ArrayIndexOutOfBound));
|
||||
|
||||
inst->members[index] = *source;
|
||||
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
bool Array_Equals(Array *a, Array *b)
|
||||
{
|
||||
/* Skip unavailable inst and invalid param. */
|
||||
state((a == NULL || b == NULL), false);
|
||||
state((a->len != b->len), false);
|
||||
|
||||
for (register int i = 0; i < a->len; i++) {
|
||||
if (!Var_Equals(&a->members[i], &b->members[i])) {
|
||||
return false;
|
||||
}
|
||||
return errstat;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Status ArrayUtils_Fill(Array *inst, Var *elem, int off, int len)
|
||||
{
|
||||
nonull(inst, apply(UnavailableInstance));
|
||||
nonull(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;
|
||||
}
|
||||
/* Assign rest of the struct members. */
|
||||
inst->len = other->len;
|
||||
inst->alive = true;
|
||||
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
void Array_Delete(Array *inst)
|
||||
{
|
||||
svoid(!inst || !inst->alive);
|
||||
|
||||
for (register int i = 0; i < inst->len; i++) {
|
||||
Var_Delete(&inst->members[i]);
|
||||
}
|
||||
|
||||
free(inst->members);
|
||||
|
||||
inst->alive = false;
|
||||
inst->len = 0;
|
||||
}
|
||||
|
||||
Status Array_GetIdx(Array *inst, Var *store, int index);
|
||||
|
||||
Status Array_SetIdx(Array *inst, Var *source, int index);
|
||||
|
||||
bool Array_Equals(Array *arr1, Array *arr2);
|
||||
|
||||
|
||||
Status ArrayUtils_Insert(Array *inst, Var *item, int index);
|
||||
|
||||
Status ArrayUtils_InsertArray(Array *inst, Array *items, int index);
|
||||
|
||||
Status ArrayUtils_Remove(Array *inst, int index);
|
||||
|
||||
Status ArrayUtils_RemoveArray(Array *inst, int off, int len);
|
||||
|
||||
Status ArrayUtils_Subarray(Array *inst, Array *store, int off, int len);
|
||||
|
||||
Status ArrayUtils_Fill(Array *inst, Var *elem, int off, int len);
|
||||
|
||||
Status ArrayUtils_Search(Array *inst, Var *item, int *store);
|
||||
|
||||
Status ArrayUtils_SearchArray(Array *inst, Array *items, int *store);
|
||||
|
||||
Status ArrayUtils_Split(Array *inst, Array *fore, Array *rear, int index);
|
||||
|
||||
Status ArrayUtils_Revert(Array *inst);
|
||||
|
||||
bool ArrayUtils_IsEmpty(Array *inst);
|
||||
|
||||
bool ArrayUtils_IsBlank(Array *inst);
|
||||
|
||||
|
@@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.5)
|
||||
|
||||
project (Compound)
|
||||
|
||||
set(CMAKE_C_COMPILER gcc)
|
||||
set(CMAKE_C_COMPILER clang)
|
||||
|
||||
add_compile_options(-g -std=c99 -Wall -Wextra -D__DEBUG__)
|
||||
|
||||
@@ -33,4 +33,6 @@ add_executable(CompoundTest
|
||||
MemMan/src/memman.c
|
||||
Status/src/status.c
|
||||
Utils/src/utils.c
|
||||
Array/src/array.c
|
||||
Var/src/var.c
|
||||
catlog.c)
|
||||
|
@@ -5,10 +5,6 @@ Status Location_Literalise(Location *inst, char *buff)
|
||||
nonull(inst, apply(UnavailableInstance));
|
||||
nonull(buff, apply(UnavailableBuffer));
|
||||
|
||||
/* Literalise line. */
|
||||
char line_buff[LITERALISATION_LENGTH_MAXIMUM] = EMPTY;
|
||||
Utils_LiteraliseInteger(inst->line, line_buff);
|
||||
|
||||
where(
|
||||
snprintf(buff, LITERALISATION_LENGTH_MAXIMUM,
|
||||
LOCATION_LITERALISE_FORMAT,inst->file,inst->line,inst->func),
|
||||
@@ -140,15 +136,6 @@ int StatusUtils_Depth(Status *stat)
|
||||
}
|
||||
|
||||
return cnt;
|
||||
|
||||
// Status *current = stat; // Include this layer of Status.
|
||||
// register int cnt;
|
||||
// for (cnt = 0; (!StatusUtils_IsRecursive(*current)
|
||||
// && StatusUtils_HasPrev(*current)); cnt++) {
|
||||
// current = current->prev;
|
||||
// }
|
||||
|
||||
// return cnt;
|
||||
}
|
||||
|
||||
// bool arguestarter_equal(ArgueStarter *inst1, ArgueStarter *inst2)
|
||||
|
@@ -1,49 +1,49 @@
|
||||
#include <Compound/utils.h>
|
||||
|
||||
int Utils_CalcDigits(long long int n)
|
||||
{
|
||||
if (n == 0) {
|
||||
return 1;
|
||||
}
|
||||
// int Utils_CalcDigits(long long int n)
|
||||
// {
|
||||
// if (n == 0) {
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
n = llabs(n);
|
||||
// n = llabs(n);
|
||||
|
||||
/* Accumulate. */
|
||||
register int i;
|
||||
for (i = 0; n; i++) n /= 10;
|
||||
// /* Accumulate. */
|
||||
// register int i;
|
||||
// for (i = 0; n; i++) n /= 10;
|
||||
|
||||
return i;
|
||||
}
|
||||
// return i;
|
||||
// }
|
||||
|
||||
int Utils_LiteraliseInteger(long long int n, char *buff)
|
||||
{
|
||||
/* Invalid buffer was presented. */
|
||||
if (strlen(buff) != LITERALISATION_LENGTH_MAXIMUM) {
|
||||
buff = NULL;
|
||||
return 0;
|
||||
}
|
||||
// int Utils_LiteraliseInteger(long long int n, char *buff)
|
||||
// {
|
||||
// /* Invalid buffer was presented. */
|
||||
// if (strlen(buff) != LITERALISATION_LENGTH_MAXIMUM) {
|
||||
// buff = NULL;
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
if (!n) {
|
||||
buff = "0";
|
||||
return 1;
|
||||
}
|
||||
// if (!n) {
|
||||
// buff = "0";
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
const int literalising_len = Utils_CalcDigits(n);
|
||||
if (literalising_len >= LITERALISATION_LENGTH_MAXIMUM) {
|
||||
buff = NULL;
|
||||
return 0;
|
||||
}
|
||||
// const int literalising_len = Utils_CalcDigits(n);
|
||||
// if (literalising_len >= LITERALISATION_LENGTH_MAXIMUM) {
|
||||
// buff = NULL;
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
char literalising[literalising_len];
|
||||
for (register int i = 0; i < literalising_len; i++) {
|
||||
literalising[i] = (n / (int)pow(10, i));
|
||||
}
|
||||
// char literalising[literalising_len];
|
||||
// for (register int i = 0; i < literalising_len; i++) {
|
||||
// literalising[i] = (n / (int)pow(10, i));
|
||||
// }
|
||||
|
||||
return literalising_len;
|
||||
}
|
||||
// return literalising_len;
|
||||
// }
|
||||
|
||||
int Utils_DateTimeLiteralise(time_t timer, char *buff,
|
||||
const char *__restrict format)
|
||||
{
|
||||
// int Utils_DateTimeLiteralise(time_t timer, char *buff,
|
||||
// const char *__restrict format)
|
||||
// {
|
||||
|
||||
}
|
||||
// }
|
||||
|
@@ -47,9 +47,10 @@ Status Var_CopyOf(Var *inst, Var *other)
|
||||
|
||||
void Var_Delete(Var *inst)
|
||||
{
|
||||
svoid(!inst);
|
||||
svoid(!inst || !inst->alive);
|
||||
|
||||
free(inst->addr);
|
||||
inst->alive = false;
|
||||
inst->addr = NULL;
|
||||
inst->size = 0;
|
||||
}
|
||||
|
2
catlog.c
2
catlog.c
@@ -99,7 +99,7 @@ Status CatlogSender_Send(CatlogSender *inst)
|
||||
}
|
||||
|
||||
Status CatlogUtils_OpenFile(FILE **fileptr, const char *filepath,
|
||||
const char const *restrict mode)
|
||||
const char *restrict mode)
|
||||
{
|
||||
/* Skip unavailable instances and parameters. */
|
||||
nonull(fileptr, apply(UnavailableBuffer));
|
||||
|
2
catlog.h
2
catlog.h
@@ -59,7 +59,7 @@ Status CatlogSender_CopyOf(CatlogSender *inst, CatlogSender *other);
|
||||
Status CatlogSender_Send(CatlogSender *inst);
|
||||
bool CatlogSender_Equals(CatlogSender *inst, CatlogSender *other);
|
||||
Status CatlogUtils_OpenFile(FILE **fileptr, const char *filepath,
|
||||
const char const *restrict mode);
|
||||
const char *restrict mode);
|
||||
Status CatlogUtils_CloseFile(FILE **fileptr);
|
||||
|
||||
#endif /* COMPOUND_CATLOG_H */
|
||||
|
7
common.h
7
common.h
@@ -1,10 +1,6 @@
|
||||
#ifndef COMPOUND_COMMON_H
|
||||
# define COMPOUND_COMMON_H
|
||||
|
||||
# ifdef __DEBUG__
|
||||
# warning DEBUG IS ON
|
||||
# endif /* __DEBUG__ */
|
||||
|
||||
# include <stdlib.h>
|
||||
# include <stdbool.h>
|
||||
|
||||
@@ -43,6 +39,9 @@
|
||||
/* Return e when passing a failing e commented with c. */
|
||||
# define fails(e, c) { notok(e, return apply(annot(_, c));) }
|
||||
|
||||
/* Return e when passing a failing e. */
|
||||
# define fail(e) { notok(e, return apply(_);) }
|
||||
|
||||
/* Return v when passing a failing e. */
|
||||
# define vfail(e, v) { notok(e, return v;) }
|
||||
|
||||
|
Reference in New Issue
Block a user