Initial implementation of hexagram_list

This commit is contained in:
XANTRONIX Development 2024-01-10 23:31:45 -05:00
parent 28375c64c5
commit 5cbd188b80
3 changed files with 110 additions and 6 deletions

26
include/hexagram/list.h Normal file
View file

@ -0,0 +1,26 @@
#ifndef _HEXAGRAM_LIST_H
#define _HEXAGRAM_LIST_H
#include <sys/types.h>
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 */

View file

@ -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

78
src/list.c Normal file
View file

@ -0,0 +1,78 @@
#include <stdlib.h>
#include <hexagram/list.h>
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; i<index; i++) {
cur = cur->next;
}
return cur->obj;
error_bounds:
return NULL;
}