乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 47|回复: 3

[编程交流] 使用lisp cha处理INI文件

[复制链接]

46

主题

161

帖子

104

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
242
发表于 2022-7-5 16:45:05 | 显示全部楼层 |阅读模式

 
在我的软件中,我使用ini文件。我编写了自己的例程来处理它们,因为我不喜欢使用acet/doslib。如果有人能编写类似于acet get ini/acet set ini或dos\u getini/dos\u setini或两者的函数,那就太好了。。。
 
这只是一个想法!不幸的是,我没有时间这样做,而且(也是更重要的)我不需要这些常规。虽然有这些东西会很棒
也许有人会把这个职位当成挑战?
 
编辑:
我可以发布有关在哪里可以找到acet/doslib函数文档的信息
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 17:20:09 | 显示全部楼层
它(defun getini(filename key/)将您自己的版本写入规则INI文件只是一个文本文件。
 
我有一些库文本文件,其中一个有200多行,其格式可以让你找到一个已知“键”的值
 
  1. eg use a comma seperated you can use TAB also
  2. Key1,value1
  3. or by position
  4. 12345678901234567890
  5. key1 Value1 Colour Linetype
回复

使用道具 举报

46

主题

161

帖子

104

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
242
发表于 2022-7-5 17:25:49 | 显示全部楼层
好啊
基于李的代码,我写了这样的东西:
 
1) pz:INI读取
将ini文件读取到格式化列表-稍微修改了李Mac的XParseConfig。
分号(;)行开头的数字符号(#)表示注释。
  1. (defun pz:INI-Read ( file / line sub lst i m )
  2. (if (and (setq file (findfile file)) (setq file (open file "r")))
  3.    (progn
  4.      (while (setq line (read-line file))
  5.        (cond
  6.          ( (wcmatch line "`[*`]")
  7.            (if sub (setq lst (cons (reverse sub) lst)))
  8.            (setq sub (list (substr line 2 (- (strlen line) 2))))
  9.          )
  10.          ( (and (/= (substr line 1 1) ";") (/= (substr line 1 1) "#") (wcmatch line "*=*"))
  11.            (setq i 0)
  12.            (while (/= "=" (substr line (setq i (1+ i)) 1)))
  13.            (setq sub (cons (list (substr line 1 (1- i)) (substr line (1+ i))) sub))
  14.          )
  15.          ( (setq sub (cons (list line) sub)) )
  16.        )
  17.      )
  18.      (close file)
  19.      (if sub (setq lst (cons (reverse sub) lst)))
  20.    )
  21. )
  22. (reverse lst)
  23. )

 
 
2) pz:INI获取
如果节为零,则返回节名称列表。
如果key为nil,则返回指定节的键列表。
如果未找到键,则返回默认值。
否则,将返回与键关联的字符串。
 
  1. (defun pz:INI-Get ( file section key default / alist res )
  2. (if
  3.    (and
  4.      (=(type file) 'STR)
  5.      (setq alist (pz:INI-Read file))
  6.    )
  7.    (cond
  8.      ( (not section)
  9.        (foreach %1 alist
  10.          (if (= (type (car %1)) 'STR)
  11.            (setq res (cons (car %1) res))
  12.          )
  13.        )
  14.        (reverse res)
  15.      )
  16.      ( (and (not key) (assoc section alist))
  17.        (foreach %2 (cdr(assoc section alist))
  18.          (if (= (length %2) 2)
  19.            (setq res (cons (car %2) res))
  20.          )
  21.        )
  22.        (reverse res)
  23.      )
  24.      ( (setq section (cdr (assoc section alist)))
  25.        (if (setq res (assoc key section))
  26.          (setq res (cadr res ))
  27.          (setq res default)
  28.        )
  29.        res
  30.      )
  31.    )
  32.    nil
  33. )
  34. )

 
3) pz:INI集
节:包含给定数据节名称的字符串。如果该节不存在,则将创建该节。
键:包含要与值关联的键的名称的字符串。如果给定部分中不存在密钥,
它将被创建。
  1. (defun pz:INI-Set ( file section key value / alist sectionl i )
  2. (if
  3.    (and
  4.      (=(type file) 'STR)
  5.      (=(type section) 'STR)
  6.      (=(type key) 'STR)
  7.      (=(type value) 'STR)
  8.      (setq alist (pz:INI-Read file))
  9.    )
  10.    (progn
  11.      (if (setq sectionl (assoc section alist))
  12.        (setq
  13.          sectionl (cdr sectionl)
  14.          sectionl
  15.          (if (assoc key sectionl)
  16.            (subst (list key value) (assoc key sectionl) sectionl)
  17.            ;(reverse (append (list(list key value)) (reverse sectionl) ))
  18.            (progn
  19.              (setq i (1-(length sectionl)))
  20.              (while (= (length(nth i sectionl)) 1)
  21.                (setq i (1- i))
  22.              )
  23.              (cd:LST_InsertItem (1+ i) sectionl (list key value))
  24.            )
  25.          )
  26.          alist (subst (cons section sectionl) (assoc section alist) alist)
  27.        )
  28.        (setq alist (reverse (append (list(list section (list key value))) (reverse alist) )))
  29.      )
  30.      (if (and (setq file (findfile file)) (setq file (open file "w")))
  31.        (progn
  32.          (foreach section alist
  33.            (if (= (type (car section)) 'STR)
  34.              (progn
  35.                (write-line (strcat "[" (car section) "]") file)
  36.                (foreach key (cdr section)
  37.                  (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
  38.                )
  39.              )
  40.              (foreach key section
  41.                (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
  42.              )
  43.            )
  44.          )
  45.          (not (close file))
  46.        )
  47.      )
  48.    )
  49.    nil
  50. )
  51. )

 
4) pz:INI Del
正在进行中
 
 
需要一个子例程
  1. (defun cd:LST_InsertItem (Pos Lst New / res)
  2. (if (< -1 Pos (1+ (length Lst)))
  3.    (progn
  4.      (repeat Pos
  5.        (setq res (cons (car Lst) res)
  6.              Lst (cdr Lst)
  7.        )
  8.      )
  9.      (append (reverse res) (list New) Lst)
  10.    )
  11.    Lst
  12. )
  13. )
回复

使用道具 举报

46

主题

161

帖子

104

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
242
发表于 2022-7-5 17:55:39 | 显示全部楼层
好的,我希望这是最终结果
它应该如何工作:
-函数将保留ini文件格式(制表符/空格)
-节和kay名称不区分大小写
-评论可以从开始;或#
ini文件示例(要测试我的*ini*功能,请在cad搜索路径中创建文件Example.ini):
 
  1. #I hope
  2. #it will be
  3. [sECTION1]
  4. KEY1  = VALUE 1_1
  5.    ;useful
  6. KEY2  =
  7. KEY3  = VALUE 1_3
  8.    [sECTION2]
  9. KEY1 = VALUE 2_1
  10. KEY2    = VALUE 2_2
  11. KEY3=VALUE 2_3
  12. [sECTION3]
  13. KEY1=VALUE 3_1
  14. KEY2=VALUE 3_2
  15.      
  16. KEY3=VALUE 3_3
  17.        [sECTION4]        
  18. KEY1    =   VALUE 4_1
  19. KEY2    =   VALUE 4_2
  20. KEY3    =   VALUE 4_3
  21.   ;for someone

 
My*ini*函数(基于第三篇文章中给出的源代码)
 
  1. ; =========================================================================================== ;
  2. ; (pz:INI-Get file section key value)                                                         ;
  3. ;  file    [sTR]     - short or full path file name                                           ;
  4. ;  section [sTR/nil] - name of section to read key                                            ;
  5. ;                      if nil then function will return all section names from .ini file      ;
  6. ;  key     [sTR/nil] - name of key to read value                                              ;
  7. ;                      if nil then function will return all key names from section            ;
  8. ;                      if key value is "" then returns nil                                    ;
  9. ;  value   [sTR/nil] - default value if section or key not found                              ;
  10. ; ------------------------------------------------------------------------------------------- ;
  11. ; (pz:INI-Get "example.ini" "SECTION1" "KEY2" nil)                                            ;
  12. ; =========================================================================================== ;
  13. (defun pz:INI-Get ( file section key value / l res tmpl )
  14. (if (and section (=(type section) 'STR))
  15.    (setq section (strcat "[" section "]"))
  16. )
  17. (if (setq l (pz:INI-Read file))
  18.    (cond
  19.      ( (not section)
  20.        (foreach %1 l
  21.          (if (= (type (car %1)) 'STR)
  22.            (setq res
  23.              (cons
  24.                (substr
  25.                  (pz:TrimStr (car %1))
  26.                  2
  27.                  (- (strlen (pz:TrimStr (car %1))) 2)
  28.                )
  29.                res
  30.              )
  31.            )
  32.          )
  33.        )
  34.        (setq res (reverse res))
  35.      )
  36.      ( (and
  37.          (not key)
  38.          (setq tmpl (pz:AssocCI section l))
  39.        )
  40.        (foreach %1 (cdr tmpl)
  41.          (if (= (length %1) 2)
  42.            (setq res (cons (pz:TrimStr(car %1)) res))
  43.          )
  44.        )
  45.        (setq res (reverse res))
  46.      )
  47.      ( (and
  48.          key
  49.          (setq section (cdr (pz:AssocCI section l)))
  50.        )
  51.        (if (setq res (pz:AssocCI key section))
  52.          (if (= (setq res (pz:TrimStr(cadr res))) "")
  53.            (setq res nil)
  54.          )
  55.          (setq res value)
  56.        )
  57.      )
  58.      ( T (setq res value))
  59.    )
  60.    (setq res value)
  61. )
  62. res
  63. )
  1. ; =========================================================================================== ;
  2. ; (pz:INI-Set file section key value)                                                         ;
  3. ;  file    [sTR] - short or full path file name                                               ;
  4. ;  section [sTR] - name of section to set key value                                           ;
  5. ;  key     [sTR] - name of key to set value                                                   ;
  6. ;  value   [sTR] - value to set                                                               ;
  7. ; ------------------------------------------------------------------------------------------- ;
  8. ; If section not found then function will create section with given key and value             ;
  9. ; If key in given section not found, function will create key with supplied value in section  ;
  10. ; ------------------------------------------------------------------------------------------- ;
  11. ; (pz:INI-Set "example.ini" "SECTION1" "KEY2" "Value 1_2")                                    ;
  12. ; =========================================================================================== ;
  13. (defun pz:INI-Set ( file section key value / l sl fvalue i )
  14. (if
  15.    (and
  16.      (=(type file) 'STR)
  17.      (=(type section) 'STR)
  18.      (=(type key) 'STR)
  19.      (=(type value) 'STR)
  20.      (setq section (strcat "[" section "]"))
  21.      (setq l (pz:INI-Read file))
  22.    )
  23.    (progn
  24.      (if (setq sl (pz:AssocCI section l))
  25.        (setq
  26.          sl (cdr sl)
  27.          sl
  28.          (if (pz:AssocCI key sl)
  29.            (progn
  30.              (setq
  31.                fvalue (vl-string-subst value (pz:TrimStr(cadr(pz:AssocCI key sl))) (cadr(pz:AssocCI key sl)))
  32.              )
  33.              (subst
  34.                (list (car(pz:AssocCI key sl)) fvalue)
  35.                (pz:AssocCI key sl)
  36.                sl
  37.              )
  38.            )
  39.            (progn
  40.              (setq i (1-(length sl)))
  41.              (while (= (length(nth i sl)) 1)
  42.                (setq i (1- i))
  43.              )
  44.              (cd:LST_InsertItem (1+ i) sl (list key value))
  45.            )
  46.          )
  47.          l (subst (cons (car(pz:AssocCI section l)) sl) (pz:AssocCI section l) l)
  48.        )
  49.        (setq l (reverse (append (list(list (car(pz:AssocCI section l)) (list key value))) (reverse l) )))
  50.      )
  51.      (if (and (setq file (findfile file)) (setq file (open file "w")))
  52.        (progn
  53.          (foreach %1 l
  54.            (if (= (type (car %1)) 'STR)
  55.              (progn
  56.                (write-line (car %1) file)
  57.                (foreach key (cdr %1)
  58.                  (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
  59.                )
  60.              )
  61.              (foreach key %1
  62.                (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
  63.              )
  64.            )
  65.          )
  66.          (not (close file))
  67.        )
  68.      )
  69.    )
  70.    nil
  71. )
  72. )
  1. ; =========================================================================================== ;
  2. ; (pz:INI-Del file section key value)                                                         ;
  3. ;  file    [sTR]     - short or full path file name                                           ;
  4. ;  section [sTR]     - name of section to delete key or key value                             ;
  5. ;  key     [sTR/nil] - name of key to delete value or key                                     ;
  6. ;                      if nil then function will delete whole section with all it's keys      ;
  7. ;                      but will leave comments                                                ;
  8. ;  value   [nil/T]   - nil - delete key with value                                            ;
  9. ;                      T   - delete only value of given key                                   ;
  10. ; ------------------------------------------------------------------------------------------- ;
  11. ; (pz:INI-Del "example.ini" "SECTION4" "KEY2" nil)                                            ;
  12. ; =========================================================================================== ;
  13. (defun pz:INI-Del ( file section key value / l sectionlst sectionl  )
  14. (if
  15.    (and
  16.      (=(type file) 'STR)
  17.      (if (and section (=(type section) 'STR))
  18.        (setq section (strcat "[" section "]"))
  19.        nil
  20.      )
  21.      (setq l (pz:INI-Read file))
  22.    )
  23.    (cond
  24.      ( (not key)
  25.        (setq
  26.          sectionlst (pz:AssocCI section l )
  27.          sectionlst
  28.          (vl-remove-if-not
  29.            '(lambda (%1)
  30.              (if (=(type %1) 'LIST)
  31.                (=(length %1) 1)
  32.                nil
  33.              )
  34.            )
  35.            sectionlst
  36.          )
  37.          l (subst sectionlst (pz:AssocCI section l) l)
  38.        )
  39.      )
  40.      ( (not value)
  41.        (if
  42.          (and
  43.            (setq
  44.              sectionl (cdr(pz:AssocCI section l ))
  45.            )
  46.            (pz:AssocCI key sectionl)
  47.          )
  48.          (setq
  49.            sectionl (vl-remove (pz:AssocCI key sectionl) sectionl)
  50.            l (subst (cons (car(pz:AssocCI section l)) sectionl) (pz:AssocCI section l) l)
  51.          )
  52.        )
  53.      )
  54.      ( T
  55.        (if
  56.          (and
  57.            (setq
  58.              sectionl (cdr(pz:AssocCI section l ))
  59.            )
  60.            (pz:AssocCI key sectionl)
  61.          )
  62.          (setq sectionl
  63.            (subst
  64.              (list (car(pz:AssocCI key sectionl)) "")
  65.              (pz:AssocCI key sectionl)
  66.              sectionl
  67.            )
  68.            l (subst (cons (car(pz:AssocCI section l)) sectionl) (pz:AssocCI section l) l)
  69.          )
  70.        )
  71.      )
  72.    )
  73. )
  74. (if (and (setq file (findfile file)) (setq file (open file "w")))
  75.    (progn
  76.      (foreach %1 l
  77.        (if (= (type (car %1)) 'STR)
  78.          (progn
  79.            (write-line (car %1) file)
  80.            (foreach key (cdr %1)
  81.              (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
  82.            )
  83.          )
  84.          (foreach key %1
  85.            (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
  86.          )
  87.        )
  88.      )
  89.      (not (close file))
  90.    )
  91. )
  92. )

要运行
pz:INI-Get/pz:INI-Set/pz:INI-Del
必须加载这些子程序
  1. ; =========================================================================================== ;
  2. ; Subroutines                                                                                 ;
  3. ; =========================================================================================== ;
  4. (defun pz:TrimStr ( s )
  5. (vl-string-trim " \t" s)
  6. )
  7. (defun pz:AssocCI (e l / )
  8. (vl-some
  9.    '(lambda (%1)
  10.      (if
  11.        (and
  12.          (= (type (car %1)) 'STR)
  13.          (= (strcase e) (strcase (pz:TrimStr(car %1) )) )
  14.        )
  15.        %1
  16.      )
  17.    )
  18.    l
  19. )
  20. )
  21. (defun pz:INI-Read ( f / l res sub i )
  22. (if
  23.    (and
  24.      (setq l (cd:SYS_ReadFile nil f))
  25.      (= (type l) 'LIST)
  26.    )
  27.    (progn
  28.      (foreach %1 l
  29.        (cond
  30.          ( (and
  31.              (/= (substr (pz:TrimStr %1) 1 1) ";")
  32.              (/= (substr (pz:TrimStr %1) 1 1) "#")
  33.              (wcmatch (pz:TrimStr %1) "`[*`]")
  34.            )
  35.            (if sub
  36.              (setq res (cons (reverse sub) res))
  37.            )
  38.            (setq sub (list %1))
  39.          )
  40.          ( (and
  41.              (/= (substr (pz:TrimStr %1) 1 1) ";")
  42.              (/= (substr (pz:TrimStr %1) 1 1) "#")
  43.              (wcmatch %1 "*=*")
  44.            )
  45.            (setq i 0)
  46.            (while (/= "=" (substr %1 (setq i (1+ i)) 1)))
  47.            (setq sub (cons (list (substr %1 1 (1- i)) (substr %1 (1+ i))) sub))
  48.          )
  49.          ((setq sub (cons (list %1) sub)))
  50.        )
  51.      )
  52.      (if sub (setq res (cons (reverse sub) res)))
  53.    )
  54. )
  55. (reverse res)
  56. )
  57. ; =========================================================================================== ;
  58. ; Subroutines from:                                                                           ;
  59. ; CADPL-Pack-v1.lsp                                                                           ;
  60. ; =========================================================================================== ;
  61. ; =========================================================================================== ;
  62. ; Czyta plik tekstowy / Read a text file                                                      ;
  63. ;  Line [iNT/nil] - INT = numer linii pliku / file line number                                ;
  64. ;                   nil = caly plik / all lines of file                                       ;
  65. ;  File [sTR]     - nazwa pliku (krotka lub ze sciezka) / short or full path file name        ;
  66. ; ------------------------------------------------------------------------------------------- ;
  67. ; Zwraca / Return:                                                                            ;
  68. ;   nil = gdy Line = INT wieksze niz ilosc linii w pliku lub plik jest pusty /                ;
  69. ;         when Line = INT is greater then number of lines in file or file is empty            ;
  70. ;     0 = brak dostepu do pliku / no access to file                                           ;
  71. ;    -1 = nie znaleziono pliku / file not found                                               ;
  72. ;   STR = gdy Line = INT / when Line = INT                                                    ;
  73. ;  LIST = gdy Line = nil / when Line = nil                                                    ;
  74. ; ------------------------------------------------------------------------------------------- ;
  75. ; (cd:SYS_ReadFile nil "data.ini"), (cd:SYS_ReadFile 10 "acad.lin")                           ;
  76. ; =========================================================================================== ;
  77. (defun cd:SYS_ReadFile (Line File / fn fd l res)
  78. (if (setq fn (findfile File))
  79.    (if (setq fd (open fn "r"))
  80.      (progn
  81.        (if Line
  82.          (progn
  83.            (repeat Line (read-line fd))
  84.            (setq res (read-line fd))
  85.          )
  86.          (progn
  87.            (setq l T)
  88.            (while l
  89.              (setq res
  90.                (cons
  91.                  (setq l (read-line fd))
  92.                  res
  93.                )
  94.              )
  95.            )
  96.            (setq res (reverse (cdr res)))
  97.          )
  98.        )
  99.        (close fd)
  100.      )
  101.      (setq res 0)
  102.    )
  103.    (setq res -1)
  104. )
  105. res
  106. )
  107. ; =========================================================================================== ;
  108. ; Wstawia nowy element na liste / Inserts a new item in the list                              ;
  109. ;  Pos [iNT]  - pozycja elementu / element position                                           ;
  110. ;  Lst [list] - lista wejsciowa / input list                                                  ;
  111. ;  New [list/INT/REAL/STR/ENAME] - nowy element / new item                                    ;
  112. ; ------------------------------------------------------------------------------------------- ;
  113. ; (cd:LST_InsertItem 3 (list 0 1 2 4 5) 3)                                                    ;
  114. ; =========================================================================================== ;
  115. (defun cd:LST_InsertItem (Pos Lst New / res)
  116. (if (< -1 Pos (1+ (length Lst)))
  117.    (progn
  118.      (repeat Pos
  119.        (setq res (cons (car Lst) res)
  120.              Lst (cdr Lst)
  121.        )
  122.      )
  123.      (append (reverse res) (list New) Lst)
  124.    )
  125.    Lst
  126. )
  127. )
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 13:41 , Processed in 0.911966 second(s), 60 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表