Lua_Files.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. //******************************************************************************
  2. //*** LUA SCRIPT FUNCTIONS ***
  3. //*** ***
  4. //*** (c) Massimo Magnano 2005 ***
  5. //*** ***
  6. //*** ***
  7. //******************************************************************************
  8. // File : Lua_Files.pas
  9. //
  10. // Description : Access from Lua scripts to some file functions.
  11. //
  12. //******************************************************************************
  13. // Exported functions :
  14. //
  15. // CreateSearchRec {Path=string, Attr=integer} return TSearchRec object.
  16. // TSearchRec:FindFirst() return ErrorCode as Int.
  17. // TSearchRec:FindNext() return ErrorCode as Int.
  18. // TSearchRec:GetName() return FileName as string.
  19. // TSearchRec:GetTime() return FileTime as integer.
  20. // TSearchRec:GetSize() return FileSize as integer.
  21. // TSearchRec:GetAttr() return FileAttr as integer.
  22. // TSearchRec:IsDir() return IsDir as boolean.
  23. // TSearchRec:FindClose() free the object.
  24. //
  25. // CopyPath(string SourcePath, DestPath, wild [, integer ExistingFlags, boolean Recursive])
  26. // CopyFile(string SourceFile, DestPath [, integer ExistingFlags, string DestFileName])
  27. // DeleteDir(string BaseDir, SelName, boolean Recursive, RemoveDirs)
  28. // RegServer(string SourceFile)
  29. // UnRegServer(string SourceFile)
  30. // return Status as boolean.
  31. unit Lua_Files;
  32. interface
  33. uses Lua, Classes, Lua_FunctionsLog;
  34. const
  35. faOnlyFile =$27; //39 Decimal
  36. faAnyDir =$1F; //31 Decimal
  37. procedure RegisterFunctions(L: Plua_State);
  38. implementation
  39. uses Windows, ShellApi, LuaUtils, SysUtils, CopyRoutines;
  40. const
  41. HANDLE_SearchRecSTR ='Lua_Files_SearchRecHandle';
  42. type
  43. TLuaSearchRec = record
  44. Path :String;
  45. Attr :Integer;
  46. Rec :TSearchRec;
  47. end;
  48. PLuaSearchRec =^TLuaSearchRec;
  49. //=============== Lua Functions Files Enumeration ==============================
  50. function GetPLuaSearchRec(L: Plua_State; Index: Integer): PLuaSearchRec;
  51. begin
  52. Result := PLuaSearchRec(LuaGetTableLightUserData(L, Index, HANDLE_SearchRecSTR));
  53. if (Result=Nil)
  54. then raise Exception.Create('Unable to Get TSearchRec');
  55. end;
  56. function LuaFindFirstTSearchRec(L: Plua_State): Integer; cdecl;
  57. Var
  58. theRec :PLuaSearchRec;
  59. NParams :Integer;
  60. begin
  61. Result := 0;
  62. NParams := lua_gettop(L);
  63. if (NParams=1)
  64. then begin
  65. try
  66. theRec :=GetPLuaSearchRec(L, 1);
  67. LuaPushInteger(L, FindFirst(theRec^.Path, theRec^.Attr, theRec^.Rec));
  68. Result :=1;
  69. except
  70. On E:Exception do begin
  71. //LuaError(L, ERR_Script+E.Message);
  72. end;
  73. end;
  74. end;
  75. end;
  76. function LuaFindNextTSearchRec(L: Plua_State): Integer; cdecl;
  77. Var
  78. theRec :PLuaSearchRec;
  79. NParams :Integer;
  80. begin
  81. Result := 0;
  82. NParams := lua_gettop(L);
  83. if (NParams=1)
  84. then begin
  85. try
  86. theRec :=GetPLuaSearchRec(L, 1);
  87. LuaPushInteger(L, FindNext(theRec^.Rec));
  88. Result :=1;
  89. except
  90. On E:Exception do begin
  91. //LuaError(L, ERR_Script+E.Message);
  92. end;
  93. end;
  94. end;
  95. end;
  96. function LuaGetNameTSearchRec(L: Plua_State): Integer; cdecl;
  97. Var
  98. theRec :PLuaSearchRec;
  99. NParams :Integer;
  100. begin
  101. Result := 0;
  102. NParams := lua_gettop(L);
  103. if (NParams=1)
  104. then begin
  105. try
  106. theRec :=GetPLuaSearchRec(L, 1);
  107. LuaPushString(L, theRec^.Rec.Name);
  108. Result :=1;
  109. except
  110. On E:Exception do begin
  111. //LuaError(L, ERR_Script+E.Message);
  112. end;
  113. end;
  114. end;
  115. end;
  116. function LuaGetTimeTSearchRec(L: Plua_State): Integer; cdecl;
  117. Var
  118. theRec :PLuaSearchRec;
  119. NParams :Integer;
  120. begin
  121. Result := 0;
  122. NParams := lua_gettop(L);
  123. if (NParams=1)
  124. then begin
  125. try
  126. theRec :=GetPLuaSearchRec(L, 1);
  127. LuaPushInteger(L, theRec^.Rec.Time);
  128. Result :=1;
  129. except
  130. On E:Exception do begin
  131. //LuaError(L, ERR_Script+E.Message);
  132. end;
  133. end;
  134. end;
  135. end;
  136. function LuaGetSizeTSearchRec(L: Plua_State): Integer; cdecl;
  137. Var
  138. theRec :PLuaSearchRec;
  139. NParams :Integer;
  140. begin
  141. Result := 0;
  142. NParams := lua_gettop(L);
  143. if (NParams=1)
  144. then begin
  145. try
  146. theRec :=GetPLuaSearchRec(L, 1);
  147. LuaPushInteger(L, theRec^.Rec.Size);
  148. Result :=1;
  149. except
  150. On E:Exception do begin
  151. //LuaError(L, ERR_Script+E.Message);
  152. end;
  153. end;
  154. end;
  155. end;
  156. function LuaGetAttrTSearchRec(L: Plua_State): Integer; cdecl;
  157. Var
  158. theRec :PLuaSearchRec;
  159. NParams :Integer;
  160. begin
  161. Result := 0;
  162. NParams := lua_gettop(L);
  163. if (NParams=1)
  164. then begin
  165. try
  166. theRec :=GetPLuaSearchRec(L, 1);
  167. LuaPushInteger(L, theRec^.Rec.Attr);
  168. Result :=1;
  169. except
  170. On E:Exception do begin
  171. //LuaError(L, ERR_Script+E.Message);
  172. end;
  173. end;
  174. end;
  175. end;
  176. function LuaIsDirTSearchRec(L: Plua_State): Integer; cdecl;
  177. Var
  178. theRec :PLuaSearchRec;
  179. NParams :Integer;
  180. begin
  181. Result := 0;
  182. NParams := lua_gettop(L);
  183. if (NParams=1)
  184. then begin
  185. try
  186. theRec :=GetPLuaSearchRec(L, 1);
  187. LuaPushBoolean(L, ((theRec^.Rec.Attr and $10)<>0) and
  188. ((theRec^.Rec.Name<>'.') and (theRec^.Rec.Name<>'..'))
  189. );
  190. Result :=1;
  191. except
  192. On E:Exception do begin
  193. //LuaError(L, ERR_Script+E.Message);
  194. end;
  195. end;
  196. end;
  197. end;
  198. function LuaFindCloseTSearchRec(L: Plua_State): Integer; cdecl;
  199. Var
  200. theRec :PLuaSearchRec;
  201. NParams :Integer;
  202. begin
  203. Result := 0;
  204. NParams := lua_gettop(L);
  205. if (NParams=1)
  206. then begin
  207. try
  208. theRec :=GetPLuaSearchRec(L, 1);
  209. FindClose(theRec^.Rec);
  210. FreeMem(theRec);
  211. LuaSetTableClear(L, 1);
  212. LuaPushBoolean(L, True);
  213. Result := 1;
  214. except
  215. On E:Exception do begin
  216. //LuaError(L, ERR_Script+E.Message);
  217. end;
  218. end;
  219. end;
  220. end;
  221. function LuaCreateSearchRec(L: Plua_State): Integer; cdecl;
  222. Var
  223. Path :String;
  224. Attr :Integer;
  225. xResult :PLuaSearchRec;
  226. begin
  227. Result := 0;
  228. try
  229. Path :=LuaGetTableString(L, 1, 'Path');
  230. Attr :=LuaGetTableInteger(L, 1, 'Attr');
  231. LuaSetTableNil(L, 1, 'Path');
  232. LuaSetTableNil(L, 1, 'Attr');
  233. GetMem(xResult, sizeOf(TLuaSearchRec));
  234. if (xResult=Nil)
  235. then raise Exception.Create('Unable to Create TSearchRec');
  236. FillChar(xResult^, sizeOf(TLuaSearchRec), 0);
  237. xResult^.Attr :=Attr;
  238. xResult^.Path :=Path;
  239. FillChar(xResult^.Rec, sizeOf(TSearchRec), 0);
  240. LuaSetTableLightUserData(L, 1, HANDLE_SearchRecSTR, xResult);
  241. LuaSetTableFunction(L, 1, 'FindFirst', LuaFindFirstTSearchRec);
  242. LuaSetTableFunction(L, 1, 'FindNext', LuaFindNextTSearchRec);
  243. LuaSetTableFunction(L, 1, 'GetName', LuaGetNameTSearchRec);
  244. LuaSetTableFunction(L, 1, 'GetTime', LuaGetTimeTSearchRec);
  245. LuaSetTableFunction(L, 1, 'GetSize', LuaGetSizeTSearchRec);
  246. LuaSetTableFunction(L, 1, 'GetAttr', LuaGetAttrTSearchRec);
  247. LuaSetTableFunction(L, 1, 'IsDir', LuaIsDirTSearchRec);
  248. LuaSetTableFunction(L, 1, 'FindClose', LuaFindCloseTSearchRec);
  249. LuaSetTableFunction(L, 1, 'Free', LuaFindCloseTSearchRec);
  250. Result := 1;
  251. except
  252. On E:Exception do begin
  253. //LuaError(L, ERR_Script+E.Message);
  254. end;
  255. end;
  256. end;
  257. //=============== Lua Functions Files Copy\Delete ==============================
  258. // CopyPath(string SourcePath, DestPath, wild [, integer ExistingFlags, boolean Recursive])
  259. function LuaCopyPath(L: Plua_State): Integer; cdecl;
  260. Var
  261. SourcePath,
  262. DestPath,
  263. wild :String;
  264. Recursive :Boolean;
  265. ExistingFlags,
  266. NParams :Integer;
  267. begin
  268. Result := 0;
  269. NParams := lua_gettop(L);
  270. if (NParams>=3)
  271. then begin
  272. try
  273. Recursive :=True;
  274. ExistingFlags :=EXISTING_IF_ASK;
  275. SourcePath :=LuaToString(L, 1);
  276. DestPath :=LuaToString(L, 2);
  277. wild :=LuaToString(L, 3);
  278. if (NParams>=4) and
  279. (lua_isnumber(L, 4)<>0)
  280. then ExistingFlags :=LuaToInteger(L, 4);
  281. if (NParams>=5) and
  282. (lua_isboolean(L, 5))
  283. then Recursive :=LuaToBoolean(L, 5);
  284. CopyPath(SourcePath, DestPath, wild, ExistingFlags, Recursive);
  285. DoFunctionLog('CopyPath', '"'+SourcePath+'"', '"'+DestPath+'"',
  286. '"'+wild+'"');
  287. LuaPushBoolean(L, True);
  288. Result := 1;
  289. except
  290. On E:Exception do begin
  291. //LuaError(L, ERR_Script+E.Message);
  292. end;
  293. end;
  294. end;
  295. end;
  296. // CopyFile(string SourceFile, DestPath, integer ExistingFlags, string DestFileName)
  297. function LuaCopyFile(L: Plua_State): Integer; cdecl;
  298. Var
  299. SourceFile,
  300. DestPath,
  301. DestFileName :String;
  302. ExistingFlags,
  303. NParams :Integer;
  304. begin
  305. Result := 0;
  306. NParams := lua_gettop(L);
  307. if (NParams>=2)
  308. then begin
  309. try
  310. DestFileName :='';
  311. ExistingFlags :=EXISTING_IF_ASK;
  312. SourceFile :=LuaToString(L, 1);
  313. DestPath :=LuaToString(L, 2);
  314. if (NParams>=3) and
  315. (lua_isnumber(L, 3)<>0)
  316. then ExistingFlags :=LuaToInteger(L, 3);
  317. if (NParams>=4) and
  318. (lua_isstring(L, 4)<>0)
  319. then DestFileName :=LuaToString(L, 4);
  320. CopyFile(SourceFile, DestPath, ExistingFlags, DestFileName);
  321. DoFunctionLog('CopyFile', '"'+SourceFile+'"', '"'+DestPath+'"',
  322. IntToStr(ExistingFlags), '"'+DestFileName+'"');
  323. LuaPushBoolean(L, True);
  324. Result := 1;
  325. except
  326. On E:Exception do begin
  327. //LuaError(L, ERR_Script+E.Message);
  328. end;
  329. end;
  330. end;
  331. end;
  332. // DeleteDir(string BaseDir, SelName, boolean Recursive, RemoveDirs)
  333. function LuaDeleteDir(L: Plua_State): Integer; cdecl;
  334. Var
  335. BaseDir,
  336. SelName :String;
  337. Recursive,
  338. RemoveDirs :Boolean;
  339. NParams :Integer;
  340. begin
  341. Result := 0;
  342. NParams := lua_gettop(L);
  343. if (NParams=4)
  344. then begin
  345. try
  346. BaseDir :=LuaToString(L, 1);
  347. SelName :=LuaToString(L, 2);
  348. Recursive :=LuaToBoolean(L, 3);
  349. RemoveDirs :=LuaToBoolean(L, 4);
  350. DeleteDir(BaseDir, SelName, Recursive, RemoveDirs);
  351. DoFunctionLog('DeleteDir', '"'+BaseDir+'"', '"'+SelName+'"',
  352. BoolToStr(Recursive, True), BoolToStr(RemoveDirs, True));
  353. LuaPushBoolean(L, True);
  354. Result := 1;
  355. except
  356. On E:Exception do begin
  357. //LuaError(L, ERR_Script+E.Message);
  358. end;
  359. end;
  360. end;
  361. end;
  362. // RegServer(string SourceFile)
  363. function LuaRegServer(L: Plua_State): Integer; cdecl;
  364. Var
  365. SourceFile :String;
  366. NParams :Integer;
  367. begin
  368. Result := 0;
  369. NParams := lua_gettop(L);
  370. if (NParams=1)
  371. then begin
  372. try
  373. SourceFile :=LuaToString(L, 1);
  374. ShellExecute(GetDesktopWindow, 'open', PChar('REGSVR32'),
  375. PChar('/s '+'"'+SourceFile+'"'),
  376. Nil, SW_SHOWNORMAL);
  377. DoFunctionLog('RegServer', '"'+SourceFile+'"');
  378. LuaPushBoolean(L, True);
  379. Result := 1;
  380. except
  381. On E:Exception do begin
  382. //LuaError(L, ERR_Script+E.Message);
  383. end;
  384. end;
  385. end;
  386. end;
  387. // UnRegServer(string SourceFile)
  388. function LuaUnRegServer(L: Plua_State): Integer; cdecl;
  389. Var
  390. SourceFile :String;
  391. NParams :Integer;
  392. begin
  393. Result := 0;
  394. NParams := lua_gettop(L);
  395. if (NParams=1)
  396. then begin
  397. try
  398. SourceFile :=LuaToString(L, 1);
  399. ShellExecute(GetDesktopWindow, 'open', PChar('REGSVR32'),
  400. PChar('/u /s '+'"'+SourceFile+'"'),
  401. Nil, SW_SHOWNORMAL);
  402. DoFunctionLog('UnRegServer', '"'+SourceFile+'"');
  403. LuaPushBoolean(L, True);
  404. Result := 1;
  405. except
  406. On E:Exception do begin
  407. //LuaError(L, ERR_Script+E.Message);
  408. end;
  409. end;
  410. end;
  411. end;
  412. procedure RegisterFunctions(L: Plua_State);
  413. begin
  414. LuaRegister(L, 'CreateSearchRec', LuaCreateSearchRec);
  415. LuaRegister(L, 'CopyPath', LuaCopyPath);
  416. LuaRegister(L, 'CopyFile', LuaCopyFile);
  417. LuaRegister(L, 'DeleteDir', LuaDeleteDir);
  418. LuaRegister(L, 'RegServer', LuaRegServer);
  419. LuaRegister(L, 'UnRegServer', LuaUnRegServer);
  420. end;
  421. end.