https://github.com/zeusdeux/c-libs
stb style single header libs for basic utilities in C programs such string view, arena allocator, gap buffer, etc.
https://github.com/zeusdeux/c-libs
arena-allocator c clibrary dynamic-array gap-buffer stb stb-style string-builder string-view
Last synced: 3 months ago
JSON representation
stb style single header libs for basic utilities in C programs such string view, arena allocator, gap buffer, etc.
- Host: GitHub
- URL: https://github.com/zeusdeux/c-libs
- Owner: zeusdeux
- License: mit
- Created: 2024-01-20T20:03:08.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-27T01:27:43.000Z (5 months ago)
- Last Synced: 2025-03-27T14:56:40.703Z (3 months ago)
- Topics: arena-allocator, c, clibrary, dynamic-array, gap-buffer, stb, stb-style, string-builder, string-view
- Language: C
- Homepage:
- Size: 350 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# c-libs
[stb style](https://github.com/nothings/stb) single header libs for basic utilities in C programs.
## Usage
> Note: DO NOT EVER BUILD DYNAMIC OR STATIC LIBS FOR ANY OF THE SINGLE HEADER LIBS
> Always build these single header libs from source in whatever project uses them
> as correctness of code in this libs is done via asserts which are normally
> turned off for release builds (via -DZDX_ASSERT_DISABLE) and only enabled in debug builds
> and thus if packaged as a dynamic or static library which means it's likely built in release mode
> all the asserts will be removed and the consumers of the library will have no protection
> against bad usage of the functions in that dynamic or static libraryTo use any library in this repository:
1. copy the header file into your project - for e.g., copy `zdx_da.h`
2. copy the util header file into your project - `zdx_util.h`
3. in *one* C file, do the following to include the implementation - for e.g., to use `zdx_da.h`
```c
#define ZDX_DA_IMPLEMENTATION
#include ""
```
4. in all other files, just a `#include ` should suffice
5. done!You can find usage examples in `tests/_test.c` - for e.g., `zdx_da.h` usage can be found in `tests/zdx_da_test.c`.
## Tests
```sh
# make test_ or make test__dbg for debug build of tests
make test_zdx_da
# OR
make test_zdx_da_dbg
```Notes on how to make a single header-file library [can be found here](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt).
## Libs
### `zdx_da.h`
This contains a (mostly) macro based dynamic array implementation.
It only works with `gcc` and `clang` for now as it uses `statement expressions`.It requires a container `struct` with the following mandatory members:
- `size_t length`: will hold dynamic array length
- `size_t capacity`: will hold the capacity of the dynamic array as multiples of items
- ` *items`: this will hold all the items of type `` pushed into the dynamic array. `` can be anything you want.Other members can also be specified in the `struct` alongside the above mandatory members.
For example, the following is a valid dynamic array container:
```c
typedef struct {
char *name;
uint8_t age;
} Person;typedef struct {
int i;
size_t length;
size_t capacity;
Person *items;
float f;
union {
uint8_t ui;
int8_t si;
}
} SomeStruct;
```#### `#define`s
- `DA_ASSERT` - defaults to `assert()` from ``. Can be redefined if you'd like to not abort on assertion failure
- `DA_RESIZE_FACTOR` - defaults to 2. Decides by what factor the capactiy of the dynamic array is scaled. It can be redefined.
- `DA_MIN_CAPACITY` - defaults to 8. Can be redefined to control the minimum capacity of a dynamic array.#### Macros
- `da_push(&container, ...elements)` - appends elements to `container.items` and returns the length after push
```c
typedef struct {
int member1;
struct {
union {
int16_t nested_member1;
float nester_member2;
}
} member2;
} Item;typedef struct {
size_t length;
size_t capacity;
Item *items;
char *tag;
} da_t;da_t container = {0};
size_t len = da_push(&container, (Item){ .member1 = 80 }, (Item){ .member1 = 100, .member2.nested_member1 = "Hello!" });
assert(len == 2 == container.length);
```
- `da_pop(&container)` - pops the last element in `container.items` and returns it
```c
Item i = da_pop(&container);
assert(i.member1 == 100);
assert(container.length == 1);
```
- `da_deinit(&container)` - `free()`s `container.items`
```c
da_deinit(&container);
assert(container.length == 0);
assert(container.capacity == 0);
assert(container.items == NULL);
```### `zdx_util`
This header contains utility macros and maybe utility functions in the future.
#### Enums
- `ZDX_LOG_LEVEL` - `L_ERROR` (1), `L_WARN` (2), `L_INFO` (3)
#### Macros
- `assertm(condition, ...)` - asserts if condition is truthy. If falsy, it prints the message provided in `...` to `stderr` and calls `abort()`
```c
assertm(moreReplHistory.capacity == 2, "Expected: 2, Received: %zu", moreReplHistory.capacity);
```
- `log(ZDX_LOG_LEVEL, ...args)` - prints logs with given level to `stderr`.Logging can be disabled by flag `-DZDX_LOGS_DISABLE`.
```c
log(L_INFO, "%d tests passed. All ok!\n", 10);
```
- `dbg(...args)` - prints debug logs to `stderr`.It's disabled by default and can be enabled by `-DZDX_TRACE_ENABLE`.
```c
dbg("Starting resizing: new capacity %zu", new_capacity);
```
- `bail(...args)` - prings `...args` to `stderr` and then calls `exit(1)`
```c
bail("Oh no!");
```