[text]
newLISP program to edit .ini files.
No associative arrays are used, and the data is
kept in its original order.
Say the .ini file contains
[profiles]
path = /home
browsable = yes
[global]
security = user
os level = 65
Then
(get "profiles" "path")
produces
"/home"
A shorter way is to use symbols:
(get .profiles .path)
The symbols have had any spaces changed to ".":
(get .global .os.level)
To change or add, use "put":
(put .global .os.level 99)
yields
(("profiles" (("path" "/home") ("browsable" "yes")))
("global" (("security" "user") ("os level" 99))))
When adding a new option, use a string instead of
a symbol:
(put .global "foo" "bar")
Now the new option can be referenced by a symbol:
(get .global .foo)
To delete an option, use "put" with no value:
(put .profiles .path)
To see what the output will look like:
(spill)
[profiles]
browsable = yes
[global]
security = user
os level = 99
foo = bar
To write the data to a file:
(setq handle (open "out.ini" "w"))
(spill handle)
(close handle)
[/text]
(define (parse-option str)
(parse str {\s*=\s*} 0))
(define (abbrev str)
(set (sym (append "." (replace " " (string str) "."))) str))
(define (get-section sec-name)
(lookup sec-name Data))
(define (change the-list key value)
(if (= nil (lookup key the-list))
(if value (push (list key value) the-list -1))
(if value
(replace-assoc key the-list (list key value))
(replace-assoc key the-list)))
the-list)
;; Add or change an option.
(define (put section-name option value , section)
(abbrev option)
(setq section (get-section section-name))
(if value
(setq section (change section option (string value)))
(setq section (change section option)))
(setq Data (change Data section-name section)))
;; Get an option's value.
(define (get section option)
(lookup option (get-section section)))
(define (add-section str)
(abbrev str)
(push (list str '()) Data -1))
(define (del-section str)
(replace-assoc str Data))
;; Write to screen if no file-handle given.
(define (output str handle)
(if handle (write-line str handle)
(println str)))
;; Write Data in .ini format.
(define (spill handle , section xx yy)
(dolist (section Data)
(dolist (xx section)
(if (list? xx)
(dolist (yy xx) (output (join yy " = ") handle))
(output (append "[" xx "]") handle)))))
(define (parse-file handle , line)
(while (read-line handle)
(setq line (trim (current-line)))
(if (regex {^\[(.*)\]$} line)
(begin
(add-section $1)
(setq section-name $1))
(apply put (flat (list section-name (parse-option line)))))))
; --------------------
(if (> 3 (length (main-args)))
(begin (println "Name of input file is needed.")(exit 5)))
(setq filename ((main-args) 2))
(if (not (setq handle (open filename "r")))
(begin (println "Can't open " filename)(exit 5)))
(if (> (length (main-args) 3)) (setq outname ((main-args) 3)))
(parse-file handle)
(close handle)
(println Data)
# (exit)