Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
get_string.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file get_string.c
12 * @brief VM opcode snippet for reading a string from INI (OP_INI_GET_STRING).
13 *
14 * Opcode: OP_INI_GET_STRING
15 * Stack: [default:string] [key:string] [section:string] [handle:int] -> [out:string]
16 *
17 * Behavior
18 * - Pops default string, key, section, and handle; looks up "section:key" and
19 * a dotted fallback. If not found, uses the provided default.
20 * - Pushes the resulting string (copied into a VM Value).
21 *
22 * Errors
23 * - Invalid handle/args result in pushing the default (or empty string).
24 */
25
26/* OP_INI_GET_STRING */
27#ifdef FUN_WITH_INI
29 Value vdef = pop_value(vm);
33 const char *def = (vdef.type == VAL_STRING && vdef.s) ? vdef.s : "";
34 const char *key = (vkey.type == VAL_STRING) ? vkey.s : NULL;
35 const char *sec = (vsec.type == VAL_STRING) ? vsec.s : NULL;
36 int h = (vh.type == VAL_INT) ? (int)vh.i : 0;
37 dictionary *d = ini_get(h);
38 const char *res = def;
39 if (d && sec && key) {
40 char full[1024];
41 char alt[1024];
42 ini_make_full_key(full, sizeof(full), sec, key);
43 /* Build alternate with dot separator for robustness */
44 ini_make_full_key(alt, sizeof(alt), sec, key);
45 size_t flen = strlen(full);
46 if (flen < sizeof(alt) && flen > 0) { /* create dot version in alt */
47 memcpy(alt, full, flen + 1);
48 for (size_t i = 0; i < flen; ++i)
49 if (alt[i] == ':') {
50 alt[i] = '.';
51 break;
52 }
53 }
54 const char *s = iniparser_getstring(d, full, def);
55 if (s == def) { /* not found, try alternate dot form */
56 s = iniparser_getstring(d, alt, def);
57 }
58 res = s ? s : "";
59 }
65 break;
66}
67#endif
@ OP_INI_GET_STRING
Definition bytecode.h:205
CURL * h
Definition download.c:59
int def
Definition get_bool.c:39
const char * sec
Definition get_bool.c:41
Value vh
Definition get_bool.c:38
Value vsec
Definition get_bool.c:37
const char * key
Definition get_bool.c:40
Value vkey
Definition get_bool.c:36
dictionary * d
Definition get_bool.c:43
push_value(vm, make_string(res))
free_value(vdef)
const char * res
Definition get_string.c:38
dictionary * ini_get(int h)
Look up a dictionary pointer by registry handle.
Definition handles.c:55
void ini_make_full_key(char *buf, size_t cap, const char *sec, const char *key)
Build a fully qualified key "section:key" into a caller-provided buffer.
Definition handles.c:68
char * full
Definition match.c:95
uint32_t s
Definition rol.c:31
Tagged union representing a Fun value.
Definition value.h:68
void vdef
Definition stubs.c:63
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
@ VAL_STRING
Definition value.h:53
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580