1
0
Fork 0
forked from fun/fun

Added support for .ini files (iniparser). (0.34.0)

This commit is contained in:
Johannes Findeisen 2025-11-30 02:44:14 +01:00
commit fb5670a699
18 changed files with 710 additions and 5 deletions

32
examples/data/complex.ini Normal file
View file

@ -0,0 +1,32 @@
[app]
name = "FunApp"
version = "1.2.3"
debug = "true"
[database]
host = "localhost"
port = "5432"
user = "fun"
pass = "secret"
pool_size = "8"
timeout = "2.5"
[network]
ssl = "yes"
retries = "3"
base_url = "https://api.example.com"
[features]
feature_x = "on"
feature_y = "off"
[paths]
data_dir = "./data"
log_file = "./logs/app.log"

View file

@ -0,0 +1,25 @@
# INI with subsection-style headers for iniparser 4.2.6
[server]
host = example.org
port = 8080
[server.tls]
enabled = true
version = 1.3
ciphers = TLS_AES_256_GCM_SHA384,TLS_CHACHA20_POLY1305_SHA256
[users.admin]
name = alice
active = yes
quota_gb = 100
[users.guest]
name = bob
active = no
quota_gb = 5
[paths.logs]
dir = ./var/log/fun
rotate = true
max_files = 7

75
examples/ini_complex.fun Normal file
View file

@ -0,0 +1,75 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-30
*/
// Complex INI parsing example using iniparser 4.2.6 opcodes.
path = "./examples/data/complex.ini"
h = ini_load(path)
if h == 0
print("Failed to load "+path)
else
// app
app_name = ini_get_string(h, "app", "name", "FunApp")
app_version = ini_get_string(h, "app", "version", "0.0.0")
app_debug = ini_get_bool(h, "app", "debug", 0)
// database
db_host = ini_get_string(h, "database", "host", "localhost")
db_port = ini_get_int(h, "database", "port", 5432)
db_user = ini_get_string(h, "database", "user", "user")
db_pass = ini_get_string(h, "database", "pass", "")
db_pool = ini_get_int(h, "database", "pool_size", 4)
db_timeout = ini_get_double(h, "database", "timeout", 2.0)
// network
net_ssl = ini_get_bool(h, "network", "ssl", 0)
net_retries = ini_get_int(h, "network", "retries", 3)
base_url = ini_get_string(h, "network", "base_url", "")
// features
feature_x = ini_get_bool(h, "features", "feature_x", 0)
feature_y = ini_get_bool(h, "features", "feature_y", 0)
// paths
data_dir = ini_get_string(h, "paths", "data_dir", "./data")
log_file = ini_get_string(h, "paths", "log_file", "./logs/app.log")
// Print a structured summary
print("[app]")
print(" name=" + app_name)
print(" version=" + app_version)
print(" debug=" + to_string(app_debug))
print("[database]")
print(" host=" + db_host)
print(" port=" + to_string(db_port))
print(" user=" + db_user)
print(" pass=" + db_pass)
print(" pool_size=" + to_string(db_pool))
print(" timeout=" + to_string(db_timeout))
print("[network]")
print(" ssl=" + to_string(net_ssl))
print(" retries=" + to_string(net_retries))
print(" base_url=" + base_url)
print("[features]")
print(" feature_x=" + to_string(feature_x))
print(" feature_y=" + to_string(feature_y))
print("[paths]")
print(" data_dir=" + data_dir)
print(" log_file=" + log_file)
// Clean up
ini_free(h)

30
examples/ini_demo.fun Normal file
View file

@ -0,0 +1,30 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-30
*/
// Minimal demo for INI opcodes using iniparser 4.2.6
path = "./examples/data/complex.ini"
h = ini_load(path)
if h == 0
print("Failed to load " + path)
else
u = ini_get_string(h, "auth", "user", "guest")
r = ini_get_int(h, "network", "retries", 3)
s = ini_get_bool(h, "network", "ssl", 0)
print("user=" + u)
print("retries=" + to_string(r))
print("ssl=" + to_string(s))
ok = ini_set(h, "auth", "token", "abcd1234")
if ok
ini_save(h, path)
ini_free(h)

View file

@ -0,0 +1,70 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-30
*/
// Demonstration of INI subsections like [section.subsection]
// Uses iniparser 4.2.6 via Fun's ini_* opcodes
path = "./examples/data/subsections.ini"
h = ini_load(path)
if h == 0
print("Failed to load "+path)
else
// Top-level server
srv_host = ini_get_string(h, "server", "host", "localhost")
srv_port = ini_get_int(h, "server", "port", 80)
// Subsection: server.tls
tls_enabled = ini_get_bool(h, "server.tls", "enabled", 0)
tls_version = ini_get_double(h, "server.tls", "version", 1.2)
tls_ciphers = ini_get_string(h, "server.tls", "ciphers", "")
// Subsections: users.*
admin_name = ini_get_string(h, "users.admin", "name", "admin")
admin_active = ini_get_bool(h, "users.admin", "active", 1)
admin_quota = ini_get_int(h, "users.admin", "quota_gb", 10)
guest_name = ini_get_string(h, "users.guest", "name", "guest")
guest_active = ini_get_bool(h, "users.guest", "active", 0)
guest_quota = ini_get_int(h, "users.guest", "quota_gb", 1)
// Subsection: paths.logs
logs_dir = ini_get_string(h, "paths.logs", "dir", "./logs")
logs_rotate = ini_get_bool(h, "paths.logs", "rotate", 0)
logs_max_files = ini_get_int(h, "paths.logs", "max_files", 5)
// Print
print("[server]")
print(" host=" + srv_host)
print(" port=" + to_string(srv_port))
print("[server.tls]")
print(" enabled=" + to_string(tls_enabled))
print(" version=" + to_string(tls_version))
print(" ciphers=" + tls_ciphers)
print("[users.admin]")
print(" name=" + admin_name)
print(" active=" + to_string(admin_active))
print(" quota_gb=" + to_string(admin_quota))
print("[users.guest]")
print(" name=" + guest_name)
print(" active=" + to_string(guest_active))
print(" quota_gb=" + to_string(guest_quota))
print("[paths.logs]")
print(" dir=" + logs_dir)
print(" rotate=" + to_string(logs_rotate))
print(" max_files=" + to_string(logs_max_files))
ini_free(h)

View file

@ -20,10 +20,10 @@ md5 = MD5()
print("=== MD5 demo (hex input) ===")
// "abc" => 0x61 0x62 0x63
print(md5.md5_hex("616263")) // -> 900150983cd24fb0d6963f7d28e17f72
print(md5.md5_hex("616263")) // -> 900150983cd24fb0d6963f7d28e17f72
// empty string "" => hex ""
print(md5.md5_hex("")) // -> d41d8cd98f00b204e9800998ecf8427e
print(md5.md5_hex("")) // -> d41d8cd98f00b204e9800998ecf8427e
// "message digest"
print(md5.md5_hex("6d65737361676520646967657374")) // -> f96b697d7cb7938d525a2f31aaf161d0
@ -32,7 +32,10 @@ print(md5.md5_hex("6d65737361676520646967657374")) // -> f96b697d7cb7938d525a2f
print(md5.md5_hex("6162636465666768696a6b6c6d6e6f707172737475767778797a")) // -> c3fcd3d76192e4007dfb496cca67e13b
// "Have Fun!"
print(md5.md5_str("Have Fun!")) // -> 852438d026c018c4307b916406f98c62
print(md5.md5_str("Have Fun!")) // -> 812f2c01287af0e7c0a0b3daa381a51a
// More MD5
print(md5.md5_str("a")) // -> 0cc175b9c0f1b6a831c399e269772661
print("=== Done ===")
@ -43,5 +46,6 @@ d41d8cd98f00b204e9800998ecf8427e
f96b697d7cb7938d525a2f31aaf161d0
c3fcd3d76192e4007dfb496cca67e13b
812f2c01287af0e7c0a0b3daa381a51a
0cc175b9c0f1b6a831c399e269772661
=== Done ===
*/