;; PROFILE.LSP Copyright 1994-97 Tony Tanzillo All rights reserved. ;; ;; Reads Windows-style .ini files ;; ;; Usage: ;; ;; (ini:load ) ;; ;; Loads an .ini file into a list and returns ;; the list to its caller, for access using ;; other functions in this library. ;; ;; (GetItem
) ;; ;; Returns the value of an item in an .ini ;; file that has been loaded by the (ini:load) ;; function. ;; ;;
is the name of the section ;; from the ini file (do not include the ;; brackets!!!). ;; ;; is the name of the item within ;; the specified
;; ;; is the list containing the ini ;; file data in the format returned by ;; the (ini:load) function. ;; ;; Example: ;; ;; ;; Load .ini file into a list ;; (setq data (Ini:load "acad.ini")) ;; ;; ;; Read the item 'Color' from the ;; ;; [Font] section of the .ini file ;; ;; ;; ;; [Font] ;; ;; Color=RED ;; ;; Size=12 ;; ;; Bold=1 ;; ;; Italic=0 ;; ;; (setq color (GetItem "Font" "Color" data)) ;; (defun ini:load (file / rslt section fd line) (cond ( (not (setq file (findfile (strcat file ".INI")))) nil) ( (not (setq fd (open file "r"))) nil) (t (ini:readfile fd)) ) ) (defun ini:write (file lst / fd) (setq fd (open file "w")) (foreach section lst (foreach item section (write-line item fd) ) ) (close fd) ) (defun ini:readfile (fd / rslt section line) (while (setq line (read-line fd)) (cond ( (wcmatch line ";*, ;*, ,")) ( (wcmatch line "`[*`]") (if section (setq rslt (append rslt (list section)))) (setq section (list (strcase line)))) ( (not section) nil) (t (setq section (append section (list line)))))) (if section (setq rslt (append rslt (list section)))) (setq fd (close fd)) rslt ) (defun ini:parse (item / name c) (setq i 0 name "") (while (not (member (setq c (substr item (setq i (1+ i)) 1)) '("=" ""))) (setq name (strcat name c))) (cons (strcase name) (substr item (1+ i))) ) (defun getsection (name data / rslt) (if (setq rslt (assoc (strcat "[" (strcase name) "]") data)) (mapcar 'ini:parse (cdr rslt)) ) ) (defun getitem (section item data / rslt) (if (setq rslt (getsection section data)) (cdr (assoc (strcase item) rslt)) ) ) (defun mapsection (name data function / d) (if (setq d (getsection name data)) (mapcar function d) ) )