在 cds.lib 文件中定义库的路径,为了规范管理库的定义,经常这样做:
$ tree
.
|-- cds.lib ------------------- cat --> 1| INCLUDE ./common/cds.lib
-- common
|-- cds.lib --------------- cat --> 1| INCLUDE ./project/cds.lib
| 2| INCLUDE ./project/cds.lib
|-- project
| |-- cds.lib ----------- cat --> 1| INCLUDE ./layout/cds.lib
| | 2| INCLUDE ./sch/cds.lib
| | 3| 略 ...
| |-- layout
| |
-- cds.lib ------- cat --> 1| DEFINE layout_lib1 ./layout_lib1
| | 2| DEFINE layout_lib2 ./layout_lib2
| | 3| 略略 ...
| |-- sch
| | -- cds.lib ------- cat --> 1| 略略略 ...
|
-- 略略略略 ...
`-- 略略略略略 ...
可以看到,对于 cds.lib 文件来说, INCLDUE
/ DEFINE
都可以使用相对路径。
而在 Skill 中使用 load()
/ loadi()
函数却不能支持相对路径:
Error load: can’t access file – “./dir/test.il”
Error load: error while loading file – “/home/yeung/tmp/load.il” at line 1
几个函数
用来获取当前文件的路径,功能类似 Perl 中的 __FILE__
函数。
procedure(ycGetFileName(\@optional fileName "g")
let((self dir)
self = get_filename(piport)
when(fileName
unless(stringp(fileName)
error("ycGetFileName: argument #1 should be a string - %A" fileName)
)
dir = ycGetDirName(self)
self = strcat(dir "/" fileName)
)
self
)
); ycGetFileName
- 创建一个
/home/yeung/tmp/load.il
脚本并运行: - 脚本内容
printf("Current path: %s\n" ycGetFileName())
- 运行
load("/home/yeung/tmp/load.il")
; Current path: /home/yeung/tmp/load.il
正则处理,从 ycGetFileName
函数结果中提取父目录的路径。
需要注意的是,如果是在 CIW 中执行 ycGetFileName
函数,它的返回值会是 *ciwInPort*
,这种情况直接用 getWorkingDir()
作为 ycGetDirName
函数的返回值。
procedure(ycGetDirName(\@optional file(ycGetFileName()) rel "tg")
let((dir)
dir = if(pcreMatchp("^\\*.+\\*$" file)
then
getWorkingDir()
else
unless(pcreMatchp("^[~/]|^\\.{1,2}/" file)
file = strcat("./" file)
)
pcreReplace(pcreCompile("(?
- 还是修改
/home/yeung/tmp/load.il
文件来演示: - 脚本内容
printf("Current directory: %s\n" ycGetDirName())
- 运行
load("/home/yeung/tmp/load.il")
; Current directory: /home/yeung/tmp
这个函数用来将 相对路径 转为 绝对路径 。
当输入路径 path 为 ~
或者 /
开头时,就不需要依赖目标文件的路径了,这种情况直接传递给 simplifyFilename
函数获取绝对路径。
procedure(ycReadRelPath(\@optional file("") "t")
let((path)
path = if(pcreMatchp("^[~/]" file)
file
sprintf(nil "%s/%s" ycGetDirName() file)
)
simplifyFilename(path t)
)
); ycReadRelPath
- 运行:
printf("Target path: %s\n" ycReadRelPath("./init/load.il"))
; Target path: /home/yeung/tmp/dir/test.il
优化 load
函数。
预留一个 ignore
参数用来优化 loadi
函数。
procedure(ycLoad(file \@optional pwd ignore "tgg")
let((path args)
path = ycReadRelPath(file)
unless(isFile(path)
error("ycLoad: not a valid file - %A" path)
)
unless(isReadable(path)
error("ycLoad: can't access file - %A" path)
)
args = list(if(ignore 'loadi 'load) path)
when(pwd
unless(stringp(pwd)
error("ycLoad: argument #2 should be a string - %A" pwd)
)
args = append1(args pwd)
)
apply('funcall args)
)
); ycLoad
优化 loadi
函数。
同样可以忽略 load 过程中遇到的错误,打印错误消息,然后继续 load 。
procedure(ycLoadi(file \@optional pwd "tg")
ycLoad(file pwd t)
); ycLoadi
- 以前 load
let((home wind sync)
home = "/home/yeung/script/skill/tools/"
wind = list("hiForm.il" "windCtrl.il")
sync = list("syncView.il" "support3rd.il")
foreach(x wind
load(strcat(home "src/wind/" x))
)
foreach(x sync
load(strcat(home "/src/sync/" x))
)
); let
非常啰嗦。
其次如果父文件夹改了名,或者移动了位置,还得一个个文件打开检查路径并修改,还要增加无意义的版本。
copy 给别人也不能傻瓜式操作,直接 load 报个错: Error load: can’t access” 唉 你 这 脚 本 有 问 题 啊 ? ”
* 现在 load
ycLoad("src/wind/hiForm.il")
ycLoad("src/wind/windCtrl.il")
ycLoad("src/sync/syncView.il")
ycLoad("src/sync/support3rd.il")
; END 这里为什么空一行,在 FAQ 会解释
“优雅地” load 完所有文件。
继续优化可以再识别 Shell 环境中的变量。
FAQ
Original: https://www.cnblogs.com/yeungchie/p/15999427.html
Author: YEUNGCHIE
Title: [ Skill ] load 函数优化,识别相对路径
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/619467/
转载文章受原作者版权保护。转载请注明原作者出处!