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
31
32/* OP_INI_GET_BOOL */
33#ifdef FUN_WITH_INI
34case OP_INI_GET_BOOL: {
35 Value vdef = pop_value(vm);
36 Value vkey = pop_value(vm);
37 Value vsec = pop_value(vm);
38 Value vh = 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
char * buf
Definition input_line.c:103
int n
Definition insert.c:41
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
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition value.c:517
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