{"id":19351550,"url":"https://github.com/opensourcedoc/c-stack-mustache","last_synced_at":"2026-06-15T03:31:37.097Z","repository":{"id":77782873,"uuid":"166396149","full_name":"opensourcedoc/c-stack-mustache","owner":"opensourcedoc","description":null,"archived":false,"fork":false,"pushed_at":"2021-07-19T01:32:05.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-24T10:35:46.714Z","etag":null,"topics":["c","generic-programming","mustache-templates"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/opensourcedoc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-01-18T11:41:21.000Z","updated_at":"2022-01-31T23:06:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"76ce4ef4-a95b-472a-867f-c0dfc774aeeb","html_url":"https://github.com/opensourcedoc/c-stack-mustache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/opensourcedoc/c-stack-mustache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opensourcedoc%2Fc-stack-mustache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opensourcedoc%2Fc-stack-mustache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opensourcedoc%2Fc-stack-mustache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opensourcedoc%2Fc-stack-mustache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opensourcedoc","download_url":"https://codeload.github.com/opensourcedoc/c-stack-mustache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opensourcedoc%2Fc-stack-mustache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34346867,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-15T02:00:07.085Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["c","generic-programming","mustache-templates"],"created_at":"2024-11-10T04:36:34.967Z","updated_at":"2026-06-15T03:31:37.091Z","avatar_url":"https://github.com/opensourcedoc.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Generic C Code with Mustache Templates\n\nThis project demonstrates how to bring **generic programming** into the C language by using [Mustache](https://mustache.github.io/), a lightweight logic-less template engine.\n\nC lacks native support for generics. Instead of relying on macros or duplicating similar code for each data type, this approach uses Mustache templates to **generate reusable, type-safe stack implementations** in plain C.\n\nThe repository includes:\n\n* Mustache templates for `.c` and `.h` files of a stack structure.\n* A simple code generation pipeline using `make`.\n* Two test cases demonstrating usage with `int` and a custom wrapper type.\n\nThis project shows that even in a low-level language like C, **meta-programming techniques** can improve maintainability and reduce boilerplate — all without giving up performance or transparency.\n\n---\n\n## Why Mustache?\n\nC macros and conditional compilation can simulate some forms of generic code, but they often come with trade-offs:\n\n* 🧹 **Complex syntax** – Macro logic can quickly become unreadable and hard to debug.\n* 🚫 **Limited flexibility** – Features like conditional defaults or type-dependent expansion are hard to express.\n* 🧪 **Poor tooling support** – IDEs and linters usually struggle with macro-heavy code.\n\nUsing a template engine like **Mustache** provides a different approach:\n\n* ✅ **Clear separation between code and logic** – Templates stay readable and simple.\n* ✅ **Flexible output** – You can generate `.c`/`.h` files for any type combination with consistent formatting.\n* ✅ **Static output** – The generated code is plain C, fully transparent to compilers and debuggers.\n* ✅ **Easy integration** – Combine with `make` or shell scripts for reproducible, automated generation.\n\nThis method doesn’t replace C’s core mechanisms — it **augments them** through pre-processing. It’s especially useful when you want to maintain **clean, consistent, reusable data structures** across multiple types without relying on compiler extensions.\n\n---\n\n## Usage Example\n\nThe following test demonstrates how to use a generated stack for a user-defined type `int_obj_t`. The type wraps an `int` value, and provides custom `clone` and `free` functions for memory safety.\n\n```c\n// int_obj_t is a wrapper around int with malloc/free.\ntypedef struct {\n    int data;\n} int_obj_t;\n```\n\nHere's a minimal usage scenario:\n\n```c\n#include \"stack_obj.h\"\n\nint_obj_t * obj_new(int data);\nvoid * obj_clone(void *obj);\nvoid obj_free(void *obj);\n\nint main(void)\n{\n    stack_t *s = stack_new((stack_params_t){\n        .clone = obj_clone,\n        .free = obj_free\n    });\n\n    int data[] = {3, 4, 5, 6};\n    for (size_t i = 0; i \u003c 4; i++) {\n        stack_push(s, obj_new(data[i]));\n        int_obj_t *top = stack_peek(s);\n        assert(top-\u003edata == data[i]);\n        obj_free(top);\n    }\n\n    // Pops: 6, 5, 4, 3\n    for (int i = 3; i \u003e= 0; i--) {\n        int_obj_t *top = stack_pop(s);\n        assert(top-\u003edata == data[i]);\n        obj_free(top);\n    }\n\n    stack_free(s);\n    return 0;\n}\n```\n\n### Build and Run\n\n```bash\nmake                # Generates C code via Mustache and builds the test\n./test_obj          # Runs the test program\n```\n\nThis confirms that the generated stack works with non-primitive types and handles memory ownership safely via callbacks.\n\n---\n\n## System Requirements\n\n* A modern C compiler\n* GNU Make\n* [`mustach`](https://gitlab.com/jobol/mustach) (template processor)\n\n---\n\n## License\n\nApache License 2.0\nCopyright (c) 2019 ByteBard\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopensourcedoc%2Fc-stack-mustache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopensourcedoc%2Fc-stack-mustache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopensourcedoc%2Fc-stack-mustache/lists"}