1
0
Fork 0
forked from fun/fun

Some refactoring and VM code cleanups. (0.41.6)

This commit is contained in:
Johannes Findeisen 2026-05-07 18:39:36 +02:00
commit 4825007b97
9 changed files with 80 additions and 177 deletions

View file

@ -30,5 +30,75 @@
#include <iniparser/dictionary.h>
#include <iniparser/iniparser.h>
#endif
#include "vm/ini/handles.h"
#endif
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
/**
* @brief One slot in the global INI handle registry.
* @details Associates an iniparser dictionary pointer with an in-use flag.
*/
typedef struct {
dictionary *dict;
int in_use;
} IniSlot;
IniSlot g_ini[64];
/**
* @brief Allocate a registry handle for a newly created dictionary.
* @param d Pointer to an iniparser dictionary.
* @return Handle id (>0) on success or 0 on failure.
*/
int ini_alloc_handle(dictionary *d) {
if (!d) return 0;
for (int i = 1; i < (int)(sizeof(g_ini) / sizeof(g_ini[0])); ++i) {
if (!g_ini[i].in_use) {
g_ini[i].in_use = 1;
g_ini[i].dict = d;
return i;
}
}
return 0;
}
/**
* @brief Look up a dictionary pointer by registry handle.
* @param h Handle id previously returned by ini_alloc_handle().
* @return Pointer to dictionary or NULL if not found.
*/
dictionary *ini_get(int h) {
if (h > 0 && h < (int)(sizeof(g_ini) / sizeof(g_ini[0])) && g_ini[h].in_use) return g_ini[h].dict;
return NULL;
}
/**
* @brief Free a previously allocated handle and close its dictionary.
* @param h Handle id to free.
* @return 1 on success, 0 on error (invalid handle or not in use).
*/
int ini_free_handle(int h) {
if (h <= 0 || h >= (int)(sizeof(g_ini) / sizeof(g_ini[0])) || !g_ini[h].in_use) return 0;
if (g_ini[h].dict) iniparser_freedict(g_ini[h].dict);
g_ini[h].dict = NULL;
g_ini[h].in_use = 0;
return 1;
}
/**
* @brief Build a fully qualified key "section:key" into a caller-provided buffer.
* @param buf Destination buffer.
* @param cap Capacity of buf in bytes (including terminator).
* @param sec Section name (may be NULL for default section).
* @param key Key name (must not be NULL).
*/
void ini_make_full_key(char *buf, size_t cap, const char *sec, const char *key) {
if (!buf || cap == 0) return;
if (!sec) sec = "";
if (!key) key = "";
/* iniparser expects section:key; lookup is case-insensitive internally */
snprintf(buf, cap, "%s:%s", sec, key);
}
#endif /* FUN_WITH_INI */

View file

@ -16,16 +16,13 @@
* This unit may be included or compiled conditionally based on build flags.
*/
/* json-c helpers and VM opcode cases (included from vm.c) */
#ifdef FUN_WITH_JSON
#include "value.h"
#include "vm.h"
//#include "value.h"
//#include "vm.h"
#include <json-c/json.h>
#include <string.h>
//#include <string.h>
/* --- Conversion helpers between json-c and Fun Value --- */
/**
* @brief Convert a json-c object into a Fun Value.
*