乐筑天下

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

[编程交流] 读取ini文件,但不读取命令

[复制链接]

3

主题

8

帖子

5

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-5 16:26:52 | 显示全部楼层 |阅读模式
您好,CadTutor用户,
 
我通过lisp ini文件读取(ini\u read“Myfile.ini”“OPTIONS”“Name1”)。
 
我的文件。ini:
 
  1. [OPTIONS]
  2. Name1=10
  3. Name2=210       
  4. Name3=0

 
这将很好地返回我“10”。
 
问题是,我想对ini文件进行注释,但不需要在结果中进行注释:
[选项]
名称1=10“注释1”
名称2=210“注释2”
名称3=0“注释3”
 
有人可以编辑我的代码吗?
如果注释以/comment或#comment或…
 
Lisp代码:
  1. (defun ini_read (inifile section entry )
  2. (if (and (= 'STR (type section)) (/= "[" (substr section 1 1))) (setq section (strcat "[" section "]")))
  3. (setq section (ini_readsection inifile section))
  4. (cadr (assoc entry section))
  5. )

 
  1. (defun ini_readsection (inifile section / ofile line result )
  2. (if (and (= 'STR (type section)) (/= "[" (substr section 1 1))) (setq section (strcat "[" section "]")))
  3. (if (findfile inifile)
  4.         (cdr (assoc section (ini_readini (findfile inifile))))
  5.         (alert (STRCAT inifile "\nNiet Gevonden!"))
  6. )
  7. )

 
谢谢你阅读我的问题
克里斯蒂娜
回复

使用道具 举报

rlx

21

主题

1505

帖子

1551

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
81
发表于 2022-7-5 16:45:38 | 显示全部楼层
海克里斯蒂娜,
 
 
zoiets?
 
 
  1. ; strip comment
  2. ; (strip-comment "hallo ; goodbey" ";")
  3. (defun strip-comment ( $s $c / i )(if (setq i (vl-string-position (ascii $c) $s 1))(substr $s 1 i) $s))

 
 
gr.Rlx
回复

使用道具 举报

3

主题

8

帖子

5

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-5 16:47:27 | 显示全部楼层
Dag Rlx,
 
 
非常感谢您的回复,但很抱歉,我不知道如何在我的代码中使用您的代码
 
 
希望你不介意我把代码放在附件里
测验拉链
回复

使用道具 举报

rlx

21

主题

1505

帖子

1551

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
81
发表于 2022-7-5 17:02:19 | 显示全部楼层
 
我看到您在ini值之后使用了空格(spatie)和制表符,所以我在制表符(“\t”)处打断ini值(字符串),然后删除空格。
 
如果ini值从不包含空格,也可以在空格字符处打断ini值
 
  1. (strip-comment (cadr (assoc entry section)) " ")

 
  1. ;(ini_read "Test.ini" "OPTIONS" "Name1")
  2. ;(ini_read (findfile "Test.ini") "OPTIONS" "Name1")
  3. (vl-load-com)
  4. (defun ini_read (inifile section entry / result)
  5.    (if (and (= 'STR (type section)) (/= "[" (substr section 1 1))) (setq section (strcat "[" section "]")))
  6.    (setq section (ini_readsection inifile section))
  7.      ; remove everything from the first tab ("\t")
  8.    (setq result (strip-comment (cadr (assoc entry section)) "\t"))
  9.      ; result is "10 " (with a space (spatie) , so remove space "10 " -> "10"
  10.      (vl-string-right-trim " " result)
  11. ; you can combine line 2,3 & 4 into one line :
  12. ; (vl-string-right-trim " " (strip-comment (cadr (assoc entry (ini_readsection inifile section))) "\t"))
  13. )
  14. ;(vl-string-right-trim character-set string)
  15. ; strip comment
  16. ; (strip-comment "hallo ; goodbye" ";")
  17. (defun strip-comment ( $s $c / i )(if (setq i (vl-string-position (ascii $c) $s 1))(substr $s 1 i) $s))

 
gr.Rlx
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 17:10:38 | 显示全部楼层
看看这篇文章的李mac代码,你可以使用逗号作为分隔符,并使列表非常容易。
 
http://www.cadtutor.net/forum/showthread.php?100466-Lisp用于从多行文字中删除一行
回复

使用道具 举报

3

主题

8

帖子

5

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-5 17:18:36 | 显示全部楼层
Dag Rlx,
 
这件作品很有魅力,非常感谢你的解释。
最后,我有了我想要的ini文件(mijn dag kan niet meer stuk)。
格罗杰斯,
克里斯蒂娜
@比加尔,
非常感谢您的链接,我们会查看它
回复

使用道具 举报

rlx

21

主题

1505

帖子

1551

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
81
发表于 2022-7-5 17:28:17 | 显示全部楼层
 
 
欢迎你,Christina_,succes met je appie:-)
 
 
gr.Rlx
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-7-12 12:28 , Processed in 1.130410 second(s), 66 queries .

© 2020-2025 乐筑天下

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