[求助]请教高手,谁有这样一个函数?
<p>谁有这样一个函数?</p><p><font face="宋体" size="2">(ini-get inifile section key default)<br/><br/>功能及参数<br/>inifile 字符串---------寻找的INI文件名<br/>section 字符串或nil----INI文件内部的Section名称 <br/>key 字符串或nil----section内部的Key名称.<br/>default 字符串---------当无法查找到key时,返回的默认值. <br/><br/>1.如果仅给了inifile,返回section名称表.<br/>(ini-get "d:/diy.ini" nil nil nil)<br/>-->("ORIGIN" "OTHER")<br/><br/>2.如果给了inifile,section.返回key表<br/>(ini-get "d:/diy.ini" "ORIGIN" nil nil)<br/>("$diy_BL" "$diy_SZZG" "$diy_HZZG" )<br/><br/>3.如果给了inifile,section,key<br/>(ini-get "d:/diy.ini" "ORIGIN" "$diy_bl" nil)<br/>-->"100" <br/>4.值不存在.则设置为默认值<br/>(ini-get "d:/diy.ini" "ORIGIN" "abc" nil)<br/>-->nil<br/>(ini-get "d:/diy.ini" "ORIGIN" "abc" "500")<br/>-->"500"<br/><br/>----以下为"d:/diy.ini"文件内容---<br/><br/><br/>;比例<br/>$diy_BL=100<br/>$diy_SZZG=350 ;数字字高<br/>;汉字字高<br/>$diy_HZZG=500<br/><br/><br/>;diy_CC交叉打断命令断开的距离.<br/>$diy_CC_WIDTH=400</font></p> ;;;*******************************************************<br/>; No.4-1 *.INI 初始化文件读取 函数 <br/>; 参数: ini文件名(cad搜索目录下可省略路径),小节名,关键字 <br/>;;;*******************************************************<br/>(defun ayIniRead (IniFile Section Keys / fp IsLoop IsThisSec xString xValue)<br/> (vl-load-com)<br/> (setq Section (strcase (strcat "[" Section "]")))<br/> (setq Keys (strcase (strcat Keys "=*")))<br/> (setq IsThisSec nil)<br/> (setq IsLoop T)<br/> (if (setq IniFile (findfile IniFile))<br/> (progn<br/> (setq fp (open IniFile "r"))<br/> (while (and IsLoop (setq xString (read-line fp)))<br/> (if (= (strcase xString) Section) (setq IsThisSec T))<br/> (setq xstring (vl-string-trim " " xstring))<br/> (if (= xstring "") (setq IsThisSec nil))<br/> (if IsThisSec<br/> (if (wcmatch (strcase xString) Keys)<br/> (progn<br/> (setq xValue (vl-string-trim " " (substr xString (strlen Keys))))<br/> (if (= xValue "") (setq IsThisSec nil))<br/> (setq IsLoop nil)<br/> );end_progn<br/> );end_if<br/> );end_if<br/> );end_while<br/> (close fp)<br/> );end_progn<br/> );end_if<br/> (setq xValue (if (not xValue) "" xValue))<br/>);end_defun <p>试了不行</p><p></p> (defun ini-get(inifile section key default / FILEID I KEYVALUE SECTIONS STRREAD TMPKEY TMPKEYNAME TMPKEYVALUE TMPSECTION)
;判断文件是否存在
(if (and inifile (setq fileid (open inifile "R")))
;文件存在,读取文件内容构造表((section (key value)(key vlue)...)(section ....))
(progn
;按行读取
(while (setq strread (read-line fileid))
;根据第一个字符判断是section还是key
(cond
;section,读取section名称
((= (substr strread 1 1) "[")
;保存之前的section表
(if tmpsection (setq sections (append sections (list tmpsection))))
;构造新的section表
(setq tmpsection (list (substr strread 2 (- (strlen strread) 2))))
)
;key,读取keyname和value组成表添加进section表里面
((= (substr strread 1 1) "$")
(setq i (vl-string-search "=" strread)
tmpkeyname (substr strread 1 i)
tmpkeyvalue (substr strread (+ i 2))
tmpkey (list tmpkeyname tmpkeyvalue)
tmpsection (append tmpsection (list tmpkey))
)
)
)
)
(if tmpsection (setq sections (append sections (list tmpsection))))
(close fileid)
;按section,key,value一级级判断
;如果section没有值或所给值不在inifile中,按nil处理
(if (or (null section)(null (setq section (assoc section sections))))
;返回所有的section名称
(mapcar 'car sections)
;找到section
(progn
(setq section (cdr section))
;是否给出key
(if key
;在section中查找key
(if (setq key (assoc key section))
;找到key,返回key值
(cadr key)
;找不到key,返回default值
default
)
;没有给出key,返回section中的所有key
(mapcar 'car section)
)
)
)
)
)
)
回复 sailorcwx 的帖子
谢谢 Sailorcwx。收藏。 我也收藏.
页:
[1]