From e72088d79804e4f689196f4bc700fb9348c77209 Mon Sep 17 00:00:00 2001 From: Carson Fleming Date: Sat, 24 Jan 2026 22:28:22 -0500 Subject: initial commit --- .gitignore | 1 + array.c | 26 ++++++++++++++++++++++++++ array.h | 23 +++++++++++++++++++++++ crash.c | 12 ++++++++++++ crash.h | 4 ++++ makefile | 24 ++++++++++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 .gitignore create mode 100644 array.c create mode 100644 array.h create mode 100644 crash.c create mode 100644 crash.h create mode 100644 makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/array.c b/array.c new file mode 100644 index 0000000..c941b05 --- /dev/null +++ b/array.c @@ -0,0 +1,26 @@ +#include "array.h" +#include "crash.h" +#define CRASH_IF_OOB(array, idx) {\ + if (idx >= array->length)\ + crash("Array access out of bounds: %ld >= %ld\n", idx, array->length);\ +} + +void array_set(array_t* array, size_t idx, void* val) { + CRASH_IF_OOB(array, idx); + array->data[idx] = val; +} + +void* array_get(const array_t* array, size_t idx) { + CRASH_IF_OOB(array, idx); + return array->data[idx]; +} + +void str_set(str_t* str, size_t idx, char val) { + CRASH_IF_OOB(str, idx); + str->data[idx] = val; +} + +char str_get(const str_t* str, size_t idx) { + CRASH_IF_OOB(str, idx); + return str->data[idx]; +} diff --git a/array.h b/array.h new file mode 100644 index 0000000..848e1a7 --- /dev/null +++ b/array.h @@ -0,0 +1,23 @@ +#ifndef __SAFEC_ARRAY_H +#define __SAFEC_ARRAY_H +#include + +/* TODO: it might be good to just have itemsz and char* data */ +typedef struct { + size_t length; + void** data; +} array_t; + +void array_set(array_t* array, size_t idx, void* val); +void* array_get(const array_t* array, size_t idx); + +typedef struct { + size_t length; + char* data; +} str_t; + +void str_set(str_t* str, size_t idx, char val); +char str_get(const str_t* str, size_t idx); +/* TODO: I would like to implement string slicing as part of this */ +/* TODO: reimplement string.h functions for this new string construct */ +#endif diff --git a/crash.c b/crash.c new file mode 100644 index 0000000..f28d88a --- /dev/null +++ b/crash.c @@ -0,0 +1,12 @@ +#include "crash.h" +#include +#include +#include + +void crash(const char* format, ...) { + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + exit(1); + va_end(args); +} \ No newline at end of file diff --git a/crash.h b/crash.h new file mode 100644 index 0000000..f0304e2 --- /dev/null +++ b/crash.h @@ -0,0 +1,4 @@ +#ifndef __SAFEC_CRASH_H +#define __SAFEC_CRASH_H +void crash(const char* message, ...); +#endif \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..6f80fb0 --- /dev/null +++ b/makefile @@ -0,0 +1,24 @@ +CC ?= cc +CFLAGS := -std=c23 -O3 -Wall -Werror -fPIC -c + +BUILD_DIR := build +TARGET := $(BUILD_DIR)/libsafec.a + +SRCS := $(wildcard *.c) +OBJS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(SRCS)) + +.PHONY: all clean + +all: $(BUILD_DIR) $(TARGET) + +$(BUILD_DIR): + mkdir -p $(BUILD_DIR) + +$(TARGET): $(OBJS) + ar rcs $@ $^ + +$(BUILD_DIR)/%.o: %.c + $(CC) $(CFLAGS) $< -o $@ + +clean: + rm -rf $(BUILD_DIR) -- cgit v1.2.3