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
25
26/* OP_INI_GET_STRING */
27#ifdef FUN_WITH_INI
29 Value vdef = pop_value(vm);
30 Value vkey = pop_value(vm);
31 Value vsec = pop_value(vm);
32 Value vh = 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 }
64 push_value(vm, make_string(res));
65 break;
66}
67#endif
int res
Definition and.c:34
@ OP_INI_GET_STRING
Definition bytecode.h:205
uint32_t s
Definition rol.c:31
Tagged union representing a Fun value.
Definition value.h:68
void vdef
Definition stubs.c:63
Value vh
Definition stubs.c:44
Value vsec
Definition stubs.c:43
Value vkey
Definition stubs.c:42
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition value.c:517
@ VAL_STRING
Definition value.h:53
@ VAL_INT
Definition value.h:51