MiniM Additional Tools | Dec 28, 2011
$ZSEARCH function emulation for MiniM
On porting applications was developed developer may need to get file list by file mask. Cache DBMS uses for this purpose special $Z function $ZSEARCH. This function may be ported to MiniM without change any available code.
To emulate system $Z function in MiniM it is need to make special routine, with name prefix %ZFUNC. This routine must contain labels corresponded to emulated $Z functions. In the case of $ZSEARCH function need to be created two labels, ZSEARCH and ZSE. Second case is short abbreviation.
This labels must accept only one argument, target, and this argument mean the following:
Function returns empty string if the following file name does not exists or return next file name as full file name with directories and disk letter.
To emulate functionality of $ZSEARCH function we create routine %ZFUNCSEARCH and local variable %zsearch. Inside function emulation we check need or not to be created new file search and check have we next file to return or not. If next file exists we return one and forgot this file for next call:
ZSEARCH(target) ; if target is not empty string, create new file list i target'="" d makenew ; get next file number n n=$o(%zsearch("f","")),ret ; if files are out, return empty string i n="" s ret="" k %zsearch ; get next file name and remove from queue e s ret=%zsearch("f",n) k %zsearch("f",n) q ret
Function makenew entirely recreates local variable %zsearch by specified file mask:
makenew ; remove entire queue k %zsearch ; save current file pattern s %zsearch("p")=target n mask,file,dir,dev,saveio,saveeof ; normalize slashs and separate dir from file s mask=$tr(target,"/","\") s dir=mask,$p(dir,"\",$l(dir,"\"))="" ; if dir was not present, use current dir i dir="" s dir=$v("proc",7)_"\" s file=$p(mask,"\",$l(mask,"\")) ; call dir OS command and ; redirect possible error message about file not found s dev="|PIPE|dir /b "_$zquote(dir_file)_" 2> NUL" ; after execution current device need to be restored s saveio=$io o dev:"rt" u dev ; use $zeof end of device input indicator s saveeof=$v("proc",5,0) ; read all line by line f r file q:$zeof s %zsearch("f",$i(%zsearch("f")))=dir_file ; restore $zeof indicator behaviour i $v("proc",5,saveeof) ; restore current device u saveio ; close dir device c dev q
Here in one function have been used simultaneously several elements:
After import and compilation of function %ZFUNCSEARCH any MiniM process on call to system function $ZSEARCH calls emulation of this function with behaviour as like as in Cache:
n mask,file s mask="c:\downloads\*.exe" s file=$zsearch(mask) i file'="" d f s file=$zse("") q:file="" d . w file,! q
Download routine %ZFUNCSEARCH (zip, 1Kb)Eugene Karataev