userzhl 发表于 2009-6-23 20:48:00

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

<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/>--&gt;("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/>--&gt;"100" <br/>4.值不存在.则设置为默认值<br/>(ini-get "d:/diy.ini" "ORIGIN" "abc" nil)<br/>--&gt;nil<br/>(ini-get "d:/diy.ini" "ORIGIN" "abc" "500")<br/>--&gt;"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>

ayunger 发表于 2009-6-23 21:05:00

;;;*******************************************************<br/>;&nbsp;&nbsp; No.4-1 *.INI 初始化文件读取 函数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>; 参数: ini文件名(cad搜索目录下可省略路径),小节名,关键字&nbsp; <br/>;;;*******************************************************<br/>(defun ayIniRead (IniFile Section Keys / fp IsLoop IsThisSec xString xValue)<br/>&nbsp;(vl-load-com)<br/>&nbsp;(setq&nbsp;Section (strcase (strcat "[" Section "]")))<br/>&nbsp;(setq&nbsp;Keys (strcase (strcat Keys "=*")))<br/>&nbsp;(setq IsThisSec nil)<br/>&nbsp; (setq&nbsp;IsLoop T)<br/>&nbsp; (if (setq IniFile (findfile IniFile))<br/>&nbsp;&nbsp;&nbsp; (progn<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq fp (open IniFile "r"))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (while (and IsLoop (setq xString (read-line fp)))<br/>&nbsp;&nbsp;&nbsp;&nbsp;(if (= (strcase xString) Section) (setq IsThisSec T))<br/>&nbsp;&nbsp;&nbsp;&nbsp;(setq xstring (vl-string-trim " " xstring))<br/>&nbsp;&nbsp;&nbsp;&nbsp;(if (= xstring "") (setq IsThisSec nil))<br/>&nbsp;&nbsp;&nbsp;&nbsp;(if IsThisSec<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (wcmatch (strcase xString) Keys)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(progn<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(setq&nbsp;xValue (vl-string-trim " " (substr xString (strlen Keys))))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (= xValue "") (setq IsThisSec nil))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(setq IsLoop nil)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);end_progn<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);end_if<br/>&nbsp;&nbsp;&nbsp;&nbsp;);end_if<br/>&nbsp;&nbsp;&nbsp;);end_while<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (close fp)<br/>&nbsp;&nbsp;&nbsp; );end_progn<br/>&nbsp; );end_if<br/>&nbsp; (setq xValue (if (not xValue) "" xValue))<br/>);end_defun

userzhl 发表于 2009-6-23 22:11:00

<p>试了不行</p><p></p>

sailorcwx 发表于 2009-6-25 01:10:00

(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)
   )
   )
)
      )
    )
)


秋枫 发表于 2011-6-7 17:09:17

回复 sailorcwx 的帖子

谢谢 Sailorcwx。收藏。

lionguns 发表于 2011-6-7 17:14:25

我也收藏.
页: [1]
查看完整版本: [求助]请教高手,谁有这样一个函数?