Merge branch 'main' of https://github.com/hdijkema/simple-ini
This commit is contained in:
15
main.rkt
15
main.rkt
@@ -8,10 +8,18 @@
|
|||||||
ini-get
|
ini-get
|
||||||
ini-set!
|
ini-set!
|
||||||
make-ini
|
make-ini
|
||||||
|
get-ini-file
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
(define (get-ini-file f)
|
||||||
|
(if (symbol? f)
|
||||||
|
(let* ((pref-dir (find-system-path 'pref-dir)))
|
||||||
|
(build-path pref-dir (string-append (symbol->string f) ".ini")))
|
||||||
|
(build-path f)))
|
||||||
|
|
||||||
(define (ini->file ini file)
|
(define (ini->file ini file)
|
||||||
(let ((out (open-output-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))
|
||||||
(for-each (lambda (section)
|
(for-each (lambda (section)
|
||||||
(let ((section-name (car section)))
|
(let ((section-name (car section)))
|
||||||
@@ -64,8 +72,9 @@
|
|||||||
#f)
|
#f)
|
||||||
s)))))))
|
s)))))))
|
||||||
|
|
||||||
(define (file->ini file)
|
(define (file->ini file*)
|
||||||
(let* ((lines (file->lines file))
|
(let* ((file (get-ini-file file*))
|
||||||
|
(lines (if (file-exists? file) (file->lines file) '()))
|
||||||
(re-section #px"^\\[([a-zA-Z0-9_-]+)\\]$")
|
(re-section #px"^\\[([a-zA-Z0-9_-]+)\\]$")
|
||||||
(re-keyval #px"^([a-zA-Z0-9_-]+)[=](.*)$")
|
(re-keyval #px"^([a-zA-Z0-9_-]+)[=](.*)$")
|
||||||
(re-comment #px"^[;](.*)$"))
|
(re-comment #px"^[;](.*)$"))
|
||||||
|
|||||||
2
roos.rkt
2
roos.rkt
@@ -12,7 +12,7 @@
|
|||||||
(set! a b))))
|
(set! a b))))
|
||||||
|
|
||||||
(def-roos (ini . _file) this (supers)
|
(def-roos (ini . _file) this (supers)
|
||||||
(file* (if (null? _file) #f (car _file)))
|
(file* (if (null? _file) #f (get-ini-file (car _file))))
|
||||||
(content (if (not (eq? file* #f))
|
(content (if (not (eq? file* #f))
|
||||||
(if (file-exists? file*)
|
(if (file-exists? file*)
|
||||||
(file->ini file*)
|
(file->ini file*)
|
||||||
|
|||||||
@@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
@defproc[(file->ini [file path-string?]) mc-pair?]{
|
@defproc[(file->ini [file path-string?]) mc-pair?]{
|
||||||
Reads an INI file from disk and parses it into an internal mutable cons pair (mc-pair) structure.
|
Reads an INI file from disk and parses it into an internal mutable cons pair (mc-pair) structure.
|
||||||
|
If the file does noet exist, an empty ini structure is made.
|
||||||
|
If file is a symbol?, the file will be constructed from the prefs-dir, the symbol and a suffix ".ini".
|
||||||
|
|
||||||
The parser supports:
|
The parser supports:
|
||||||
|
|
||||||
@@ -42,7 +44,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
@defproc[(ini->file [ini mc-pair?] [file path-string?]) void?]{
|
@defproc[(ini->file [ini mc-pair?] [file path-string?]) void?]{
|
||||||
Writes an INI structure (as produced by @racket[file->ini] or @racket[make-ini]) to the specified file.
|
Writes an INI structure (as produced by @racket[file->ini] or @racket[make-ini]) to the specified file.
|
||||||
|
If file is a symbol?, the file will be constructed from the prefs-dir, the symbol and a suffix ".ini".
|
||||||
|
|
||||||
The output preserves:
|
The output preserves:
|
||||||
@itemlist[
|
@itemlist[
|
||||||
|
|||||||
Reference in New Issue
Block a user