好的,我希望这是最终结果
它应该如何工作:
-函数将保留ini文件格式(制表符/空格)
-节和kay名称不区分大小写
-评论可以从开始;或#
ini文件示例(要测试我的*ini*功能,请在cad搜索路径中创建文件Example.ini):
- #I hope
- #it will be
-
- [sECTION1]
- KEY1 = VALUE 1_1
- ;useful
- KEY2 =
- KEY3 = VALUE 1_3
- [sECTION2]
- KEY1 = VALUE 2_1
- KEY2 = VALUE 2_2
- KEY3=VALUE 2_3
- [sECTION3]
- KEY1=VALUE 3_1
- KEY2=VALUE 3_2
-
- KEY3=VALUE 3_3
- [sECTION4]
- KEY1 = VALUE 4_1
- KEY2 = VALUE 4_2
- KEY3 = VALUE 4_3
- ;for someone
My*ini*函数(基于第三篇文章中给出的源代码)
- ; =========================================================================================== ;
- ; (pz:INI-Get file section key value) ;
- ; file [sTR] - short or full path file name ;
- ; section [sTR/nil] - name of section to read key ;
- ; if nil then function will return all section names from .ini file ;
- ; key [sTR/nil] - name of key to read value ;
- ; if nil then function will return all key names from section ;
- ; if key value is "" then returns nil ;
- ; value [sTR/nil] - default value if section or key not found ;
- ; ------------------------------------------------------------------------------------------- ;
- ; (pz:INI-Get "example.ini" "SECTION1" "KEY2" nil) ;
- ; =========================================================================================== ;
- (defun pz:INI-Get ( file section key value / l res tmpl )
- (if (and section (=(type section) 'STR))
- (setq section (strcat "[" section "]"))
- )
- (if (setq l (pz:INI-Read file))
- (cond
- ( (not section)
- (foreach %1 l
- (if (= (type (car %1)) 'STR)
- (setq res
- (cons
- (substr
- (pz:TrimStr (car %1))
- 2
- (- (strlen (pz:TrimStr (car %1))) 2)
- )
- res
- )
- )
- )
- )
- (setq res (reverse res))
- )
- ( (and
- (not key)
- (setq tmpl (pz:AssocCI section l))
- )
- (foreach %1 (cdr tmpl)
- (if (= (length %1) 2)
- (setq res (cons (pz:TrimStr(car %1)) res))
- )
- )
- (setq res (reverse res))
- )
- ( (and
- key
- (setq section (cdr (pz:AssocCI section l)))
- )
- (if (setq res (pz:AssocCI key section))
- (if (= (setq res (pz:TrimStr(cadr res))) "")
- (setq res nil)
- )
- (setq res value)
- )
- )
- ( T (setq res value))
- )
- (setq res value)
- )
- res
- )
|