Better serialization/deserialization
This commit is contained in:
@@ -20,6 +20,27 @@
|
||||
(build-path pref-dir (string-append (symbol->string f) ".ini")))
|
||||
(build-path f)))
|
||||
|
||||
|
||||
(define (output-value v out)
|
||||
(cond
|
||||
((string? v)
|
||||
(if (regexp-match? #rx"[\r\n]" v)
|
||||
(begin
|
||||
(display "VALUE:" out)
|
||||
(write v out))
|
||||
(display v out)))
|
||||
((path? v)
|
||||
(display (format "PATH:~a" v) out))
|
||||
((number? v)
|
||||
(display v out))
|
||||
((boolean? v)
|
||||
(display (if v "true" "false") out))
|
||||
(else
|
||||
(display"SER:" out)
|
||||
(write (serialize v) out))
|
||||
)
|
||||
)
|
||||
|
||||
(define (ini->file ini file)
|
||||
(let ((out (open-output-file (get-ini-file file) #:exists 'replace)))
|
||||
(let ((last-is-newline #f))
|
||||
@@ -50,8 +71,7 @@
|
||||
(set! last-is-newline #f)
|
||||
(display (cadr line) out)
|
||||
(display "=" out)
|
||||
(display"SER:" out)
|
||||
(write (serialize (caddr line)) out)
|
||||
(output-value (caddr line) out)
|
||||
(newline out))
|
||||
(error "Unknown line format")))))
|
||||
lines))))
|
||||
@@ -65,12 +85,15 @@
|
||||
(define re-bool #px"^(#f|#t|true|false)$")
|
||||
(define re-value #px"^VALUE:(.*)$")
|
||||
(define re-serialize #px"^SER:(.*)$")
|
||||
(define re-path #px"^PATH:(.*)$")
|
||||
|
||||
(define (interpret s)
|
||||
(let ((sr (regexp-match re-serialize s)))
|
||||
(if (eq? sr #f)
|
||||
(let ((m (regexp-match re-value s)))
|
||||
(if (eq? m #f)
|
||||
(let ((p (regexp-match re-path s)))
|
||||
(if (eq? p #f)
|
||||
(let ((ss (string-downcase (string-trim s))))
|
||||
(let ((m-num (regexp-match re-num ss)))
|
||||
(if (eq? m-num #f)
|
||||
@@ -82,6 +105,7 @@
|
||||
#f)
|
||||
))
|
||||
(string->number (car m-num)))))
|
||||
(string->path (cadr p))))
|
||||
(let* ((content (cadr m)))
|
||||
(with-handlers ([exn? (λ (e) content)])
|
||||
(with-input-from-string content read)))))
|
||||
|
||||
Reference in New Issue
Block a user