forked from ground/cground
List stuff
This commit is contained in:
54
src/types.c
54
src/types.c
@@ -1,5 +1,6 @@
|
||||
#include "types.h"
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GroundValue createIntGroundValue(int64_t in) {
|
||||
GroundValue gv;
|
||||
@@ -309,3 +310,56 @@ void printGroundInstruction(GroundInstruction* gi) {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
List createList() {
|
||||
List list;
|
||||
list.size = 0;
|
||||
list.values = malloc(sizeof(GroundValue));
|
||||
return list;
|
||||
}
|
||||
|
||||
void appendToList(List* list, GroundValue value) {
|
||||
if (list == NULL) {
|
||||
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
GroundValue* ptr = realloc(list, (list->size + 1) * sizeof(GroundValue));
|
||||
if (ptr == NULL) {
|
||||
printf("There was an error allocating memory for a list.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
list->size++;
|
||||
list->values = ptr;
|
||||
list->values[list->size - 1] = value;
|
||||
}
|
||||
|
||||
ListAccess getListAt(List* list, int idx) {
|
||||
if (list == NULL) {
|
||||
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (idx < list->size) {
|
||||
ListAccess retval;
|
||||
retval.value = &list->values[idx];
|
||||
retval.status = LIST_OKAY;
|
||||
return retval;
|
||||
} else {
|
||||
ListAccess retval;
|
||||
retval.value = NULL;
|
||||
retval.status = LIST_OUT_OF_BOUNDS;
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
ListAccessStatus setListAt(List* list, int idx, GroundValue value) {
|
||||
if (list == NULL) {
|
||||
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (idx < list->size) {
|
||||
list->values[idx] = value;
|
||||
return LIST_OKAY;
|
||||
} else {
|
||||
return LIST_OUT_OF_BOUNDS;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user