TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Peter2 on January 30, 2014, 09:31:22 AM

Title: Handling of INI files?
Post by: Peter2 on January 30, 2014, 09:31:22 AM
Hi

maybe I missed a standard feature, but how to handle read/write INI-files in (V)Lisp without DosLib?

Regards
Title: Re: Handling of INI files?
Post by: Marc'Antonio Alessi on January 30, 2014, 10:05:13 AM
I use Lisp files as INI for my settings, in my archive I have this:
Code: [Select]
;; PROFILE.LSP  Copyright 1994-97  Tony Tanzillo  All rights reserved.
;;
;; Reads Windows-style .ini files
;;
;; Usage:
;;
;;   (ini:load <filename>)
;;   
;;      Loads an .ini file into a list and returns
;;      the list to its caller, for access using
;;      other functions in this library.
;;     
;;   (GetItem <section> <item> <data>)
;;   
;;      Returns the value of an item in an .ini
;;      file that has been loaded by the (ini:load)
;;      function.
;;     
;;        <section> is the name of the section
;;        from the ini file (do not include the
;;        brackets!!!).
;;       
;;        <item> is the name of the item within
;;        the specified <section>
;;       
;;        <data> 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))
;;       
Title: Re: Handling of INI files?
Post by: Peter2 on January 30, 2014, 10:28:05 AM
Looks interesting. Could you also upload the code?

EDIT: But there is no write-function.
Title: Re: Handling of INI files?
Post by: Matt__W on January 30, 2014, 10:32:13 AM
I use DosLib and its INI functions to read/write to an INI file, it's just SOOO much easier.  Do you not want to use DosLib because you don't want to rely on a third-party program?
Title: Re: Handling of INI files?
Post by: dgorsman on January 30, 2014, 10:35:28 AM
- read the file into a list
- parse the list to create a list of section headers; usually included in [___] but helps to be flexible
- parse a section to create a list of dotted pairs of elements; usually indicated with ELEMENT=VALUE but again, helps to be flexible
- handle comments, usually indicated with a single or double semi-colon

This was one of my first LISP tool libraries.  Highly useful for developing list, string, and file handling coding practices.
Title: Re: Handling of INI files?
Post by: Peter2 on January 30, 2014, 10:35:59 AM
I use DosLib and its INI functions to read/write to an INI file, it's just SOOO much easier....
Yes, I know. Normally I use it too.
...Do you not want to use DosLib because you don't want to rely on a third-party program?
I'm thinking about it. In this case I would only need the INI features, and maybe I can abstain from DosLib for this code.
Title: Re: Handling of INI files?
Post by: Marc'Antonio Alessi on January 30, 2014, 11:36:36 AM
Looks interesting. Could you also upload the code?

EDIT: But there is no write-function.
I'm sorry but I do not remember where I copied it and I do not know if I can do it for the Copyright, I made ​​a mistake of this kind years ago... Write me a personal message and I will send to you.
Title: Re: Handling of INI files?
Post by: NICK_VNV on January 30, 2014, 12:56:56 PM
You can use Express Tools acet- functions "acet-ini-set" and "acet-ini-get" :
Code: [Select]
; Write to INI
(acet-ini-set "c:\\filename.ini" "dir_name" "name1" "value1")
(acet-ini-set "c:\\filename.ini" "dir_name" "name2" "value2")
; Read from INI
(acet-ini-get "c:\\filename.ini" "dir_name" "name1") ; will return "value1"
;or
(acet-ini-get "c:\\filename.ini" "dir_name" ) ; will return list  ("name1" "name2")

Contents of Filename.ini:

[dir_name]
name1=value1
name2=value2
Title: Re: Handling of INI files?
Post by: Peter2 on January 30, 2014, 02:35:45 PM
You can use Express Tools acet- functions "acet-ini-set" and "acet-ini-get" ....
That's fine, thanks. But because some people don't have the Express Tools I will take a look at the copyright of the code - maybe I can integrate it.