123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- //******************************************************************************
- //*** LUA SCRIPT FUNCTIONS ***
- //*** ***
- //*** (c) Massimo Magnano 2005 ***
- //*** ***
- //*** ***
- //******************************************************************************
- // File : Lua_Files.pas
- //
- // Description : Access from Lua scripts to some file functions.
- //
- //******************************************************************************
- // Exported functions :
- //
- // CreateSearchRec {Path=string, Attr=integer} return TSearchRec object.
- // TSearchRec:FindFirst() return ErrorCode as Int.
- // TSearchRec:FindNext() return ErrorCode as Int.
- // TSearchRec:GetName() return FileName as string.
- // TSearchRec:GetTime() return FileTime as integer.
- // TSearchRec:GetSize() return FileSize as integer.
- // TSearchRec:GetAttr() return FileAttr as integer.
- // TSearchRec:IsDir() return IsDir as boolean.
- // TSearchRec:FindClose() free the object.
- //
- // CopyPath(string SourcePath, DestPath, wild [, integer ExistingFlags, boolean Recursive])
- // CopyFile(string SourceFile, DestPath [, integer ExistingFlags, string DestFileName])
- // DeleteDir(string BaseDir, SelName, boolean Recursive, RemoveDirs)
- // RegServer(string SourceFile)
- // UnRegServer(string SourceFile)
- // return Status as boolean.
- unit Lua_Files;
- interface
- uses Lua, Classes, Lua_FunctionsLog;
- const
- faOnlyFile =$27; //39 Decimal
- faAnyDir =$1F; //31 Decimal
- procedure RegisterFunctions(L: Plua_State);
- implementation
- uses Windows, ShellApi, LuaUtils, SysUtils, CopyRoutines;
- const
- HANDLE_SearchRecSTR ='Lua_Files_SearchRecHandle';
- type
- TLuaSearchRec = record
- Path :String;
- Attr :Integer;
- Rec :TSearchRec;
- end;
- PLuaSearchRec =^TLuaSearchRec;
- //=============== Lua Functions Files Enumeration ==============================
- function GetPLuaSearchRec(L: Plua_State; Index: Integer): PLuaSearchRec;
- begin
- Result := PLuaSearchRec(LuaGetTableLightUserData(L, Index, HANDLE_SearchRecSTR));
- if (Result=Nil)
- then raise Exception.Create('Unable to Get TSearchRec');
- end;
- function LuaFindFirstTSearchRec(L: Plua_State): Integer; cdecl;
- Var
- theRec :PLuaSearchRec;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- theRec :=GetPLuaSearchRec(L, 1);
- LuaPushInteger(L, FindFirst(theRec^.Path, theRec^.Attr, theRec^.Rec));
- Result :=1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- function LuaFindNextTSearchRec(L: Plua_State): Integer; cdecl;
- Var
- theRec :PLuaSearchRec;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- theRec :=GetPLuaSearchRec(L, 1);
- LuaPushInteger(L, FindNext(theRec^.Rec));
- Result :=1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- function LuaGetNameTSearchRec(L: Plua_State): Integer; cdecl;
- Var
- theRec :PLuaSearchRec;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- theRec :=GetPLuaSearchRec(L, 1);
- LuaPushString(L, theRec^.Rec.Name);
- Result :=1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- function LuaGetTimeTSearchRec(L: Plua_State): Integer; cdecl;
- Var
- theRec :PLuaSearchRec;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- theRec :=GetPLuaSearchRec(L, 1);
- LuaPushInteger(L, theRec^.Rec.Time);
- Result :=1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- function LuaGetSizeTSearchRec(L: Plua_State): Integer; cdecl;
- Var
- theRec :PLuaSearchRec;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- theRec :=GetPLuaSearchRec(L, 1);
- LuaPushInteger(L, theRec^.Rec.Size);
- Result :=1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- function LuaGetAttrTSearchRec(L: Plua_State): Integer; cdecl;
- Var
- theRec :PLuaSearchRec;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- theRec :=GetPLuaSearchRec(L, 1);
- LuaPushInteger(L, theRec^.Rec.Attr);
- Result :=1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- function LuaIsDirTSearchRec(L: Plua_State): Integer; cdecl;
- Var
- theRec :PLuaSearchRec;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- theRec :=GetPLuaSearchRec(L, 1);
- LuaPushBoolean(L, ((theRec^.Rec.Attr and $10)<>0) and
- ((theRec^.Rec.Name<>'.') and (theRec^.Rec.Name<>'..'))
- );
- Result :=1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- function LuaFindCloseTSearchRec(L: Plua_State): Integer; cdecl;
- Var
- theRec :PLuaSearchRec;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- theRec :=GetPLuaSearchRec(L, 1);
- FindClose(theRec^.Rec);
- FreeMem(theRec);
- LuaSetTableClear(L, 1);
- LuaPushBoolean(L, True);
- Result := 1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- function LuaCreateSearchRec(L: Plua_State): Integer; cdecl;
- Var
- Path :String;
- Attr :Integer;
- xResult :PLuaSearchRec;
- begin
- Result := 0;
- try
- Path :=LuaGetTableString(L, 1, 'Path');
- Attr :=LuaGetTableInteger(L, 1, 'Attr');
- LuaSetTableNil(L, 1, 'Path');
- LuaSetTableNil(L, 1, 'Attr');
- GetMem(xResult, sizeOf(TLuaSearchRec));
- if (xResult=Nil)
- then raise Exception.Create('Unable to Create TSearchRec');
- FillChar(xResult^, sizeOf(TLuaSearchRec), 0);
- xResult^.Attr :=Attr;
- xResult^.Path :=Path;
- FillChar(xResult^.Rec, sizeOf(TSearchRec), 0);
- LuaSetTableLightUserData(L, 1, HANDLE_SearchRecSTR, xResult);
- LuaSetTableFunction(L, 1, 'FindFirst', LuaFindFirstTSearchRec);
- LuaSetTableFunction(L, 1, 'FindNext', LuaFindNextTSearchRec);
- LuaSetTableFunction(L, 1, 'GetName', LuaGetNameTSearchRec);
- LuaSetTableFunction(L, 1, 'GetTime', LuaGetTimeTSearchRec);
- LuaSetTableFunction(L, 1, 'GetSize', LuaGetSizeTSearchRec);
- LuaSetTableFunction(L, 1, 'GetAttr', LuaGetAttrTSearchRec);
- LuaSetTableFunction(L, 1, 'IsDir', LuaIsDirTSearchRec);
- LuaSetTableFunction(L, 1, 'FindClose', LuaFindCloseTSearchRec);
- LuaSetTableFunction(L, 1, 'Free', LuaFindCloseTSearchRec);
- Result := 1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- //=============== Lua Functions Files Copy\Delete ==============================
- // CopyPath(string SourcePath, DestPath, wild [, integer ExistingFlags, boolean Recursive])
- function LuaCopyPath(L: Plua_State): Integer; cdecl;
- Var
- SourcePath,
- DestPath,
- wild :String;
- Recursive :Boolean;
- ExistingFlags,
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams>=3)
- then begin
- try
- Recursive :=True;
- ExistingFlags :=EXISTING_IF_ASK;
- SourcePath :=LuaToString(L, 1);
- DestPath :=LuaToString(L, 2);
- wild :=LuaToString(L, 3);
- if (NParams>=4) and
- (lua_isnumber(L, 4)<>0)
- then ExistingFlags :=LuaToInteger(L, 4);
- if (NParams>=5) and
- (lua_isboolean(L, 5))
- then Recursive :=LuaToBoolean(L, 5);
- CopyPath(SourcePath, DestPath, wild, ExistingFlags, Recursive);
- DoFunctionLog('CopyPath', '"'+SourcePath+'"', '"'+DestPath+'"',
- '"'+wild+'"');
- LuaPushBoolean(L, True);
- Result := 1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- // CopyFile(string SourceFile, DestPath, integer ExistingFlags, string DestFileName)
- function LuaCopyFile(L: Plua_State): Integer; cdecl;
- Var
- SourceFile,
- DestPath,
- DestFileName :String;
- ExistingFlags,
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams>=2)
- then begin
- try
- DestFileName :='';
- ExistingFlags :=EXISTING_IF_ASK;
- SourceFile :=LuaToString(L, 1);
- DestPath :=LuaToString(L, 2);
- if (NParams>=3) and
- (lua_isnumber(L, 3)<>0)
- then ExistingFlags :=LuaToInteger(L, 3);
- if (NParams>=4) and
- (lua_isstring(L, 4)<>0)
- then DestFileName :=LuaToString(L, 4);
- CopyFile(SourceFile, DestPath, ExistingFlags, DestFileName);
- DoFunctionLog('CopyFile', '"'+SourceFile+'"', '"'+DestPath+'"',
- IntToStr(ExistingFlags), '"'+DestFileName+'"');
- LuaPushBoolean(L, True);
- Result := 1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- // DeleteDir(string BaseDir, SelName, boolean Recursive, RemoveDirs)
- function LuaDeleteDir(L: Plua_State): Integer; cdecl;
- Var
- BaseDir,
- SelName :String;
- Recursive,
- RemoveDirs :Boolean;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=4)
- then begin
- try
- BaseDir :=LuaToString(L, 1);
- SelName :=LuaToString(L, 2);
- Recursive :=LuaToBoolean(L, 3);
- RemoveDirs :=LuaToBoolean(L, 4);
- DeleteDir(BaseDir, SelName, Recursive, RemoveDirs);
- DoFunctionLog('DeleteDir', '"'+BaseDir+'"', '"'+SelName+'"',
- BoolToStr(Recursive, True), BoolToStr(RemoveDirs, True));
- LuaPushBoolean(L, True);
- Result := 1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- // RegServer(string SourceFile)
- function LuaRegServer(L: Plua_State): Integer; cdecl;
- Var
- SourceFile :String;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- SourceFile :=LuaToString(L, 1);
- ShellExecute(GetDesktopWindow, 'open', PChar('REGSVR32'),
- PChar('/s '+'"'+SourceFile+'"'),
- Nil, SW_SHOWNORMAL);
- DoFunctionLog('RegServer', '"'+SourceFile+'"');
- LuaPushBoolean(L, True);
- Result := 1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- // UnRegServer(string SourceFile)
- function LuaUnRegServer(L: Plua_State): Integer; cdecl;
- Var
- SourceFile :String;
- NParams :Integer;
- begin
- Result := 0;
- NParams := lua_gettop(L);
- if (NParams=1)
- then begin
- try
- SourceFile :=LuaToString(L, 1);
- ShellExecute(GetDesktopWindow, 'open', PChar('REGSVR32'),
- PChar('/u /s '+'"'+SourceFile+'"'),
- Nil, SW_SHOWNORMAL);
- DoFunctionLog('UnRegServer', '"'+SourceFile+'"');
- LuaPushBoolean(L, True);
- Result := 1;
- except
- On E:Exception do begin
- //LuaError(L, ERR_Script+E.Message);
- end;
- end;
- end;
- end;
- procedure RegisterFunctions(L: Plua_State);
- begin
- LuaRegister(L, 'CreateSearchRec', LuaCreateSearchRec);
- LuaRegister(L, 'CopyPath', LuaCopyPath);
- LuaRegister(L, 'CopyFile', LuaCopyFile);
- LuaRegister(L, 'DeleteDir', LuaDeleteDir);
- LuaRegister(L, 'RegServer', LuaRegServer);
- LuaRegister(L, 'UnRegServer', LuaUnRegServer);
- end;
- end.
|