+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Full Member ziele_o2k's Avatar
    Using
    AutoCAD 2016
    Join Date
    Jun 2014
    Location
    Poland
    Posts
    62

    Default Handle INI files with lisp challenge (without acet)

    Registered forum members do not see this ad.

    Hey,

    In my software I'm using ini files. I wrote my own routines to handle them, because I don't like tu use acet/doslib. It would be great if somone would wrote functions simillar to acet-get-ini / acet-set-ini or dos_getini / dos_setini or both...

    It's just an idea! Unfortunately I don't have time to do that, and (also and more important) i don't need those routines. Although it would be great to have that stuff
    Maybe some one will take this post as challenge ?

    EDIT:
    I can post info about where to find documentation for acet / doslib functions

  2. #2
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    8,625

    Default

    Its (defun getini ( filename key / )) write your own version as a rule INI file is just a text file.

    I have some library text files one has 200+ lines in it and its formatted in a way that you can find the value of a known "Key"

    Code:
     eg use a comma seperated you can use TAB also 
    Key1,value1
    
    or by position
    12345678901234567890
    key1 Value1 Colour Linetype
    Last edited by BIGAL; 8th Jan 2017 at 12:51 am.
    A man who never made mistakes never made anything

  3. #3
    Full Member ziele_o2k's Avatar
    Using
    AutoCAD 2016
    Join Date
    Jun 2014
    Location
    Poland
    Posts
    62

    Default

    Registered forum members do not see this ad.

    Ok,
    based on this Lee Mac's code i wrote something like this:

    1) pz:INI-Read
    Read ini file to formated list - slighty modified XParseConfig by Lee Mac.
    Semicolons ( ; ) and number signs ( # ) at the beginning of the line indicate a comment.
    Code:
    (defun pz:INI-Read ( file / line sub lst i m )
      (if (and (setq file (findfile file)) (setq file (open file "r")))
        (progn
          (while (setq line (read-line file))
            (cond
              ( (wcmatch line "`[*`]")
                (if sub (setq lst (cons (reverse sub) lst)))
                (setq sub (list (substr line 2 (- (strlen line) 2))))
              )
              ( (and (/= (substr line 1 1) ";") (/= (substr line 1 1) "#") (wcmatch line "*=*"))
                (setq i 0)
                (while (/= "=" (substr line (setq i (1+ i)) 1)))
                (setq sub (cons (list (substr line 1 (1- i)) (substr line (1+ i))) sub))
              )
              ( (setq sub (cons (list line) sub)) )
            )
          )
          (close file)
          (if sub (setq lst (cons (reverse sub) lst)))
        )
      )
      (reverse lst)
    )

    2) pz:INI-Get
    If section is nil, a list of section names is returned.
    If key is nil, a list of keys for the specified section is returned.
    If key is not found, default is returned.
    Otherwise, a string associated with key is returned.

    Code:
    (defun pz:INI-Get ( file section key default / alist res )
      (if 
        (and
          (=(type file) 'STR)
          (setq alist (pz:INI-Read file))
        )
        (cond
          ( (not section)
            (foreach %1 alist
              (if (= (type (car %1)) 'STR)
                (setq res (cons (car %1) res))
              )
            )
            (reverse res)
          )
          ( (and (not key) (assoc section alist))
            (foreach %2 (cdr(assoc section alist))
              (if (= (length %2) 2)
                (setq res (cons (car %2) res))
              )
            )
            (reverse res)
          )
          ( (setq section (cdr (assoc section alist)))
            (if (setq res (assoc key section))
              (setq res (cadr res ))
              (setq res default)
            )
            res
          )
        )
        nil
      )
    )
    3) pz:INI-Set
    section: A string containing the name of the section for the given data. If the section does not exist, it will be created.
    key: A string containing the name of the key to be associated with a value. If the key does not exist in the given section,
    it will be created.
    Code:
    (defun pz:INI-Set ( file section key value / alist sectionl i )
      (if
        (and
          (=(type file) 'STR)
          (=(type section) 'STR)
          (=(type key) 'STR)
          (=(type value) 'STR)
          (setq alist (pz:INI-Read file))
        )
        (progn
          (if (setq sectionl (assoc section alist))
            (setq 
              sectionl (cdr sectionl)
              sectionl 
              (if (assoc key sectionl)
                (subst (list key value) (assoc key sectionl) sectionl)
                ;(reverse (append (list(list key value)) (reverse sectionl) ))
                (progn
                  (setq i (1-(length sectionl)))
                  (while (= (length(nth i sectionl)) 1)
                    (setq i (1- i))
                  )
                  (cd:LST_InsertItem (1+ i) sectionl (list key value))
                )
              )
              alist (subst (cons section sectionl) (assoc section alist) alist)
            )
            (setq alist (reverse (append (list(list section (list key value))) (reverse alist) )))
          )
          (if (and (setq file (findfile file)) (setq file (open file "w")))
            (progn
              (foreach section alist
                (if (= (type (car section)) 'STR)
                  (progn
                    (write-line (strcat "[" (car section) "]") file)
                    (foreach key (cdr section)
                      (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
                    )
                  )
                  (foreach key section
                    (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
                  )
                )
              )
              (not (close file))
            )
          )
        )
        nil
      )
    )
    4) pz:INI-Del
    In progress


    one subroutine needed
    Code:
    (defun cd:LST_InsertItem (Pos Lst New / res)
      (if (< -1 Pos (1+ (length Lst)))
        (progn
          (repeat Pos
            (setq res (cons (car Lst) res)
                  Lst (cdr Lst)
            )
          )
          (append (reverse res) (list New) Lst)
        )
        Lst
      )
    )

Similar Threads

  1. after loding lisp : "error no function definition , ACET-STR-FORMAT"
    By Salama in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 28th Mar 2014, 11:03 pm
  2. Acet- functions family... help me & help files
    By danie7LT in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 8th Jul 2013, 04:04 pm
  3. New Challenge! Lisp for Tags.
    By tomhamlet in forum AutoLISP, Visual LISP & DCL
    Replies: 0
    Last Post: 8th Nov 2012, 02:50 pm
  4. the challenge lisp
    By motee-z in forum AutoLISP, Visual LISP & DCL
    Replies: 13
    Last Post: 7th Jan 2012, 04:41 pm
  5. acet-help not loading from Lisp
    By slimpickinz in forum AutoLISP, Visual LISP & DCL
    Replies: 91
    Last Post: 19th Oct 2009, 04:22 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts