(import (chicken base) scheme (scheme base) (chicken foreign) (chicken memory) (chicken gc) (chicken format) rfc3339 coops coops-primitive-objects) (foreign-declare "#include ") (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 () ((ptr :accessor ptr :initform #f))) (define-class () ((ptr :accessor ptr :initform #f))) (define-method (toml-self-key (tarr )) ((foreign-lambda* c-string ((c-pointer tarr)) "C_return(toml_array_key(tarr));") (ptr tarr))) (define-method (toml-count-entries (tarr )) ((foreign-lambda* int ((c-pointer tarr)) "C_return(toml_array_nelem(tarr));") (ptr tarr))) (define-method (toml-string (tarr ) (index )) ((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 ) (index )) ((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 ) (index )) ((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 ) (index )) ((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 ) (index )) (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 ) (index )) (make '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 ) (index )) (make '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 '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 '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 )) ((foreign-lambda* c-string ((c-pointer ttbl)) "C_return(toml_table_key(ttbl));") (ptr ttbl))) (define-method (toml-key-exists? (ttbl ) (key )) (= 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 )) ((foreign-lambda* int ((c-pointer ttbl)) "C_return(toml_table_nkval(ttbl));") (ptr ttbl))) (define-method (toml-count-arrays (ttbl )) ((foreign-lambda* int ((c-pointer ttbl)) "C_return(toml_table_narr(ttbl));") (ptr ttbl))) (define-method (toml-count-tables (ttbl )) ((foreign-lambda* int ((c-pointer ttbl)) "C_return(toml_table_ntab(ttbl));") (ptr ttbl))) (define-method (toml-key-at (ttbl ) (index )) ((foreign-lambda* c-string ((c-pointer ttbl) (int index)) "C_return(toml_key_in(ttbl, index));") (ptr ttbl) index)) (define-method (toml-string (ttbl ) (key )) ((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 ) (key )) ((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 ) (key )) ((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 ) (key )) ((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 ) (key )) (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 ) (key )) (make '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 ) (key )) (make 'ptr ((foreign-lambda* c-pointer ((c-pointer ttbl) (c-string key)) "C_return(toml_table_in(ttbl, key));") (ptr ttbl) key)))