{"id":16095735,"url":"https://github.com/zeusdeux/c-libs","last_synced_at":"2025-04-05T20:14:50.769Z","repository":{"id":218268606,"uuid":"746009098","full_name":"zeusdeux/c-libs","owner":"zeusdeux","description":"stb style single header libs for basic utilities in C programs such string view, arena allocator, gap buffer, etc.","archived":false,"fork":false,"pushed_at":"2025-01-27T01:27:43.000Z","size":358,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T14:56:40.703Z","etag":null,"topics":["arena-allocator","c","clibrary","dynamic-array","gap-buffer","stb","stb-style","string-builder","string-view"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zeusdeux.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-20T20:03:08.000Z","updated_at":"2025-01-27T01:27:46.000Z","dependencies_parsed_at":"2024-03-19T00:22:46.899Z","dependency_job_id":"4fbf05e2-f1ac-449f-801f-6d86464bba6e","html_url":"https://github.com/zeusdeux/c-libs","commit_stats":{"total_commits":148,"total_committers":1,"mean_commits":148.0,"dds":0.0,"last_synced_commit":"62811a898b8db1a1dd983fc24b8dbab1f3a912f6"},"previous_names":["zeusdeux/c-libs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeusdeux%2Fc-libs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeusdeux%2Fc-libs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeusdeux%2Fc-libs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeusdeux%2Fc-libs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zeusdeux","download_url":"https://codeload.github.com/zeusdeux/c-libs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393573,"owners_count":20931813,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arena-allocator","c","clibrary","dynamic-array","gap-buffer","stb","stb-style","string-builder","string-view"],"created_at":"2024-10-09T17:08:25.531Z","updated_at":"2025-04-05T20:14:50.743Z","avatar_url":"https://github.com/zeusdeux.png","language":"C","readme":"# c-libs\n\n[stb style](https://github.com/nothings/stb) single header libs for basic utilities in C programs.\n\n## Usage\n\n\u003e Note: DO NOT EVER BUILD DYNAMIC OR STATIC LIBS FOR ANY OF THE SINGLE HEADER LIBS\n\u003e Always build these single header libs from source in whatever project uses them\n\u003e as correctness of code in this libs is done via asserts which are normally\n\u003e turned off for release builds (via -DZDX_ASSERT_DISABLE) and only enabled in debug builds\n\u003e and thus if packaged as a dynamic or static library which means it's likely built in release mode\n\u003e all the asserts will be removed and the consumers of the library will have no protection\n\u003e against bad usage of the functions in that dynamic or static library\n\nTo use any library in this repository:\n\n1. copy the header file into your project - for e.g., copy `zdx_da.h`\n2. copy the util header file into your project - `zdx_util.h`\n3. in *one* C file, do the following to include the implementation - for e.g., to use `zdx_da.h`\n  ```c\n  #define ZDX_DA_IMPLEMENTATION\n  #include \"\u003cpath to zdx_da.h\u003e\"\n  ```\n4. in all other files, just a `#include \u003cpath to lib header\u003e` should suffice\n5. done!\n\nYou can find usage examples in `tests/\u003clib\u003e_test.c` - for e.g., `zdx_da.h` usage can be found in `tests/zdx_da_test.c`.\n\n## Tests\n\n```sh\n# make test_\u003clib name\u003e or make test_\u003clib name\u003e_dbg for debug build of tests\nmake test_zdx_da\n# OR\nmake test_zdx_da_dbg\n```\n\nNotes on how to make a single header-file library [can be found here](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt).\n\n## Libs\n\n### `zdx_da.h`\n\nThis contains a (mostly) macro based dynamic array implementation.\nIt only works with `gcc` and `clang` for now as it uses `statement expressions`.\n\nIt requires a container `struct` with the following mandatory members:\n\n- `size_t length`: will hold dynamic array length\n- `size_t capacity`: will hold the capacity of the dynamic array as multiples of items\n- `\u003ctype\u003e *items`: this will hold all the items of type `\u003ctype\u003e` pushed into the dynamic array. `\u003ctype\u003e` can be anything you want.\n\nOther members can also be specified in the `struct` alongside the above mandatory members.\n\nFor example, the following is a valid dynamic array container:\n\n```c\ntypedef struct {\n    char *name;\n    uint8_t age;\n} Person;\n\ntypedef struct {\n    int i;\n    size_t length;\n    size_t capacity;\n    Person *items;\n    float f;\n    union {\n        uint8_t ui;\n        int8_t si;\n    }\n} SomeStruct;\n```\n\n#### `#define`s\n\n- `DA_ASSERT` - defaults to `assert()` from `\u003cassert.h\u003e`. Can be redefined if you'd like to not abort on assertion failure\n- `DA_RESIZE_FACTOR` - defaults to 2. Decides by what factor the capactiy of the dynamic array is scaled. It can be redefined.\n- `DA_MIN_CAPACITY` - defaults to 8. Can be redefined to control the minimum capacity of a dynamic array.\n\n#### Macros\n\n- `da_push(\u0026container, ...elements)` - appends elements to `container.items` and returns the length after push\n  ```c\n  typedef struct {\n      int member1;\n      struct {\n        union {\n          int16_t nested_member1;\n          float nester_member2;\n        }\n      } member2;\n  } Item;\n\n  typedef struct {\n      size_t length;\n      size_t capacity;\n      Item *items;\n      char *tag;\n  } da_t;\n\n  da_t container = {0};\n\n  size_t len = da_push(\u0026container, (Item){ .member1 = 80 }, (Item){ .member1 = 100, .member2.nested_member1 = \"Hello!\" });\n  assert(len == 2 == container.length);\n  ```\n  - `da_pop(\u0026container)` - pops the last element in `container.items` and returns it\n  ```c\n  Item i = da_pop(\u0026container);\n  assert(i.member1 == 100);\n  assert(container.length == 1);\n  ```\n  - `da_deinit(\u0026container)` - `free()`s `container.items`\n  ```c\n  da_deinit(\u0026container);\n  assert(container.length == 0);\n  assert(container.capacity == 0);\n  assert(container.items == NULL);\n  ```\n\n### `zdx_util`\n\nThis header contains utility macros and maybe utility functions in the future.\n\n#### Enums\n\n- `ZDX_LOG_LEVEL` - `L_ERROR` (1), `L_WARN` (2), `L_INFO` (3)\n\n#### Macros\n\n- `assertm(condition, ...)` - asserts if condition is truthy. If falsy, it prints the message provided in `...` to `stderr` and calls `abort()`\n  ```c\n  assertm(moreReplHistory.capacity == 2, \"Expected: 2, Received: %zu\", moreReplHistory.capacity);\n  ```\n- `log(ZDX_LOG_LEVEL, ...args)` - prints logs with given level to `stderr`.\n\n  Logging can be disabled by flag `-DZDX_LOGS_DISABLE`.\n  ```c\n  log(L_INFO, \"%d tests passed. All ok!\\n\", 10);\n  ```\n- `dbg(...args)` - prints debug logs to `stderr`.\n\n  It's disabled by default and can be enabled by `-DZDX_TRACE_ENABLE`.\n  ```c\n  dbg(\"Starting resizing: new capacity %zu\", new_capacity);\n  ```\n- `bail(...args)` - prings `...args` to `stderr` and then calls `exit(1)`\n  ```c\n  bail(\"Oh no!\");\n  ```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeusdeux%2Fc-libs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeusdeux%2Fc-libs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeusdeux%2Fc-libs/lists"}