Compare commits
3 Commits
6a4005c1d8
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a9f1f9b7b6 | |||
| 8343be2a66 | |||
| e2c56e8b82 |
@@ -3,6 +3,7 @@
|
||||
(require racket/string)
|
||||
(require racket/file)
|
||||
(require racket/port)
|
||||
(require racket/serialize)
|
||||
|
||||
(provide file->ini
|
||||
ini->file
|
||||
@@ -19,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))
|
||||
@@ -49,8 +71,7 @@
|
||||
(set! last-is-newline #f)
|
||||
(display (cadr line) out)
|
||||
(display "=" out)
|
||||
(display"VALUE:" out)
|
||||
(write (caddr line) out)
|
||||
(output-value (caddr line) out)
|
||||
(newline out))
|
||||
(error "Unknown line format")))))
|
||||
lines))))
|
||||
@@ -63,10 +84,16 @@
|
||||
(define re-num #px"^([+]|[-])?([0-9]+([.]([0-9]+))?)$")
|
||||
(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)
|
||||
@@ -78,8 +105,12 @@
|
||||
#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)))))
|
||||
(let ((content (cadr sr)))
|
||||
(deserialize (with-input-from-string content read))))))
|
||||
|
||||
(define (file->ini file*)
|
||||
(let* ((file (get-ini-file file*))
|
||||
|
||||
Reference in New Issue
Block a user