Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
get_double.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_DOUBLE */
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 double def = (vdef.type == VAL_FLOAT) ? vdef.d : (vdef.type == VAL_INT ? (double)vdef.i : 0.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 double outd = 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 char buf[256];
54 size_t n = strlen(s);
55 if (n >= 2 && ((s[0] == '"' && s[n - 1] == '"') || (s[0] == '\'' && s[n - 1] == '\''))) {
56 size_t copy = (n - 2) < sizeof(buf) - 1 ? (n - 2) : sizeof(buf) - 1;
57 memcpy(buf, s + 1, copy);
58 buf[copy] = '\0';
59 s = buf;
60 }
61 while (*s && (unsigned char)*s <= ' ')
62 s++;
63 char *endp = NULL;
64 double v = strtod(s, &endp);
65 if (endp && endp != s)
66 outd = v;
67 else
68 outd = def;
69 } else {
70 outd = def;
71 }
72 }
77 push_value(vm, make_float(outd));
78 break;
79}
80#endif
@ OP_INI_GET_DOUBLE
Definition bytecode.h:207
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_float(double v)
Construct a Value representing a double-precision float.
Definition value.c:64
@ VAL_STRING
Definition value.h:53
@ VAL_INT
Definition value.h:51
@ VAL_FLOAT
Definition value.h:58