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