From 5cbd188b8081d74f422edf8b14427c14c0dc8c0e Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 10 Jan 2024 23:31:45 -0500 Subject: [PATCH] Initial implementation of hexagram_list --- include/hexagram/list.h | 26 ++++++++++++++ src/Makefile | 12 +++---- src/list.c | 78 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 include/hexagram/list.h create mode 100644 src/list.c diff --git a/include/hexagram/list.h b/include/hexagram/list.h new file mode 100644 index 0000000..e4c6c17 --- /dev/null +++ b/include/hexagram/list.h @@ -0,0 +1,26 @@ +#ifndef _HEXAGRAM_LIST_H +#define _HEXAGRAM_LIST_H + +#include + +typedef struct _hexagram_list_item { + void *obj; + struct _hexagram_list_item *next; +} hexagram_list_item; + +typedef struct _hexagram_list { + size_t count; + + hexagram_list_item *head, + *tail; +} hexagram_list; + +hexagram_list *hexagram_list_new(void); + +void hexagram_list_destroy(hexagram_list *list); + +int hexagram_list_item_add(hexagram_list *list, void *obj); + +void *hexagram_list_item_get(hexagram_list *list, size_t index); + +#endif /* _HEXAGRAM_LIST_H */ diff --git a/src/Makefile b/src/Makefile index 5654c62..f527c6c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,15 +7,15 @@ CC = $(CROSS)cc CFLAGS += -fPIC -I$(INCLUDE_PATH) $(shell pkg-config --cflags cairo x11) LDFLAGS = $(shell pkg-config --libs cairo x11) -lXext -HEADERS = dict.h hash.h can.h capture.h pcapng.h module.h window.h \ - gauge.h tacho.h speedo.h thermo.h fuel.h mfd.h cluster.h \ - sim.h schedule.h +HEADERS = list.h dict.h hash.h can.h capture.h pcapng.h module.h \ + window.h gauge.h tacho.h speedo.h thermo.h fuel.h mfd.h \ + cluster.h sim.h schedule.h HEADERS_LOCAL = util.h -OBJS = dict.o hash.o can.o capture.o pcapng.o module.o window.o \ - gauge.o tacho.o speedo.o thermo.o fuel.o mfd.o cluster.o \ - sim.o schedule.o +OBJS = list.o dict.o hash.o can.o capture.o pcapng.o module.o \ + window.o gauge.o tacho.o speedo.o thermo.o fuel.o mfd.o \ + cluster.o sim.o schedule.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..15bed3d --- /dev/null +++ b/src/list.c @@ -0,0 +1,78 @@ +#include + +#include + +hexagram_list *hexagram_list_new(void) { + hexagram_list *list; + + if ((list = malloc(sizeof(*list))) == NULL) { + goto error_malloc_list; + } + + list->count = 0; + list->head = NULL; + list->tail = NULL; + + return list; + +error_malloc_list: + return NULL; +} + +void hexagram_list_destroy(hexagram_list *list) { + hexagram_list_item *cur = list->head; + + while (cur) { + hexagram_list_item *next = cur->next; + + free(cur); + + cur = next; + } + + free(list); +} + +int hexagram_list_item_add(hexagram_list *list, void *obj) { + hexagram_list_item *item; + + if ((item = malloc(sizeof(*item))) == NULL) { + goto error_malloc_item; + } + + item->obj = obj; + item->next = NULL; + + if (list->head == NULL) { + list->head = item; + list->tail = item; + } else { + list->tail->next = item; + list->tail = item; + } + + return 0; + +error_malloc_item: + return -1; +} + +void *hexagram_list_item_get(hexagram_list *list, size_t index) { + hexagram_list_item *cur; + size_t i; + + if (index > list->count) { + goto error_bounds; + } + + cur = list->head; + + for (i=0; inext; + } + + return cur->obj; + +error_bounds: + return NULL; +}