confgen

Config parser generator
git clone git://git.finwo.net/app/confgen
Log | Files | Refs | LICENSE

commit 60277d27ee7f5763765bf7a0c811b72268e79320
Author: finwo <finwo@pm.me>
Date:   Wed, 15 Jul 2026 00:38:43 +0200

Project init

Diffstat:
A.dep | 0
A.editorconfig | 13+++++++++++++
A.gitignore | 3+++
ALICENSE.md | 34++++++++++++++++++++++++++++++++++
AMakefile | 37+++++++++++++++++++++++++++++++++++++
Asrc/main.c | 41+++++++++++++++++++++++++++++++++++++++++
Asrc/util/buffer.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/util/buffer.h | 32++++++++++++++++++++++++++++++++
8 files changed, 221 insertions(+), 0 deletions(-)

diff --git a/.dep b/.dep diff --git a/.editorconfig b/.editorconfig @@ -0,0 +1,13 @@ +# 2d0e4a3f-ac19-430c-bf1b-46c68651ce21 +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +indent_size = 2 +indent_style = space +trim_trailing_whitespace = true + +[Makefile*] +indent_style = tab diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +/confgen +*.o +/lib/ diff --git a/LICENSE.md b/LICENSE.md @@ -0,0 +1,34 @@ +Copyright (c) 2026 finwo + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to use, copy, +modify, and distribute the Software, subject to the following conditions: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions, and the following disclaimer. + + 2. Redistributions in binary form, or any public offering of the Software + (including hosted or managed services), must reproduce the above copyright + notice, this list of conditions, and the following disclaimer in the + documentation and/or other materials provided. + + 3. Any redistribution or public offering of the Software must clearly attribute + the Software to the original copyright holder, reference this License, and + include a link to the official project repository or website. + + 4. The Software may not be renamed, rebranded, or marketed in a manner that + implies it is an independent or proprietary product. Derivative works must + clearly state that they are based on the Software. + + 5. Modifications to copies of the Software must carry prominent notices stating + that changes were made, the nature of the modifications, and the date of the + modifications. + +Any violation of these conditions terminates the permissions granted herein. + +THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,37 @@ +# Initial config +# LIBS= +SRC:= +SRC+=$(wildcard src/*.c) +SRC+=$(wildcard src/*/*.c) + +CFLAGS?=-Wall -s -O2 + +INCLUDES:= +INCLUDES+=-I src + +# include lib/.dep/config.mk + +CFLAGS+=$(INCLUDES) + +# Which objects to generate before merging everything together +OBJ:=$(SRC:.c=.o) + +BIN=confgen + +.PHONY: default +default: $(BIN) +# default: $(BIN) $(BIN).1 + +%.o: %.c $(LIBS) + $(CC) $(CFLAGS) $(@:.o=.c) -D__NAME=\"$(BIN)\" -c -o $@ + +$(BIN): $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $@ + +# # Build manpage +# $(BIN).1: manpage.1.md +# env NAME=$(BIN) envsubst < manpage.1.md | pandoc --standalone --from markdown --to man -o $(BIN).1 + +.PHONY: clean +clean: + rm -f $(OBJ) diff --git a/src/main.c b/src/main.c @@ -0,0 +1,41 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "util/buffer.h" + +int main() { + + struct confgen_buffer *buf = calloc(1, sizeof(struct confgen_buffer)); + + confgen_buffer_append(buf, "Hello world", 11); + + printf("Stats:\n"); + printf(" alloc: %p\n" , buf->alloc); + printf(" dat : %p\n" , buf->dat); + printf(" len : %ld\n", buf->len); + printf(" cap : %ld\n", buf->cap); + printf(" data : [%*s]\n", (int)(buf->len), buf->dat); + printf("\n"); + + confgen_buffer_append(buf, "™", 3); + + printf("Stats:\n"); + printf(" alloc: %p\n" , buf->alloc); + printf(" dat : %p\n" , buf->dat); + printf(" len : %ld\n", buf->len); + printf(" cap : %ld\n", buf->cap); + printf(" data : [%*s]\n", (int)(buf->len), buf->dat); + printf("\n"); + + confgen_buffer_consume(buf, 6); + + printf("Stats:\n"); + printf(" alloc: %p\n" , buf->alloc); + printf(" dat : %p\n" , buf->dat); + printf(" len : %ld\n", buf->len); + printf(" cap : %ld\n", buf->cap); + printf(" data : [%*s]\n", (int)(buf->len), buf->dat); + printf("\n"); + + return 0; +} diff --git a/src/util/buffer.c b/src/util/buffer.c @@ -0,0 +1,61 @@ +#include <string.h> +#include <stdlib.h> + +#include "buffer.h" + +#ifndef MAX +#define MAX(a,b) ((a)>(b)?(a):(b)) +#endif + +#ifndef MIN +#define MIN(a,b) ((a)<(b)?(a):(b)) +#endif + + +void confgen_buffer_append(struct confgen_buffer *subject, const char *source, size_t length) { + if (!subject) return; + + // Initial alloc if blank buffer + if (!subject->alloc) { + size_t l = MAX(32, length); + subject->alloc = malloc(l); + subject->dat = subject->alloc; + subject->cap = l; + subject->len = 0; + goto confgen_buffer_append_copy; // Simply skips cap checks + } + + // Eject consumed data if we need space + if ( + (subject->dat != subject->alloc) && + ((subject->len + length) > subject->cap) + ) { + size_t diff = subject->dat - subject->alloc; + memmove(subject->alloc, subject->dat, subject->len); + subject->cap += diff; + subject->dat = subject->alloc; + } + + // Grow buffer if still not enough space + if ((subject->len + length) > subject->cap) { + subject->cap = MAX(32, subject->cap * 2); + subject->alloc = realloc(subject->alloc, subject->cap); + subject->dat = subject->alloc; + } + + // Add the data to the end +confgen_buffer_append_copy: + memcpy(subject->dat + subject->len, source, length); + subject->len += length; +} + +size_t confgen_buffer_consume(struct confgen_buffer *subject, size_t length) { + if (!subject) return 0; + + size_t n = MIN(length, subject->len); + subject->dat += n; + subject->len -= n; + subject->cap -= n; + + return n; +} diff --git a/src/util/buffer.h b/src/util/buffer.h @@ -0,0 +1,32 @@ +#ifndef __CONFGEN_UTIL_BUFFER_H__ +#define __CONFGEN_UTIL_BUFFER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stddef.h> + +struct confgen_buffer { + char *alloc; + char *dat; + size_t len; + size_t cap; +}; + +/** + * Append <length> bytes to the data in the buffer + * Copies the data, does not mutate the source + */ +void confgen_buffer_append(struct confgen_buffer *subject, const char *source, size_t length); + +/** + * Remove <length> bytes from the front of the buffer + */ +size_t confgen_buffer_consume(struct confgen_buffer *subject, size_t length); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // __CONFGEN_UTIL_BUFFER_H__