Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
get_bool.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_bool.c
12 * @brief VM opcode snippet for reading a boolean from an INI dictionary (OP_INI_GET_BOOL).
13 *
14 * Included by vm.c when FUN_WITH_INI is enabled.
15 *
16 * Opcode: OP_INI_GET_BOOL
17 * Stack: [default:int|bool] [key:string] [section:string] [handle:int] -> [out:int]
18 *
19 * Behavior
20 * - Pops default value (0/1), key, section, and handle.
21 * - Looks up the entry "section:key" in the referenced dictionary. If not found,
22 * also tries a dotted variant "section.key" for compatibility.
23 * - Accepts textual booleans (true/false, yes/no, on/off; case-insensitive) and
24 * numeric values (non-zero => true). Falls back to the provided default when
25 * parsing fails or entry is missing.
26 * - Pushes 1 for true or 0 for false.
27 *
28 * Errors
29 * - Invalid handle or arguments simply yield the default value; no exception.
30 */
31
32/* OP_INI_GET_BOOL */
33#ifdef FUN_WITH_INI
35 Value vdef = pop_value(vm);
39 int def = (vdef.type == VAL_INT || vdef.type == VAL_BOOL) ? (int)vdef.i : 0;
40 const char *key = (vkey.type == VAL_STRING) ? vkey.s : NULL;
41 const char *sec = (vsec.type == VAL_STRING) ? vsec.s : NULL;
42 int h = (vh.type == VAL_INT) ? (int)vh.i : 0;
43 dictionary *d = ini_get(h);
44 int outb = def;
45 if (d && sec && key) {
46 char full[1024];
47 char alt[1024];
48 ini_make_full_key(full, sizeof(full), sec, key);
49 memcpy(alt, full, sizeof(alt));
50 for (size_t i = 0; i < sizeof(alt) && alt[i]; ++i) {
51 if (alt[i] == ':') {
52 alt[i] = '.';
53 break;
54 }
55 }
56 const char *s = iniparser_getstring(d, full, NULL);
57 if (!s) s = iniparser_getstring(d, alt, NULL);
58 if (s) {
59 /* normalize and parse boolean */
60 char buf[256];
61 size_t n = strlen(s);
62 if (n >= 2 && ((s[0] == '"' && s[n - 1] == '"') || (s[0] == '\'' && s[n - 1] == '\''))) {
63 size_t copy = (n - 2) < sizeof(buf) - 1 ? (n - 2) : sizeof(buf) - 1;
64 memcpy(buf, s + 1, copy);
65 buf[copy] = '\0';
66 s = buf;
67 }
68 /* trim spaces */
69 while (*s && (unsigned char)*s <= ' ')
70 s++;
71 /* lower copy for textual booleans */
72 char lb[256];
73 size_t li = 0;
74 for (; s[li] && li < sizeof(lb) - 1; ++li)
75 lb[li] = (char)tolower((unsigned char)s[li]);
76 lb[li] = '\0';
77 if (strcmp(lb, "true") == 0 || strcmp(lb, "yes") == 0 || strcmp(lb, "on") == 0) {
78 outb = 1;
79 } else if (strcmp(lb, "false") == 0 || strcmp(lb, "no") == 0 || strcmp(lb, "off") == 0) {
80 outb = 0;
81 } else {
82 /* numeric */
83 char *endp = NULL;
84 long v = strtol(lb, &endp, 10);
85 outb = (endp && endp != lb) ? (v != 0) : def;
86 }
87 } else {
88 outb = def;
89 }
90 }
95 push_value(vm, make_int(outb ? 1 : 0));
96 break;
97}
98#endif
@ OP_INI_GET_BOOL
Definition bytecode.h:208
Value v
Definition cast.c:22
CURL * h
Definition download.c:59
FunCurlBuf buf
Definition get.c:48
int def
Definition get_bool.c:39
const char * sec
Definition get_bool.c:41
Value vh
Definition get_bool.c:38
int outb
Definition get_bool.c:44
Value vsec
Definition get_bool.c:37
free_value(vdef)
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_int(outb ? 1 :0))
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
int n
Definition insert.c:41
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_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_BOOL
Definition value.h:52
@ 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