明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1315|回复: 5

[求助]请教高手,谁有这样一个函数?

[复制链接]
发表于 2009-6-23 20:48:00 | 显示全部楼层 |阅读模式

谁有这样一个函数?

(ini-get inifile section key default)

功能及参数
inifile 字符串---------寻找的INI文件名
section 字符串或nil----INI文件内部的Section名称
key 字符串或nil----section内部的Key名称.
default 字符串---------当无法查找到key时,返回的默认值.

1.如果仅给了inifile,返回section名称表.
(ini-get "d:/diy.ini" nil nil nil)
-->("ORIGIN" "OTHER")

2.如果给了inifile,section.返回key表
(ini-get "d:/diy.ini" "ORIGIN" nil nil)
("$diy_BL" "$diy_SZZG" "$diy_HZZG" )

3.如果给了inifile,section,key
(ini-get "d:/diy.ini" "ORIGIN" "$diy_bl" nil)
-->"100"
4.值不存在.则设置为默认值
(ini-get "d:/diy.ini" "ORIGIN" "abc" nil)
-->nil
(ini-get "d:/diy.ini" "ORIGIN" "abc" "500")
-->"500"

----以下为"d:/diy.ini"文件内容---

[ORIGIN]
;比例
$diy_BL=100
$diy_SZZG=350 ;数字字高
;汉字字高
$diy_HZZG=500

[OTHER]
;diy_CC交叉打断命令断开的距离.
$diy_CC_WIDTH=400

"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2009-6-23 21:05:00 | 显示全部楼层
;;;*******************************************************
;   No.4-1 *.INI 初始化文件读取 函数                     
; 参数: ini文件名(cad搜索目录下可省略路径),小节名,关键字 
;;;*******************************************************
(defun ayIniRead (IniFile Section Keys / fp IsLoop IsThisSec xString xValue)
 (vl-load-com)
 (setq Section (strcase (strcat "[" Section "]")))
 (setq Keys (strcase (strcat Keys "=*")))
 (setq IsThisSec nil)
  (setq IsLoop T)
  (if (setq IniFile (findfile IniFile))
    (progn
      (setq fp (open IniFile "r"))
      (while (and IsLoop (setq xString (read-line fp)))
    (if (= (strcase xString) Section) (setq IsThisSec T))
    (setq xstring (vl-string-trim " " xstring))
    (if (= xstring "") (setq IsThisSec nil))
    (if IsThisSec
        (if (wcmatch (strcase xString) Keys)
      (progn
       (setq xValue (vl-string-trim " " (substr xString (strlen Keys))))
       (if (= xValue "") (setq IsThisSec nil))
       (setq IsLoop nil)
      );end_progn
     );end_if
    );end_if
   );end_while
      (close fp)
    );end_progn
  );end_if
  (setq xValue (if (not xValue) "" xValue))
);end_defun
 楼主| 发表于 2009-6-23 22:11:00 | 显示全部楼层

试了不行

发表于 2009-6-25 01:10:00 | 显示全部楼层
  1. (defun ini-get(inifile section key default / FILEID I KEYVALUE SECTIONS STRREAD TMPKEY TMPKEYNAME TMPKEYVALUE TMPSECTION)
  2.   ;判断文件是否存在
  3.   (if (and inifile (setq fileid (open inifile "R")))
  4.     ;文件存在,读取文件内容构造表((section (key value)(key vlue)...)(section ....))
  5.     (progn
  6.       ;按行读取
  7.       (while (setq strread (read-line fileid))
  8. ;根据第一个字符判断是section还是key
  9. (cond
  10.    ;section,读取section名称
  11.    ((= (substr strread 1 1) "[")
  12.     ;保存之前的section表
  13.     (if tmpsection (setq sections (append sections (list tmpsection))))
  14.     ;构造新的section表
  15.     (setq tmpsection (list (substr strread 2 (- (strlen strread) 2))))
  16.     )
  17.    ;key,读取keyname和value组成表添加进section表里面
  18.    ((= (substr strread 1 1) "$")
  19.     (setq i (vl-string-search "=" strread)
  20.    tmpkeyname (substr strread 1 i)
  21.    tmpkeyvalue (substr strread (+ i 2))
  22.    tmpkey (list tmpkeyname tmpkeyvalue)
  23.    tmpsection (append tmpsection (list tmpkey))
  24.    )
  25.     )
  26.    )
  27. )
  28.       (if tmpsection (setq sections (append sections (list tmpsection))))
  29.       (close fileid)
  30.       ;按section,key,value一级级判断
  31.       ;如果section没有值或所给值不在inifile中,按nil处理
  32.       (if (or (null section)(null (setq section (assoc section sections))))
  33. ;返回所有的section名称
  34. (mapcar 'car sections)
  35. ;找到section
  36. (progn
  37.    
  38.    (setq section (cdr section))
  39.    ;是否给出key
  40.    (if key
  41.      ;在section中查找key
  42.      (if (setq key (assoc key section))
  43.        ;找到key,返回key值
  44.        (cadr key)
  45.        ;找不到key,返回default值
  46.        default
  47.       
  48.        )
  49.      ;没有给出key,返回section中的所有key
  50.      (mapcar 'car section)
  51.      )
  52.    )
  53. )
  54.       )
  55.     )
  56.   )
发表于 2011-6-7 17:09:17 | 显示全部楼层
回复 sailorcwx 的帖子

谢谢 Sailorcwx。收藏。
发表于 2011-6-7 17:14:25 | 显示全部楼层
我也收藏.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-7-26 08:37 , Processed in 0.170893 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表