Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
get_int.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_INT */
27#ifdef FUN_WITH_INI
28case OP_INI_GET_INT: {
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 int def = (vdef.type == VAL_INT) ? (int)vdef.i : 0;
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 int outi = 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 memcpy(alt, full, sizeof(alt));
44 for (size_t i = 0; i < sizeof(alt) && alt[i]; ++i) {
45 if (alt[i] == ':') {
46 alt[i] = '.';
47 break;
48 }
49 }
50 const char *s = iniparser_getstring(d, full, NULL);
51 if (!s) s = iniparser_getstring(d, alt, NULL);
52 if (s) {
53 /* strip optional quotes and parse */
54 char buf[256];
55 size_t n = strlen(s);
56 if (n >= 2 && ((s[0] == '"' && s[n - 1] == '"') || (s[0] == '\'' && s[n - 1] == '\''))) {
57 size_t copy = (n - 2) < sizeof(buf) - 1 ? (n - 2) : sizeof(buf) - 1;
58 memcpy(buf, s + 1, copy);
59 buf[copy] = '\0';
60 s = buf;
61 }
62 /* skip leading spaces */
63 while (*s && (unsigned char)*s <= ' ')
64 s++;
65 char *endp = NULL;
66 long v = strtol(s, &endp, 10);
67 if (endp && endp != s)
68 outi = (int)v;
69 else
70 outi = def;
71 } else {
72 outi = def;
73 }
74 }
79 push_value(vm, make_int(outi));
80 break;
81}
82#endif
@ OP_INI_GET_INT
Definition bytecode.h:206
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_STRING
Definition value.h:53
@ VAL_INT
Definition value.h:51