Compare commits
162 Commits
Author | SHA1 | Date |
---|---|---|
Daniel Ziltener | 99e8b1e8ed | |
Daniel Ziltener | 91ea498688 | |
Daniel Ziltener | f2ccebdd2d | |
Daniel Ziltener | b4b9e39c70 | |
Martin Tournoij | 5221b3d3d6 | |
CK Tan | 6fe7fee127 | |
Vlad-Ștefan Harbuz | 52e9c039c5 | |
CK Tan | d4c94500c4 | |
Daniel Ziltener | ec1ef8715a | |
Daniel Ziltener | d2e5d78a38 | |
Daniel Ziltener | c966b0e022 | |
CK Tan | 894902820a | |
Kamil Giszczak | e4107c4554 | |
Vladislav Laetansky | 034b23ed3e | |
CK Tan | 4e7b082ccc | |
Vladislav Laetansky | 8a863d1ec8 | |
CK Tan | 99c1e33222 | |
CK Tan | d3a4391ae9 | |
CK Tan | 32c38751b9 | |
CK Tan | 64e280e20b | |
ownesis | 8434e75006 | |
CK Tan | 310f02dd67 | |
Toni | 62320462f5 | |
CK Tan | 353c68c3df | |
Cameron Moore | 1d4fc82ebc | |
CK Tan | 37954d9b20 | |
CK Tan | 208203af46 | |
CK Tan | b5818182a4 | |
CK Tan | 4636173fed | |
CK Tan | 6b49aaf863 | |
CK Tan | 45921ae3db | |
CK Tan | bf0447ab49 | |
CK Tan | 511749ebb9 | |
CK Tan | 24e4472b44 | |
CK Tan | 789930f7c9 | |
CK Tan | d7dd697c35 | |
CK Tan | 22aa38e31d | |
CK Tan | 3c1107177f | |
CK Tan | 45e8f3c903 | |
CK Tan | a7557174ae | |
CK Tan | e9bfb63807 | |
CK Tan | 06e2fceedf | |
CK Tan | 369fd2451b | |
CK Tan | dbc1ef26d6 | |
CK Tan | 937d88e009 | |
CK Tan | 836ac94afe | |
CK Tan | 20bee50b59 | |
CK Tan | 715fa54d45 | |
CK Tan | 1cfa8e42c4 | |
CK Tan | fd82018f79 | |
mweshahy | 970ab84bae | |
CK Tan | c5d2e37db7 | |
CK Tan | e97a56633e | |
CK Tan | 94ded5f2c1 | |
CK Tan | 63feb40e3b | |
CK Tan | 5039045945 | |
CK Tan | 61557dcbaa | |
CK Tan | ae9514e87b | |
CK Tan | 13b8fde204 | |
CK Tan | f7fde945e6 | |
CK Tan | a2d704c7af | |
CK Tan | 72eda69b5a | |
CK Tan | 51e9daabd2 | |
CK Tan | f7c39273fe | |
CK Tan | 0a280911de | |
CK Tan | ba73b18f50 | |
CK Tan | 32636ccd01 | |
CK Tan | e9ea925e05 | |
CK Tan | 579e72c377 | |
CK Tan | 5fb977508a | |
CK Tan | 577e48e03f | |
CK Tan | 1ee269d00c | |
CK Tan | 94136b9f04 | |
CK Tan | 6c72be57df | |
CK Tan | 893f24e206 | |
CK Tan | 3fdd187b2a | |
CK Tan | 26fdad9205 | |
CK Tan | 5c60d1e15c | |
CK Tan | f01507cabe | |
CK Tan | 6eb7ea27f2 | |
CK Tan | 3941e0bade | |
CK Tan | 7a3e402cee | |
CK Tan | a28aae757f | |
CK Tan | 6e1f2e3db8 | |
CK Tan | 84fd6725e0 | |
CK Tan | 71a9fd9772 | |
CK Tan | d17c005da8 | |
CK Tan | 3c50f43516 | |
CK Tan | 7a3f8afc15 | |
CK Tan | 5be06807ad | |
CK Tan | bc6f2fa6b8 | |
CK Tan | 2e8feafae8 | |
CK Tan | 1901ac3166 | |
CK Tan | ddb9cc4eeb | |
Fridfinnur M | 4b42548c3d | |
CK Tan | e3a03f5ec7 | |
CK Tan | 9ea8eedd85 | |
CK Tan | b539e3f20e | |
CK Tan | d879b89737 | |
dcresswell | 5d1d8e3389 | |
dcresswell | e1e7369907 | |
CK Tan | a1d23056ac | |
CK Tan | 7af2dbf91f | |
CK Tan | 8a5050696f | |
CK Tan | a94dbebc17 | |
Stanley Pinchak | f25dee1273 | |
Stanley Pinchak | 8278e68df9 | |
CK Tan | c59fa7a058 | |
CK Tan | 5bc8f64fdd | |
CK Tan | cdbb9decfb | |
CK Tan | eaa5d04539 | |
CK Tan | 4078d23a33 | |
CK Tan | 416be8de63 | |
CK Tan | 10787cbff9 | |
CK Tan | 532239d83a | |
Theak | 7efd69ca83 | |
CK Tan | b92d6a2c82 | |
CK Tan | f31bcd0adf | |
CK Tan | f782b12864 | |
CK Tan | 2453664abb | |
CK Tan | 987f4ef0d7 | |
CK Tan | 685ca97470 | |
CK Tan | 896561d5b7 | |
CK Tan | 9ed009b950 | |
CK Tan | a1a8911503 | |
CK Tan | c32a6e92f1 | |
CK Tan | 63793f92ef | |
CK Tan | bc951780ab | |
CK Tan | 15f44eba49 | |
CK Tan | c5907e1504 | |
CK Tan | 9394192bfc | |
CK Tan | 4a31cd8cbf | |
CK Tan | f12c8f00b4 | |
CK Tan | 3bc235331f | |
Odin Hultgren Van Der Horst | aba3905eb8 | |
CK Tan | b701a09579 | |
Oliver Breitwieser | 93cae7a0e6 | |
CK Tan | e93b40c3ea | |
Jesper Peterson | 386dccc84f | |
CK Tan | bd76f1276e | |
CK Tan | c4325a38fb | |
CK Tan | 0ce0c0d9d0 | |
CK Tan | 05e27a7cab | |
CK Tan | 43f7a52064 | |
CK Tan | 3b6fe37d66 | |
Stanley Pinchak | 3fed0ab455 | |
Stanley Pinchak | b53c016877 | |
Stanley Pinchak | 88eacb3fe0 | |
CK Tan | f06bf3a5cc | |
Iain Anderson | 55334019ac | |
CK Tan | 56c42b7aed | |
CK Tan | 624013252b | |
CK Tan | 95ee4414f6 | |
bamchoh | 79b40abd94 | |
CK Tan | 01ecb88d13 | |
CK Tan | edc6a13246 | |
CK Tan | a0b45225e2 | |
CK Tan | e6a6db8bda | |
CK Tan | 287defadb1 | |
CK Tan | a0b38a34ea | |
CK Tan | 09de99f7a8 | |
CK Tan | 75aa7b2d5a |
|
@ -0,0 +1 @@
|
|||
use guix chicken chicken-r7rs chicken-rfc3339 chicken-coops chicken-test chicken-salmonella chicken-salmonella-html-report
|
28
Makefile
28
Makefile
|
@ -1,14 +1,14 @@
|
|||
##
|
||||
# Toml
|
||||
#
|
||||
# @file
|
||||
# @version 0.1
|
||||
|
||||
.PHONY: salmonella
|
||||
salmonella:
|
||||
rm -rf report
|
||||
rm -f toml*.so
|
||||
salmonella --keep-repo --repo-dir=./petri-dish; \
|
||||
salmonella-html-report ./salmonella.log report
|
||||
|
||||
# end
|
||||
##
|
||||
# Toml
|
||||
#
|
||||
# @file
|
||||
# @version 0.1
|
||||
|
||||
.PHONY: salmonella
|
||||
salmonella:
|
||||
rm -rf report
|
||||
rm -f toml*.so
|
||||
salmonella --keep-repo --repo-dir=./petri-dish; \
|
||||
salmonella-html-report ./salmonella.log report
|
||||
|
||||
# end
|
||||
|
|
105
tests/run.scm
105
tests/run.scm
|
@ -1,67 +1,52 @@
|
|||
(import (r7rs)
|
||||
(scheme base)
|
||||
(scheme write)
|
||||
(srfi 64)
|
||||
(srfi 152)
|
||||
(test)
|
||||
(rfc3339)
|
||||
(toml))
|
||||
|
||||
(define (tap-test-runner)
|
||||
(let ((runner (test-runner-null))
|
||||
(testcounter 0))
|
||||
(display "TAP version 13\n")
|
||||
(test-runner-on-test-end! runner
|
||||
(lambda (runner)
|
||||
(set! testcounter (+ testcounter 1))
|
||||
(display
|
||||
(string-append
|
||||
(if (test-passed? runner) "ok " "not ok ")
|
||||
(number->string testcounter) " - "
|
||||
(string-join (test-runner-group-path runner) " - ")
|
||||
" - " (test-runner-test-name runner)
|
||||
(if (eq? 'skip (test-result-kind runner)) "# SKIP" "")
|
||||
"\n"))))
|
||||
(test-runner-on-final! runner
|
||||
(lambda (runner)
|
||||
(display (string-append "1.." (number->string testcounter) "\n"))))
|
||||
runner))
|
||||
(test-group "Basic"
|
||||
(let ((tdat (table-from-file "basic.toml")))
|
||||
(test "7 Key-Value-Pairs"
|
||||
7 (toml-count-key-vals tdat))
|
||||
(test "Field name is TOML"
|
||||
"TOML" (toml-string tdat "name"))
|
||||
(test "Field language is Chicken Scheme"
|
||||
"Chicken Scheme" (toml-string tdat "language"))
|
||||
(test "has-bool is #t"
|
||||
#t (toml-bool tdat "has-bool"))
|
||||
(test "int is 5"
|
||||
5 (toml-int tdat "int"))
|
||||
(test "double is 10.8"
|
||||
10.8 (toml-double tdat "double"))
|
||||
(test "timestamp parsing"
|
||||
#(1979 05 27 07 32 00 0.0 0)
|
||||
(rfc3339->vector (toml-timestamp tdat "timestamp")))))
|
||||
|
||||
(test-runner-factory
|
||||
(lambda () (tap-test-runner)))
|
||||
(test-group "Table"
|
||||
(let ((tdat (table-from-file "table.toml")))
|
||||
(test "No top-level Key-Value-Pairs"
|
||||
0 (toml-count-key-vals tdat))
|
||||
(test "One top-level table"
|
||||
1 (toml-count-tables tdat))
|
||||
(let ((servertbl (toml-table tdat "server")))
|
||||
(test "\"server\" table has 2 Key-Value-Pairs"
|
||||
2 (toml-count-key-vals servertbl))
|
||||
(test "host is www.example.com"
|
||||
"www.example.com" (toml-string servertbl "host"))
|
||||
(test "timestamp parsing"
|
||||
#(2022 09 09 0 0 0 0.0 0)
|
||||
(rfc3339->vector (toml-timestamp servertbl "timestamp"))))))
|
||||
|
||||
(test-begin "Basic")
|
||||
(test-group "Array"
|
||||
(let* ((tdat (table-from-file "table.toml"))
|
||||
(tserv (toml-table tdat "server"))
|
||||
(tarr (toml-array tserv "port")))
|
||||
(test "There is one array"
|
||||
1 (toml-count-arrays tserv))
|
||||
(test "The array has three entries"
|
||||
3 (toml-count-entries tarr))
|
||||
(test "Element 0 is 8080"
|
||||
8080 (toml-int tarr 0))
|
||||
(test "Element 2 is 8282"
|
||||
8282 (toml-int tarr 2))))
|
||||
|
||||
(let ((tdat (table-from-file "basic.toml")))
|
||||
(test-equal 2 (toml-count-key-vals tdat))
|
||||
(test-equal "TOML" (toml-string tdat "name"))
|
||||
(test-equal "Chicken Scheme" (toml-string tdat "language"))
|
||||
(test-equal #t (toml-bool tdat "has-bool"))
|
||||
(test-equal 5 (toml-int tdat "int"))
|
||||
(test-equal 10.8 (toml-double tdat "double"))
|
||||
(test-equal (rfc3339->string (vector->rfc3339 #(1979 05 27 07 32 00 0 0)))
|
||||
(rfc3339->string (toml-timestamp tdat "timestamp"))))
|
||||
|
||||
(test-end "Basic")
|
||||
|
||||
(test-begin "Table")
|
||||
|
||||
(let ((tdat (table-from-file "table.toml")))
|
||||
(test-equal 0 (toml-count-key-vals tdat))
|
||||
(test-equal 1 (toml-count-tables tdat))
|
||||
(let ((servertbl (toml-table tdat "server")))
|
||||
(test-equal 1 (toml-count-key-vals servertbl))
|
||||
(test-equal "www.example.com" (toml-string servertbl "host"))))
|
||||
|
||||
(test-end "Table")
|
||||
|
||||
(test-begin "Array")
|
||||
|
||||
(let* ((tdat (table-from-file "table.toml"))
|
||||
(tserv (toml-table tdat "server"))
|
||||
(tarr (toml-array tserv "port")))
|
||||
(test-equal 1 (toml-count-arrays tserv))
|
||||
(test-equal 3 (toml-count-entries tarr))
|
||||
(test-equal 8080 (toml-int tarr 0))
|
||||
(test-equal 8282 (toml-int tarr 2)))
|
||||
|
||||
(test-end "Array")
|
||||
(test-exit)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
[server]
|
||||
host = "www.example.com"
|
||||
port = [ 8080, 8181, 8282 ]
|
||||
timestamp = 2022-09-09
|
||||
|
|
473
toml-impl.scm
473
toml-impl.scm
|
@ -1,239 +1,234 @@
|
|||
(import (chicken base)
|
||||
scheme
|
||||
(scheme base)
|
||||
(chicken foreign)
|
||||
(chicken memory)
|
||||
(chicken gc)
|
||||
(chicken format)
|
||||
rfc3339
|
||||
coops
|
||||
coops-primitive-objects)
|
||||
|
||||
(foreign-declare "#include <toml.h>")
|
||||
|
||||
(define (zeropad n)
|
||||
(if (< n 10)
|
||||
(sprintf "0~S" n)
|
||||
(sprintf "~S" n)))
|
||||
|
||||
(define (set-toml-table-finalizer ttable)
|
||||
(set-finalizer! ttable
|
||||
(lambda (obj)
|
||||
((foreign-lambda* void ((c-pointer ttp))
|
||||
"toml_free(ttp);")
|
||||
(ptr ttable)))))
|
||||
|
||||
(define-class <TomlArray> ()
|
||||
((ptr :accessor ptr :initform #f)))
|
||||
|
||||
(define-class <TomlTable> ()
|
||||
((ptr :accessor ptr :initform #f)))
|
||||
|
||||
|
||||
|
||||
(define-method (toml-self-key (tarr <TomlArray>))
|
||||
((foreign-lambda* c-string ((c-pointer tarr))
|
||||
"C_return(toml_array_key(tarr));")
|
||||
(ptr tarr)))
|
||||
|
||||
(define-method (toml-count-entries (tarr <TomlArray>))
|
||||
((foreign-lambda* int ((c-pointer tarr))
|
||||
"C_return(toml_array_nelem(tarr));")
|
||||
(ptr tarr)))
|
||||
|
||||
(define-method (toml-string (tarr <TomlArray>) (index <integer>))
|
||||
((foreign-primitive ((c-pointer tarr)
|
||||
(int index))
|
||||
"toml_datum_t datum = toml_string_at(tarr, index);"
|
||||
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(datum.u.s)));"
|
||||
"C_word data[3] = { C_SCHEME_UNDEFINED, C_k, C_string2(&s, datum.u.s) };"
|
||||
"free(datum.u.s);"
|
||||
"C_values(3, data);")
|
||||
(ptr tarr) index))
|
||||
|
||||
(define-method (toml-bool (tarr <TomlArray>) (index <integer>))
|
||||
((foreign-lambda* bool ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_bool_at(tarr, index).u.b);")
|
||||
(ptr tarr) index))
|
||||
|
||||
(define-method (toml-int (tarr <TomlArray>) (index <integer>))
|
||||
((foreign-lambda* int ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_int_at(tarr, index).u.i);")
|
||||
(ptr tarr) index))
|
||||
|
||||
(define-method (toml-double (tarr <TomlArray>) (index <integer>))
|
||||
((foreign-lambda* double ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_double_at(tarr, index).u.d);")
|
||||
(ptr tarr) index))
|
||||
|
||||
(define-method (toml-timestamp (tarr <TomlArray>) (index <integer>))
|
||||
(let*-values (((Y M D h m s millis z)
|
||||
((foreign-primitive ((c-pointer tarr)
|
||||
(int index))
|
||||
"toml_datum_t datum = toml_timestamp_at(tarr, index);"
|
||||
"toml_timestamp_t* stamp = datum.u.ts;"
|
||||
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(stamp->z)));"
|
||||
"C_word data[10] = { C_SCHEME_UNDEFINED, C_k, "
|
||||
"C_fix(stamp->year ? *stamp->year : 0), C_fix(stamp->month ? *stamp->month : 0), C_fix(stamp->day ? *stamp->day : 0), "
|
||||
"C_fix(stamp->hour ? *stamp->hour : 0), C_fix(stamp->minute ? *stamp->minute : 0),"
|
||||
"C_fix(stamp->second ? *stamp->second : 0), C_fix(stamp->millisec ? *stamp->second : 0), "
|
||||
"C_string2(&s, stamp->z ?: \"Z\") } ;"
|
||||
"free(datum.u.ts);"
|
||||
"C_values(10, data);")
|
||||
(ptr tarr) index))
|
||||
((rfcstr) (sprintf "~A-~A-~AT~A:~A:~A.~A~A"
|
||||
Y (zeropad M) (zeropad D)
|
||||
(zeropad h) (zeropad m) (zeropad s)
|
||||
millis z)))
|
||||
(string->rfc3339 rfcstr)))
|
||||
|
||||
(define-method (toml-array (tarr <TomlArray>) (index <integer>))
|
||||
(make <TomlArray> 'ptr
|
||||
((foreign-lambda* c-pointer ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_array_at(tarr, index));")
|
||||
(ptr tarr) index)))
|
||||
|
||||
(define-method (toml-table (tarr <TomlArray>) (index <integer>))
|
||||
(make <TomlTable> 'ptr
|
||||
((foreign-lambda* c-pointer ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_table_at(tarr, index));")
|
||||
(ptr tarr) index)))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(define (table-from-file filename)
|
||||
(let ((ttp ((foreign-lambda* c-pointer ((c-string fname))
|
||||
"FILE* fp = fopen(fname, \"r\");"
|
||||
"char errbuf[200];"
|
||||
"toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));"
|
||||
"fclose(fp);"
|
||||
"C_return(conf);")
|
||||
filename)))
|
||||
(when (not (eq? ttp 0))
|
||||
(let ((tomltable (make <TomlTable> 'ptr ttp)))
|
||||
(set-toml-table-finalizer tomltable)
|
||||
tomltable))))
|
||||
|
||||
(define (table-from-string str)
|
||||
(let ((ttp ((foreign-lambda* c-pointer ((c-string confdata))
|
||||
"char errbuf[200];"
|
||||
"toml_table_t* conf = toml_parse(confdata, errbuf, sizeof(errbuf));"
|
||||
"C_return(conf);")
|
||||
str)))
|
||||
(when (not (eq? ttp 0))
|
||||
(let ((tomltable (make <TomlTable> 'ptr ttp)))
|
||||
(set-toml-table-finalizer tomltable)
|
||||
tomltable))))
|
||||
|
||||
(define (set-toml-datum-string-finalizer tdatum)
|
||||
(set-finalizer! tdatum
|
||||
(lambda (obj)
|
||||
((foreign-lambda* void ((c-pointer tdat))
|
||||
"free(tdat);")
|
||||
(ptr tdatum)))))
|
||||
|
||||
(define-method (toml-self-key (ttbl <TomlTable>))
|
||||
((foreign-lambda* c-string ((c-pointer ttbl))
|
||||
"C_return(toml_table_key(ttbl));")
|
||||
(ptr ttbl)))
|
||||
|
||||
(define-method (toml-key-exists? (ttbl <TomlTable>) (key <string>))
|
||||
(= 1
|
||||
((foreign-lambda* int ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_key_exists(ttbl, key));")
|
||||
(ptr ttbl) key)))
|
||||
|
||||
(define-method (toml-count-key-vals (ttbl <TomlTable>))
|
||||
((foreign-lambda* int ((c-pointer ttbl))
|
||||
"C_return(toml_table_nkval(ttbl));")
|
||||
(ptr ttbl)))
|
||||
|
||||
(define-method (toml-count-arrays (ttbl <TomlTable>))
|
||||
((foreign-lambda* int ((c-pointer ttbl))
|
||||
"C_return(toml_table_narr(ttbl));")
|
||||
(ptr ttbl)))
|
||||
|
||||
(define-method (toml-count-tables (ttbl <TomlTable>))
|
||||
((foreign-lambda* int ((c-pointer ttbl))
|
||||
"C_return(toml_table_ntab(ttbl));")
|
||||
(ptr ttbl)))
|
||||
|
||||
(define-method (toml-key-at (ttbl <TomlTable>) (index <integer>))
|
||||
((foreign-lambda* c-string ((c-pointer ttbl)
|
||||
(int index))
|
||||
"C_return(toml_key_in(ttbl, index));")
|
||||
(ptr ttbl) index))
|
||||
|
||||
(define-method (toml-string (ttbl <TomlTable>) (key <string>))
|
||||
((foreign-primitive ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"toml_datum_t datum = toml_string_in(ttbl, key);"
|
||||
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(datum.u.s)));"
|
||||
"C_word data[3] = { C_SCHEME_UNDEFINED, C_k, C_string2(&s, datum.u.s) };"
|
||||
"free(datum.u.s);"
|
||||
"C_values(3, data);")
|
||||
(ptr ttbl) key))
|
||||
|
||||
(define-method (toml-bool (ttbl <TomlTable>) (key <string>))
|
||||
((foreign-lambda* bool ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_bool_in(ttbl, key).u.b);")
|
||||
(ptr ttbl) key))
|
||||
|
||||
(define-method (toml-int (ttbl <TomlTable>) (key <string>))
|
||||
((foreign-lambda* int ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_int_in(ttbl, key).u.i);")
|
||||
(ptr ttbl) key))
|
||||
|
||||
(define-method (toml-double (ttbl <TomlTable>) (key <string>))
|
||||
((foreign-lambda* double ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_double_in(ttbl, key).u.d);")
|
||||
(ptr ttbl) key))
|
||||
|
||||
(define-method (toml-timestamp (ttbl <TomlTable>) (key <string>))
|
||||
(let*-values (((Y M D h m s millis z)
|
||||
((foreign-primitive ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"toml_datum_t datum = toml_timestamp_in(ttbl, key);"
|
||||
"toml_timestamp_t* stamp = datum.u.ts;"
|
||||
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(stamp->z)));"
|
||||
"C_word data[10] = { C_SCHEME_UNDEFINED, C_k, "
|
||||
"C_fix(stamp->year ? *stamp->year : 0), C_fix(stamp->month ? *stamp->month : 0), C_fix(stamp->day ? *stamp->day : 0), "
|
||||
"C_fix(stamp->hour ? *stamp->hour : 0), C_fix(stamp->minute ? *stamp->minute : 0),"
|
||||
"C_fix(stamp->second ? *stamp->second : 0), C_fix(stamp->millisec ? *stamp->second : 0), "
|
||||
"C_string2(&s, stamp->z ?: \"Z\") } ;"
|
||||
"free(datum.u.ts);"
|
||||
"C_values(10, data);")
|
||||
(ptr ttbl) key))
|
||||
((rfcstr) (sprintf "~A-~A-~AT~A:~A:~A.~A~A"
|
||||
Y (zeropad M) (zeropad D)
|
||||
(zeropad h) (zeropad m) (zeropad s)
|
||||
millis z)))
|
||||
(string->rfc3339 rfcstr)))
|
||||
|
||||
(define-method (toml-array (ttbl <TomlTable>) (key <string>))
|
||||
(make <TomlArray> 'ptr
|
||||
((foreign-lambda* c-pointer ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_array_in(ttbl, key));")
|
||||
(ptr ttbl) key)))
|
||||
|
||||
(define-method (toml-table (ttbl <TomlTable>) (key <string>))
|
||||
(make <TomlTable> 'ptr
|
||||
((foreign-lambda* c-pointer ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_table_in(ttbl, key));")
|
||||
(ptr ttbl) key)))
|
||||
(import (chicken base)
|
||||
scheme
|
||||
(scheme base)
|
||||
(chicken foreign)
|
||||
(chicken memory)
|
||||
(chicken gc)
|
||||
(chicken format)
|
||||
rfc3339
|
||||
coops
|
||||
coops-primitive-objects)
|
||||
|
||||
(foreign-declare "#include <toml.h>")
|
||||
|
||||
(define (zeropad n)
|
||||
(if (< n 10)
|
||||
(sprintf "0~S" n)
|
||||
(sprintf "~S" n)))
|
||||
|
||||
(define (set-toml-table-finalizer ttable)
|
||||
(set-finalizer! ttable
|
||||
(lambda (obj)
|
||||
((foreign-lambda* void ((c-pointer ttp))
|
||||
"toml_free(ttp);")
|
||||
(ptr ttable)))))
|
||||
|
||||
(define-class <TomlArray> ()
|
||||
((ptr :accessor ptr :initform #f)))
|
||||
|
||||
(define-class <TomlTable> ()
|
||||
((ptr :accessor ptr :initform #f)))
|
||||
|
||||
|
||||
|
||||
(define-method (toml-self-key (tarr <TomlArray>))
|
||||
((foreign-lambda* c-string ((c-pointer tarr))
|
||||
"C_return(toml_array_key(tarr));")
|
||||
(ptr tarr)))
|
||||
|
||||
(define-method (toml-count-entries (tarr <TomlArray>))
|
||||
((foreign-lambda* int ((c-pointer tarr))
|
||||
"C_return(toml_array_nelem(tarr));")
|
||||
(ptr tarr)))
|
||||
|
||||
(define-method (toml-string (tarr <TomlArray>) (index <integer>))
|
||||
((foreign-primitive ((c-pointer tarr)
|
||||
(int index))
|
||||
"toml_datum_t datum = toml_string_at(tarr, index);"
|
||||
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(datum.u.s)));"
|
||||
"C_word data[3] = { C_SCHEME_UNDEFINED, C_k, C_string2(&s, datum.u.s) };"
|
||||
"free(datum.u.s);"
|
||||
"C_values(3, data);")
|
||||
(ptr tarr) index))
|
||||
|
||||
(define-method (toml-bool (tarr <TomlArray>) (index <integer>))
|
||||
((foreign-lambda* bool ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_bool_at(tarr, index).u.b);")
|
||||
(ptr tarr) index))
|
||||
|
||||
(define-method (toml-int (tarr <TomlArray>) (index <integer>))
|
||||
((foreign-lambda* int ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_int_at(tarr, index).u.i);")
|
||||
(ptr tarr) index))
|
||||
|
||||
(define-method (toml-double (tarr <TomlArray>) (index <integer>))
|
||||
((foreign-lambda* double ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_double_at(tarr, index).u.d);")
|
||||
(ptr tarr) index))
|
||||
|
||||
(define-method (toml-timestamp (tarr <TomlArray>) (index <integer>))
|
||||
(let*-values (((Y M D h m s millis z)
|
||||
((foreign-primitive ((c-pointer tarr)
|
||||
(int index))
|
||||
"toml_datum_t datum = toml_timestamp_at(tarr, index);"
|
||||
"toml_timestamp_t* stamp = datum.u.ts;"
|
||||
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(stamp->z ?: \"Z\")));"
|
||||
"C_word data[10] = { C_SCHEME_UNDEFINED, C_k, "
|
||||
"C_fix(stamp->year ? *stamp->year : 0), C_fix(stamp->month ? *stamp->month : 0), C_fix(stamp->day ? *stamp->day : 0), "
|
||||
"C_fix(stamp->hour ? *stamp->hour : 0), C_fix(stamp->minute ? *stamp->minute : 0),"
|
||||
"C_fix(stamp->second ? *stamp->second : 0), C_fix(stamp->millisec ? *stamp->second : 0), "
|
||||
"C_string2(&s, stamp->z ?: \"Z\") } ;"
|
||||
"free(datum.u.ts);"
|
||||
"C_values(10, data);")
|
||||
(ptr tarr) index))
|
||||
((rfcstr) (sprintf "~A-~A-~AT~A:~A:~A.~A~A"
|
||||
Y (zeropad M) (zeropad D)
|
||||
(zeropad h) (zeropad m) (zeropad s)
|
||||
millis z)))
|
||||
(string->rfc3339 rfcstr)))
|
||||
|
||||
(define-method (toml-array (tarr <TomlArray>) (index <integer>))
|
||||
(make <TomlArray> 'ptr
|
||||
((foreign-lambda* c-pointer ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_array_at(tarr, index));")
|
||||
(ptr tarr) index)))
|
||||
|
||||
(define-method (toml-table (tarr <TomlArray>) (index <integer>))
|
||||
(make <TomlTable> 'ptr
|
||||
((foreign-lambda* c-pointer ((c-pointer tarr)
|
||||
(int index))
|
||||
"C_return(toml_table_at(tarr, index));")
|
||||
(ptr tarr) index)))
|
||||
|
||||
|
||||
|
||||
(define (table-from-file filename)
|
||||
(let ((ttp ((foreign-lambda* c-pointer ((c-string fname))
|
||||
"FILE* fp = fopen(fname, \"r\");"
|
||||
"char errbuf[200];"
|
||||
"toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));"
|
||||
"fclose(fp);"
|
||||
"C_return(conf);")
|
||||
filename)))
|
||||
(when (not (eq? ttp 0))
|
||||
(let ((tomltable (make <TomlTable> 'ptr ttp)))
|
||||
(set-toml-table-finalizer tomltable)
|
||||
tomltable))))
|
||||
|
||||
(define (table-from-string str)
|
||||
(let ((ttp ((foreign-lambda* c-pointer ((c-string confdata))
|
||||
"char errbuf[200];"
|
||||
"toml_table_t* conf = toml_parse(confdata, errbuf, sizeof(errbuf));"
|
||||
"C_return(conf);")
|
||||
str)))
|
||||
(when (not (eq? ttp 0))
|
||||
(let ((tomltable (make <TomlTable> 'ptr ttp)))
|
||||
(set-toml-table-finalizer tomltable)
|
||||
tomltable))))
|
||||
|
||||
(define (set-toml-datum-string-finalizer tdatum)
|
||||
(set-finalizer! tdatum
|
||||
(lambda (obj)
|
||||
((foreign-lambda* void ((c-pointer tdat))
|
||||
"free(tdat);")
|
||||
(ptr tdatum)))))
|
||||
|
||||
(define-method (toml-self-key (ttbl <TomlTable>))
|
||||
((foreign-lambda* c-string ((c-pointer ttbl))
|
||||
"C_return(toml_table_key(ttbl));")
|
||||
(ptr ttbl)))
|
||||
|
||||
(define-method (toml-key-exists? (ttbl <TomlTable>) (key <string>))
|
||||
(= 1
|
||||
((foreign-lambda* int ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_key_exists(ttbl, key));")
|
||||
(ptr ttbl) key)))
|
||||
|
||||
(define-method (toml-count-key-vals (ttbl <TomlTable>))
|
||||
((foreign-lambda* int ((c-pointer ttbl))
|
||||
"C_return(toml_table_nkval(ttbl));")
|
||||
(ptr ttbl)))
|
||||
|
||||
(define-method (toml-count-arrays (ttbl <TomlTable>))
|
||||
((foreign-lambda* int ((c-pointer ttbl))
|
||||
"C_return(toml_table_narr(ttbl));")
|
||||
(ptr ttbl)))
|
||||
|
||||
(define-method (toml-count-tables (ttbl <TomlTable>))
|
||||
((foreign-lambda* int ((c-pointer ttbl))
|
||||
"C_return(toml_table_ntab(ttbl));")
|
||||
(ptr ttbl)))
|
||||
|
||||
(define-method (toml-key-at (ttbl <TomlTable>) (index <integer>))
|
||||
((foreign-lambda* c-string ((c-pointer ttbl)
|
||||
(int index))
|
||||
"C_return(toml_key_in(ttbl, index));")
|
||||
(ptr ttbl) index))
|
||||
|
||||
(define-method (toml-string (ttbl <TomlTable>) (key <string>))
|
||||
((foreign-primitive ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"toml_datum_t datum = toml_string_in(ttbl, key);"
|
||||
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(datum.u.s)));"
|
||||
"C_word data[3] = { C_SCHEME_UNDEFINED, C_k, C_string2(&s, datum.u.s) };"
|
||||
"free(datum.u.s);"
|
||||
"C_values(3, data);")
|
||||
(ptr ttbl) key))
|
||||
|
||||
(define-method (toml-bool (ttbl <TomlTable>) (key <string>))
|
||||
((foreign-lambda* bool ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_bool_in(ttbl, key).u.b);")
|
||||
(ptr ttbl) key))
|
||||
|
||||
(define-method (toml-int (ttbl <TomlTable>) (key <string>))
|
||||
((foreign-lambda* int ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_int_in(ttbl, key).u.i);")
|
||||
(ptr ttbl) key))
|
||||
|
||||
(define-method (toml-double (ttbl <TomlTable>) (key <string>))
|
||||
((foreign-lambda* double ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_double_in(ttbl, key).u.d);")
|
||||
(ptr ttbl) key))
|
||||
|
||||
(define-method (toml-timestamp (ttbl <TomlTable>) (key <string>))
|
||||
(let*-values (((Y M D h m s millis z)
|
||||
((foreign-primitive ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"toml_datum_t datum = toml_timestamp_in(ttbl, key);"
|
||||
"toml_timestamp_t* stamp = datum.u.ts;"
|
||||
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(stamp->z ?: \"Z\")));"
|
||||
"C_word data[10] = { C_SCHEME_UNDEFINED, C_k, "
|
||||
"C_fix(stamp->year ? *stamp->year : 0), C_fix(stamp->month ? *stamp->month : 0), C_fix(stamp->day ? *stamp->day : 0), "
|
||||
"C_fix(stamp->hour ? *stamp->hour : 0), C_fix(stamp->minute ? *stamp->minute : 0),"
|
||||
"C_fix(stamp->second ? *stamp->second : 0), C_fix(stamp->millisec ? *stamp->second : 0), "
|
||||
"C_string2(&s, stamp->z ?: \"Z\") } ;"
|
||||
"free(datum.u.ts);"
|
||||
"C_values(10, data);")
|
||||
(ptr ttbl) key))
|
||||
((rfcstr) (sprintf "~A-~A-~AT~A:~A:~A.~A~A"
|
||||
Y (zeropad M) (zeropad D)
|
||||
(zeropad h) (zeropad m) (zeropad s)
|
||||
millis z)))
|
||||
(string->rfc3339 rfcstr)))
|
||||
|
||||
(define-method (toml-array (ttbl <TomlTable>) (key <string>))
|
||||
(make <TomlArray> 'ptr
|
||||
((foreign-lambda* c-pointer ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_array_in(ttbl, key));")
|
||||
(ptr ttbl) key)))
|
||||
|
||||
(define-method (toml-table (ttbl <TomlTable>) (key <string>))
|
||||
(make <TomlTable> 'ptr
|
||||
((foreign-lambda* c-pointer ((c-pointer ttbl)
|
||||
(c-string key))
|
||||
"C_return(toml_table_in(ttbl, key));")
|
||||
(ptr ttbl) key)))
|
||||
|
|
32
toml.egg
32
toml.egg
|
@ -1,16 +1,16 @@
|
|||
;; -*- mode: scheme -*-
|
||||
((author "Daniel Ziltener")
|
||||
(synopsis "A Chicken binding to read TOML configuration files")
|
||||
(category parsing)
|
||||
(license "MIT")
|
||||
(version "0.6")
|
||||
(dependencies r7rs rfc3339 coops)
|
||||
(test-dependencies srfi-64 srfi-152)
|
||||
|
||||
(components
|
||||
(c-object tomlc99/toml
|
||||
(source "tomlc99/toml.c"))
|
||||
(extension toml
|
||||
(objects tomlc99/toml)
|
||||
(csc-options "-X" "r7rs" "-R" "r7rs" "-K" "prefix" "-sJ"
|
||||
"-Itomlc99"))))
|
||||
;; -*- mode: scheme -*-
|
||||
((author "Daniel Ziltener")
|
||||
(synopsis "A Chicken binding to read TOML configuration files")
|
||||
(category parsing)
|
||||
(license "MIT")
|
||||
(version "0.8")
|
||||
(dependencies r7rs rfc3339 coops)
|
||||
(test-dependencies test)
|
||||
|
||||
(components
|
||||
(c-object tomlc99/toml
|
||||
(source "tomlc99/toml.c"))
|
||||
(extension toml
|
||||
(objects tomlc99/toml)
|
||||
(csc-options "-X" "r7rs" "-R" "r7rs" "-K" "prefix" "-sJ"
|
||||
"-Itomlc99"))))
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
(repo git "https://gitea.lyrion.ch/zilti/toml.git")
|
||||
(uri targz "https://gitea.lyrion.ch/zilti/toml/archive/{egg-release}.tar.gz")
|
||||
(release "0.6")
|
||||
;; -*- Scheme -*-
|
||||
(repo git "https://gitea.lyrion.ch/Chicken/toml.git")
|
||||
(uri targz "https://gitea.lyrion.ch/Chicken/toml/archive/{egg-release}.tar.gz")
|
||||
(release "0.8")
|
||||
(release "0.7")
|
||||
(release "0.6")
|
||||
|
|
241
toml.wiki
241
toml.wiki
|
@ -1,118 +1,123 @@
|
|||
[[tags: egg]]
|
||||
[[toc:]]
|
||||
== TOML
|
||||
A Chicken wrapper for the TOML configuration language
|
||||
|
||||
=== Requirements
|
||||
|
||||
[[/eggref/5/r7rs|r7rs]]
|
||||
[[/eggref/5/rfc3339|rfc3339]]
|
||||
[[/eggref/5/coops|coops]]
|
||||
|
||||
=== Usage
|
||||
|
||||
<enscript language=scheme>
|
||||
(import toml)
|
||||
</enscript>
|
||||
|
||||
=== Loading TOML configuration
|
||||
|
||||
<syntax>(table-from-file FILENAME) --> <TomlTable></syntax>
|
||||
|
||||
Loads {{FILENAME}} contents as a TOML configuration.
|
||||
|
||||
<syntax>(table-from-string STRING) --> <TomlTable></syntax>
|
||||
|
||||
Loads the contents of {{STRING}} as TOML configuration.
|
||||
|
||||
=== Tables
|
||||
|
||||
<syntax>(toml-self-key TOMLTABLE) --> string</syntax>
|
||||
|
||||
Returns the key, if any, to which {{TOMLTABLE}} is assigned.
|
||||
|
||||
<syntax>(toml-key-exists? TOMLTABLE KEY) --> bool</syntax>
|
||||
|
||||
Checks if {{KEY}} exists in {{TOMLTABLE}}.
|
||||
|
||||
<syntax>(toml-count-key-vals TOMLTABLE) --> int</syntax>
|
||||
<syntax>(toml-count-arrays TOMLTABLE) --> int</syntax>
|
||||
<syntax>(toml-count-tables TOMLTABLE) --> int</syntax>
|
||||
|
||||
Returns the number of key-value entries, arrays, or tables respectively in {{TOMLTABLE}}.
|
||||
|
||||
<syntax>(toml-key-at TOMLTABLE INDEX) --> string</syntax>
|
||||
|
||||
Returns the table key at position {{INDEX}} in {{TOMLTABLE}}.
|
||||
|
||||
<syntax>(toml-string TOMLTABLE KEY) --> string</syntax>
|
||||
<syntax>(toml-bool TOMLTABLE KEY) --> bool</syntax>
|
||||
<syntax>(toml-int TOMLTABLE KEY) --> int</syntax>
|
||||
<syntax>(toml-double TOMLTABLE KEY) --> double</syntax>
|
||||
<syntax>(toml-timestamp TOMLTABLE KEY) --> rfc3339</syntax>
|
||||
<syntax>(toml-array TOMLTABLE KEY) --> <TomlArray></syntax>
|
||||
<syntax>(toml-table TOMLTABLE KEY) --> <TomlTable></syntax>
|
||||
|
||||
Returns the element of the given type in {{TOMLTABLE}} at {{KEY}}.
|
||||
|
||||
=== Arrays
|
||||
|
||||
<syntax>(toml-self-key TOMLARRAY) --> string</syntax>
|
||||
|
||||
Returns the key, if any, to which {{TOMLARRAY}} is assigned.
|
||||
|
||||
<syntax>(toml-count-entries TOMLARRAY) --> int</syntax>
|
||||
|
||||
Returns the number of entries in {{TOMLARRAY}}.
|
||||
|
||||
<syntax>(toml-string TOMLARRAY KEY) --> string</syntax>
|
||||
<syntax>(toml-bool TOMLARRAY KEY) --> bool</syntax>
|
||||
<syntax>(toml-int TOMLARRAY KEY) --> int</syntax>
|
||||
<syntax>(toml-double TOMLARRAY KEY) --> double</syntax>
|
||||
<syntax>(toml-timestamp TOMLARRAY KEY) --> rfc3339</syntax>
|
||||
<syntax>(toml-array TOMLARRAY KEY) --> <TomlArray></syntax>
|
||||
<syntax>(toml-table TOMLARRAY KEY) --> <TomlTable></syntax>
|
||||
|
||||
Returns the element of the given type in {{TOMLARRAY}} at {{KEY}}.
|
||||
|
||||
== About this egg
|
||||
|
||||
=== Authors
|
||||
Daniel Ziltener
|
||||
CK Tan
|
||||
|
||||
=== Repository
|
||||
|
||||
The repository of the Chicken wrapper can be found at [[https://gitea.lyrion.ch/zilti/toml|https://gitea.lyrion.ch/zilti/toml]].
|
||||
|
||||
The repository of the C implementation being wrapped can be found at [[https://github.com/cktan/tomlc99|https://github.com/cktan/tomlc99]].
|
||||
|
||||
=== Version History
|
||||
; 0.6 : first version of the wrapper
|
||||
|
||||
=== License
|
||||
MIT License
|
||||
|
||||
Copyright (c) Daniel Ziltener
|
||||
https://gitea.lyrion.ch/zilti/toml
|
||||
|
||||
Copyright (c) CK Tan
|
||||
https://github.com/cktan/tomlc99
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
[[tags: egg]]
|
||||
[[toc:]]
|
||||
== TOML
|
||||
A Chicken wrapper for the TOML configuration language
|
||||
|
||||
=== Requirements
|
||||
|
||||
[[/eggref/5/r7rs|r7rs]]
|
||||
[[/eggref/5/rfc3339|rfc3339]]
|
||||
[[/eggref/5/coops|coops]]
|
||||
|
||||
=== Usage
|
||||
|
||||
<enscript language=scheme>
|
||||
(import toml)
|
||||
</enscript>
|
||||
|
||||
=== Loading TOML configuration
|
||||
|
||||
<syntax>(table-from-file FILENAME) --> <TomlTable></syntax>
|
||||
|
||||
Loads {{FILENAME}} contents as a TOML configuration.
|
||||
|
||||
<syntax>(table-from-string STRING) --> <TomlTable></syntax>
|
||||
|
||||
Loads the contents of {{STRING}} as TOML configuration.
|
||||
|
||||
=== Tables
|
||||
|
||||
<syntax>(toml-self-key TOMLTABLE) --> string</syntax>
|
||||
|
||||
Returns the key, if any, to which {{TOMLTABLE}} is assigned.
|
||||
|
||||
<syntax>(toml-key-exists? TOMLTABLE KEY) --> bool</syntax>
|
||||
|
||||
Checks if {{KEY}} exists in {{TOMLTABLE}}.
|
||||
|
||||
<syntax>(toml-count-key-vals TOMLTABLE) --> int</syntax>
|
||||
<syntax>(toml-count-arrays TOMLTABLE) --> int</syntax>
|
||||
<syntax>(toml-count-tables TOMLTABLE) --> int</syntax>
|
||||
|
||||
Returns the number of key-value entries, arrays, or tables respectively in {{TOMLTABLE}}.
|
||||
|
||||
<syntax>(toml-key-at TOMLTABLE INDEX) --> string</syntax>
|
||||
|
||||
Returns the table key at position {{INDEX}} in {{TOMLTABLE}}.
|
||||
|
||||
<syntax>(toml-string TOMLTABLE KEY) --> string</syntax>
|
||||
<syntax>(toml-bool TOMLTABLE KEY) --> bool</syntax>
|
||||
<syntax>(toml-int TOMLTABLE KEY) --> int</syntax>
|
||||
<syntax>(toml-double TOMLTABLE KEY) --> double</syntax>
|
||||
<syntax>(toml-timestamp TOMLTABLE KEY) --> rfc3339</syntax>
|
||||
<syntax>(toml-array TOMLTABLE KEY) --> <TomlArray></syntax>
|
||||
<syntax>(toml-table TOMLTABLE KEY) --> <TomlTable></syntax>
|
||||
|
||||
Returns the element of the given type in {{TOMLTABLE}} at {{KEY}}.
|
||||
|
||||
=== Arrays
|
||||
|
||||
<syntax>(toml-self-key TOMLARRAY) --> string</syntax>
|
||||
|
||||
Returns the key, if any, to which {{TOMLARRAY}} is assigned.
|
||||
|
||||
<syntax>(toml-count-entries TOMLARRAY) --> int</syntax>
|
||||
|
||||
Returns the number of entries in {{TOMLARRAY}}.
|
||||
|
||||
<syntax>(toml-string TOMLARRAY KEY) --> string</syntax>
|
||||
<syntax>(toml-bool TOMLARRAY KEY) --> bool</syntax>
|
||||
<syntax>(toml-int TOMLARRAY KEY) --> int</syntax>
|
||||
<syntax>(toml-double TOMLARRAY KEY) --> double</syntax>
|
||||
<syntax>(toml-timestamp TOMLARRAY KEY) --> rfc3339</syntax>
|
||||
<syntax>(toml-array TOMLARRAY KEY) --> <TomlArray></syntax>
|
||||
<syntax>(toml-table TOMLARRAY KEY) --> <TomlTable></syntax>
|
||||
|
||||
Returns the element of the given type in {{TOMLARRAY}} at {{KEY}}.
|
||||
|
||||
== About this egg
|
||||
|
||||
=== Authors
|
||||
Daniel Ziltener
|
||||
CK Tan
|
||||
|
||||
=== Repository
|
||||
|
||||
The repository of the Chicken wrapper can be found at [[https://gitea.lyrion.ch/Chicken/toml|https://gitea.lyrion.ch/Chicken/toml]].
|
||||
|
||||
The repository of the C implementation being wrapped can be found at [[https://github.com/cktan/tomlc99|https://github.com/cktan/tomlc99]].
|
||||
|
||||
=== Version History
|
||||
|
||||
; 0.8 : updated the tomlc99 dependency
|
||||
|
||||
; 0.7 : fixed an issue with timestamp parsing
|
||||
|
||||
; 0.6 : first version of the wrapper
|
||||
|
||||
=== License
|
||||
MIT License
|
||||
|
||||
Copyright (c) Daniel Ziltener
|
||||
https://gitea.lyrion.ch/zilti/toml
|
||||
|
||||
Copyright (c) CK Tan
|
||||
https://github.com/cktan/tomlc99
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
|
|
@ -37,8 +37,11 @@ install: all
|
|||
install $(LIB) ${prefix}/lib
|
||||
install $(LIB_SHARED) ${prefix}/lib
|
||||
ifeq "$(prefix)" "/usr/local"
|
||||
ifneq ("$(wildcard $(PCFILE))","")
|
||||
install $(PCFILE) /usr/local/lib/pkgconfig
|
||||
endif
|
||||
endif
|
||||
|
||||
|
||||
clean:
|
||||
rm -f *.o $(EXEC) $(LIB) $(LIB_SHARED)
|
||||
|
|
|
@ -6,7 +6,7 @@ If you are looking for a C++ library, you might try this wrapper: [https://githu
|
|||
|
||||
* Compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
|
||||
* Tested with multiple test suites, including
|
||||
[BurntSushi/toml-test](https://github.com/BurntSushi/toml-test) and
|
||||
[toml-lang/toml-test](https://github.com/toml-lang/toml-test) and
|
||||
[iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests).
|
||||
* Provides very simple and intuitive interface.
|
||||
|
||||
|
@ -174,7 +174,7 @@ Alternatively, specify `make install prefix=/a/file/path` to install into
|
|||
|
||||
## Testing
|
||||
|
||||
To test against the standard test set provided by BurntSushi/toml-test:
|
||||
To test against the standard test set provided by toml-lang/toml-test:
|
||||
|
||||
```sh
|
||||
% make
|
||||
|
@ -191,4 +191,4 @@ To test against the standard test set provided by iarna/toml:
|
|||
% cd test2
|
||||
% bash build.sh # do this once
|
||||
% bash run.sh # this will run the test suite
|
||||
```
|
||||
```
|
|
@ -1,9 +1,6 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
mkdir -p $DIR/goworkspace
|
||||
export GOPATH=$DIR/goworkspace
|
||||
go get github.com/BurntSushi/toml-test@latest # install test suite
|
||||
go install github.com/BurntSushi/toml/cmd/toml-test-decoder@latest # e.g., install my parser
|
||||
cp $GOPATH/bin/* .
|
||||
|
||||
export GOBIN=$DIR
|
||||
go install github.com/toml-lang/toml-test/cmd/toml-test@latest # install test suite
|
|
@ -1,5 +1,5 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
rm -f tests
|
||||
ln -s ./goworkspace/pkg/mod/github.com/\!burnt\!sushi/toml-test@v0.1.0/tests
|
||||
./toml-test ../toml_json
|
||||
$DIR/toml-test $DIR/../toml_json
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
[ -d toml-spec-tests ] || git clone https://github.com/cktan/toml-spec-tests.git
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
if ! (which jq >& /dev/null); then
|
||||
#!/usr/bin/env bash
|
||||
|
||||
if ! (which jq >& /dev/null); then
|
||||
echo "ERROR: please install the 'jq' utility"
|
||||
exit 1
|
||||
fi
|
||||
|
@ -12,11 +14,11 @@ for i in toml-spec-tests/values/*.toml; do
|
|||
fname="${fname%.*}"
|
||||
echo -n $fname ' '
|
||||
res='[OK]'
|
||||
if (../toml_json $fname.toml >& $fname.json.out); then
|
||||
if (../toml_json $fname.toml >& $fname.json.out); then
|
||||
jq -S . $fname.json.out > t.json
|
||||
mv t.json $fname.json.out
|
||||
if [ -f $fname.json ]; then
|
||||
if ! (diff $fname.json $fname.json.out >& /dev/null); then
|
||||
if ! (diff $fname.json $fname.json.out >& /dev/null); then
|
||||
res='[FAILED]'
|
||||
else
|
||||
rm -f $fname.json.out
|
||||
|
@ -32,10 +34,10 @@ done
|
|||
#
|
||||
# NEGATIVE tests
|
||||
#
|
||||
for i in toml-spec-tests/errors/*.toml; do
|
||||
for i in toml-spec-tests/errors/*.toml; do
|
||||
echo -n $i ' '
|
||||
res='[OK]'
|
||||
if (../toml_json $i >& $i.json.out); then
|
||||
if (../toml_json $i >& $i.json.out); then
|
||||
res='[FAILED]'
|
||||
fi
|
||||
echo ... $res
|
||||
|
|
|
@ -45,6 +45,7 @@ void toml_set_memutil(void *(*xxmalloc)(size_t), void (*xxfree)(void *)) {
|
|||
ppfree = xxfree;
|
||||
}
|
||||
|
||||
#define ALIGN8(sz) (((sz) + 7) & ~7)
|
||||
#define MALLOC(a) ppmalloc(a)
|
||||
#define FREE(a) ppfree(a)
|
||||
|
||||
|
@ -53,7 +54,7 @@ void toml_set_memutil(void *(*xxmalloc)(size_t), void (*xxfree)(void *)) {
|
|||
#define calloc(x, y) error - forbidden - use CALLOC instead
|
||||
|
||||
static void *CALLOC(size_t nmemb, size_t sz) {
|
||||
int nb = sz * nmemb;
|
||||
int nb = ALIGN8(sz) * nmemb;
|
||||
void *p = MALLOC(nb);
|
||||
if (p) {
|
||||
memset(p, 0, nb);
|
||||
|
@ -220,8 +221,8 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||
110xxxxx 10xxxxxx
|
||||
*/
|
||||
if (code <= 0x000007FF) {
|
||||
buf[0] = (unsigned char) (0xc0 | (code >> 6));
|
||||
buf[1] = (unsigned char) (0x80 | (code & 0x3f));
|
||||
buf[0] = (unsigned char)(0xc0 | (code >> 6));
|
||||
buf[1] = (unsigned char)(0x80 | (code & 0x3f));
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
@ -229,9 +230,9 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||
1110xxxx 10xxxxxx 10xxxxxx
|
||||
*/
|
||||
if (code <= 0x0000FFFF) {
|
||||
buf[0] = (unsigned char) (0xe0 | (code >> 12));
|
||||
buf[1] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
|
||||
buf[2] = (unsigned char) (0x80 | (code & 0x3f));
|
||||
buf[0] = (unsigned char)(0xe0 | (code >> 12));
|
||||
buf[1] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
|
||||
buf[2] = (unsigned char)(0x80 | (code & 0x3f));
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
@ -239,10 +240,10 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
*/
|
||||
if (code <= 0x001FFFFF) {
|
||||
buf[0] = (unsigned char) (0xf0 | (code >> 18));
|
||||
buf[1] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
|
||||
buf[2] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
|
||||
buf[3] = (unsigned char) (0x80 | (code & 0x3f));
|
||||
buf[0] = (unsigned char)(0xf0 | (code >> 18));
|
||||
buf[1] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
|
||||
buf[2] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
|
||||
buf[3] = (unsigned char)(0x80 | (code & 0x3f));
|
||||
return 4;
|
||||
}
|
||||
|
||||
|
@ -250,11 +251,11 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
*/
|
||||
if (code <= 0x03FFFFFF) {
|
||||
buf[0] = (unsigned char) (0xf8 | (code >> 24));
|
||||
buf[1] = (unsigned char) (0x80 | ((code >> 18) & 0x3f));
|
||||
buf[2] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
|
||||
buf[3] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
|
||||
buf[4] = (unsigned char) (0x80 | (code & 0x3f));
|
||||
buf[0] = (unsigned char)(0xf8 | (code >> 24));
|
||||
buf[1] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
|
||||
buf[2] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
|
||||
buf[3] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
|
||||
buf[4] = (unsigned char)(0x80 | (code & 0x3f));
|
||||
return 5;
|
||||
}
|
||||
|
||||
|
@ -262,12 +263,12 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
*/
|
||||
if (code <= 0x7FFFFFFF) {
|
||||
buf[0] = (unsigned char) (0xfc | (code >> 30));
|
||||
buf[1] = (unsigned char) (0x80 | ((code >> 24) & 0x3f));
|
||||
buf[2] = (unsigned char) (0x80 | ((code >> 18) & 0x3f));
|
||||
buf[3] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
|
||||
buf[4] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
|
||||
buf[5] = (unsigned char) (0x80 | (code & 0x3f));
|
||||
buf[0] = (unsigned char)(0xfc | (code >> 30));
|
||||
buf[1] = (unsigned char)(0x80 | ((code >> 24) & 0x3f));
|
||||
buf[2] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
|
||||
buf[3] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
|
||||
buf[4] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
|
||||
buf[5] = (unsigned char)(0x80 | (code & 0x3f));
|
||||
return 6;
|
||||
}
|
||||
|
||||
|
@ -411,8 +412,10 @@ static void *expand(void *p, int sz, int newsz) {
|
|||
if (!s)
|
||||
return 0;
|
||||
|
||||
memcpy(s, p, sz);
|
||||
FREE(p);
|
||||
if (p) {
|
||||
memcpy(s, p, sz);
|
||||
FREE(p);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
@ -422,8 +425,10 @@ static void **expand_ptrarr(void **p, int n) {
|
|||
return 0;
|
||||
|
||||
s[n] = 0;
|
||||
memcpy(s, p, n * sizeof(void *));
|
||||
FREE(p);
|
||||
if (p) {
|
||||
memcpy(s, p, n * sizeof(void *));
|
||||
FREE(p);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
@ -2211,6 +2216,7 @@ int toml_rtos(toml_raw_t src, char **ret) {
|
|||
if (!src)
|
||||
return -1;
|
||||
|
||||
// for strings, first char must be a s-quote or d-quote
|
||||
int qchar = src[0];
|
||||
int srclen = strlen(src);
|
||||
if (!(qchar == '\'' || qchar == '"')) {
|
||||
|
@ -2219,12 +2225,14 @@ int toml_rtos(toml_raw_t src, char **ret) {
|
|||
|
||||
// triple quotes?
|
||||
if (qchar == src[1] && qchar == src[2]) {
|
||||
multiline = 1;
|
||||
sp = src + 3;
|
||||
sq = src + srclen - 3;
|
||||
/* last 3 chars in src must be qchar */
|
||||
if (!(sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar))
|
||||
multiline = 1; // triple-quote implies multiline
|
||||
sp = src + 3; // first char after quote
|
||||
sq = src + srclen - 3; // first char of ending quote
|
||||
|
||||
if (!(sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) {
|
||||
// last 3 chars in src must be qchar
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* skip new line immediate after qchar */
|
||||
if (sp[0] == '\n')
|
||||
|
@ -2233,13 +2241,18 @@ int toml_rtos(toml_raw_t src, char **ret) {
|
|||
sp += 2;
|
||||
|
||||
} else {
|
||||
sp = src + 1;
|
||||
sq = src + srclen - 1;
|
||||
/* last char in src must be qchar */
|
||||
if (!(sp <= sq && *sq == qchar))
|
||||
sp = src + 1; // first char after quote
|
||||
sq = src + srclen - 1; // ending quote
|
||||
if (!(sp <= sq && *sq == qchar)) {
|
||||
/* last char in src must be qchar */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// at this point:
|
||||
// sp points to first valid char after quote.
|
||||
// sq points to one char beyond last valid char.
|
||||
// string len is (sq - sp).
|
||||
if (qchar == '\'') {
|
||||
*ret = norm_lit_str(sp, sq - sp, multiline, 0, 0);
|
||||
} else {
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#define TOML_H
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable: 4996)
|
||||
#pragma warning(disable : 4996)
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
|
|
@ -91,15 +91,16 @@ static void print_raw(const char *s) {
|
|||
else
|
||||
millisec[0] = 0;
|
||||
if (ts.year && ts.hour) {
|
||||
printf("{\"type\":\"datetime\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%"
|
||||
printf("{\"type\":\"%s\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%"
|
||||
"s%s\"}",
|
||||
(ts.z ? "datetime" : "datetime-local"),
|
||||
*ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second,
|
||||
millisec, (ts.z ? ts.z : ""));
|
||||
} else if (ts.year) {
|
||||
printf("{\"type\":\"date\",\"value\":\"%04d-%02d-%02d\"}", *ts.year,
|
||||
printf("{\"type\":\"date-local\",\"value\":\"%04d-%02d-%02d\"}", *ts.year,
|
||||
*ts.month, *ts.day);
|
||||
} else if (ts.hour) {
|
||||
printf("{\"type\":\"time\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour,
|
||||
printf("{\"type\":\"time-local\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour,
|
||||
*ts.minute, *ts.second, millisec);
|
||||
}
|
||||
} else {
|
||||
|
@ -149,36 +150,43 @@ static void print_table_array(toml_array_t *curarr) {
|
|||
}
|
||||
|
||||
static void print_array(toml_array_t *curarr) {
|
||||
toml_array_t *arr;
|
||||
const char *raw;
|
||||
int i;
|
||||
|
||||
if (toml_array_kind(curarr) == 't') {
|
||||
print_table_array(curarr);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("{\"type\":\"array\",\"value\":[");
|
||||
switch (toml_array_kind(curarr)) {
|
||||
printf("[");
|
||||
|
||||
case 'v':
|
||||
for (i = 0; 0 != (raw = toml_raw_at(curarr, i)); i++) {
|
||||
printf("%s", i > 0 ? "," : "");
|
||||
print_raw(raw);
|
||||
}
|
||||
break;
|
||||
const char *raw;
|
||||
toml_array_t *arr;
|
||||
toml_table_t *tab;
|
||||
|
||||
case 'a':
|
||||
for (i = 0; 0 != (arr = toml_array_at(curarr, i)); i++) {
|
||||
printf("%s", i > 0 ? "," : "");
|
||||
const int n = toml_array_nelem(curarr);
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%s", i > 0 ? "," : "");
|
||||
|
||||
if (0 != (arr = toml_array_at(curarr, i))) {
|
||||
print_array(arr);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
if (0 != (tab = toml_table_at(curarr, i))) {
|
||||
print_table(tab);
|
||||
continue;
|
||||
}
|
||||
|
||||
raw = toml_raw_at(curarr, i);
|
||||
if (raw) {
|
||||
print_raw(raw);
|
||||
continue;
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
fprintf(stderr, "ERROR: unable to decode value in array\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("]}");
|
||||
|
||||
printf("]");
|
||||
}
|
||||
|
||||
static void cat(FILE *fp) {
|
||||
|
|
Loading…
Reference in New Issue