我想爲Tcl編寫一個自定義文件系統使用Tclapi(它的工作相關,不會進入細節),但我堅持嘗試弄清楚爲什麼這不起作用。tcl「打開」命令不工作時替換Tcl_Filesystem與重複
在這段代碼中,我得到原始/本機Tcl_Filesystem,將其所有內容(函數指針)複製到my_fs,然後在my_fs上調用Tcl_FSRegister。很簡單,認爲它應該工作。
// global scope
const Tcl_Filesystem *ori_fs;
Tcl_Filesystem *my_fs;
...
// in Init
// Get the original Tcl_Filesystem.
Tcl_Obj *root_obj = Tcl_NewStringObj("/", -1);
Tcl_IncrRefCount(root_obj);
ori_fs = Tcl_FSGetFileSystemForPath(root_obj);
Tcl_DecrRefCount(root_obj);
// create a duplicate of the original Tcl_Filesystem struct.
my_fs = malloc(sizeof(Tcl_Filesystem));
memmove(my_fs, ori_fs, ori_fs->structureLength);
int ret = Tcl_FSRegister((ClientData)1, my_fs);
if (ret == TCL_ERROR) {
...
當我跑
load <path to .so>/my_fs[info sharedlibextension]
# sanity check
puts [pwd]
set fp [open test.txt]
不過,我得到這個
<my current directory>
while executing
"open test.txt"
invoked from within
"set fp [open test.txt]"
(file "test.tcl" line 3)
注意如何 「把[密碼]」 的作品,但不是 「開放的test.txt」?
在調用Tcl_FSRegister時用「ori_fs」替換「my_fs」似乎可行... 我已經花費了太多的時間來解決這個問題。如果有人能幫助我,我將不勝感激!