Repository navigation
objects.h
tanazd1 edited this page Apr 5, 2026
·
3 revisions
Macro-based object system for C using fixed buffers and function pointers.
Obj = lines + key-value pairs + function pointer
Each object is a static struct with:
- Name
- Lines (array of strings)
- Key-value pairs (string → string)
- Function pointer (
run)
typedef struct ObjKV {
char key[64];
char value[256];
} ObjKV;Stores a key-value pair (both strings).
typedef struct Obj {
char name[50];
char lines[MAX_OBJ_LINES][MAX_LINE_LEN];
int line_count;
ObjKV kvs[MAX_OBJ_KV];
int kv_count;
void (*run)(struct Obj*, int argc, char** argv);
} Obj;MAX_OBJ_LINES = 1024
MAX_LINE_LEN = 256
MAX_OBJ_KV = 128obj(myobj) {
// content
}- Creates a global variable:
Obj obj_myobj;-
Sets:
- name = "myobj"
- line_count = 0
- kv_count = 0
- run = NULL
-
Creates a temporary pointer
_obj_ptr
for (Obj* _obj_ptr = &obj_myobj; _obj_ptr; _obj_ptr = NULL)This is how your macros work:
- Runs the block once
- Gives access to
_obj_ptr - Lets inner macros modify the object
line("Hello");- Copies string into:
_obj_ptr->lines[_obj_ptr->line_count]- Increments
line_count
strncpy(..., MAX_LINE_LEN-1);- Max 1024 lines
- Each line max 255 chars
kvs("name", "Yohan");Stores:
_obj_ptr->kvs[kv_count].key = "name"
_obj_ptr->kvs[kv_count].value = "Yohan"Then:
kv_count++- Only string → string
- Max 128 key-value pairs
- Key max 63 chars
- Value max 255 chars
cmd(run_fn);_obj_ptr->run = run_fn;void run_fn(Obj* o, int argc, char** argv);objrune(myobj, "arg1", "arg2");- Builds array:
char* args[] = {"arg1", "arg2", NULL};- Calls:
obj_myobj.run(&obj_myobj, argc, args);sizeof(args)/sizeof(args[0]) - 1✔ Counts arguments automatically
❗ Includes NULL internally
char* args[] = {"a", "b", NULL};
objrunve(myobj, args);obj_myobj.run(&obj_myobj, 0, argv);objrunvp → objrunve
objrunvpe → objrunve
objrunpe → objrunveThese are just wrappers (same behavior)
objadd(myobj) {
line("extra");
}- Reuses existing object
- Adds more lines / kvs
There are NO helper functions, so you must manually loop:
for (int i = 0; i < o->line_count; i++) {
printf("%s\n", o->lines[i]);
}for (int i = 0; i < o->kv_count; i++) {
if (strcmp(o->kvs[i].key, "name") == 0) {
printf("%s\n", o->kvs[i].value);
}
}#include <objects.h>
#include <stdio.h>
#include <string.h>
void run(Obj* o, int argc, char** argv) {
printf("Object: %s\n", o->name);
for (int i = 0; i < o->line_count; i++) {
printf("Line: %s\n", o->lines[i]);
}
for (int i = 0; i < o->kv_count; i++) {
printf("%s = %s\n", o->kvs[i].key, o->kvs[i].value);
}
}
obj(test) {
line("Hello");
line("World");
kvs("lang", "C");
kvs("type", "object");
cmd(run);
}
int main() {
objrune(test, "arg1");
}obj() → creates struct
→ fills data (line, kvs)
→ attaches function
objrun*() → calls function pointer
→ passes object + args
- ❌ Only string key-values
- ❌ No dynamic memory
- ❌ Fixed buffers
- ❌ No bounds error reporting
- ❌ objrunve ignores argc
- ❌ No lookup helpers
- ❌ Not thread-safe
This is NOT OOP.
It is:
- A structured data container
- With:
- text storage
- key-value metadata
- executable callback
Your system is basically:
Struct + Macros + Function Pointer = "Object"
Continue → Objects Tool
🧩 Objects Tool • Built for Termux • Zero dependencies
Home • Objects Tool • objects.h • Examples • install.sh
- Macro-based object system for C
- Static memory (no malloc)
- Designed for Termux environment
🚧 Active Development