Advertisement
HEX0x29A

uApiSearch

Aug 18th, 2013
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.95 KB | None | 0 0
  1. unit uApiSearch;
  2. (*
  3. API file search
  4. Date  : 18.08.2013
  5. Author: HEX0x29A
  6. *)
  7. interface
  8.  
  9. uses
  10.   Windows, Messages;
  11.  
  12. type
  13.   TSearchCallbackFunc = function(const lpPath: PWideChar;
  14.                                  const FindData: _WIN32_FIND_DATAW): BOOL;
  15.   // Return False = (pbSearchAbort^ = True);
  16.  
  17.   function SearchFiles(const lpSearchPath       : PWideChar;
  18.                        const pbSearchAbort      : PBool;
  19.                        const pFileCallback      : TSearchCallbackFunc = nil;
  20.                        const pDirectoryCallback : TSearchCallbackFunc = nil;
  21.                        const bRecursive         : BOOL                = True;
  22.                        const lpFileMask         : PWideChar           = nil): BOOL;
  23.  
  24.   procedure ProcessMessages;
  25.  
  26. implementation
  27.  
  28. procedure ProcessMessages;
  29. var
  30.   Msg: TMsg;
  31.   function ProcessMsg(var Msg: TMsg): BOOL;
  32.   begin
  33.     Result := false;
  34.     if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
  35.     begin
  36.       Result := true;
  37.       if Msg.Message <> WM_QUIT then
  38.       begin
  39.         TranslateMessage(Msg);
  40.         DispatchMessage(Msg);
  41.       end
  42.       else
  43.         DispatchMessage(Msg);
  44.     end;
  45.   end;
  46. begin
  47.   while ProcessMsg(Msg) do;
  48. end;
  49.  
  50. function SearchFiles(const lpSearchPath       : PWideChar;
  51.                      const pbSearchAbort      : PBool;
  52.                      const pFileCallback      : TSearchCallbackFunc = nil;
  53.                      const pDirectoryCallback : TSearchCallbackFunc = nil;
  54.                      const bRecursive         : BOOL                = True;
  55.                      const lpFileMask         : PWideChar           = nil): BOOL;
  56. const
  57.   DEFAUL_FILE_MASK: WideString = '*.*';
  58. var
  59.   fbSearchAbort : BOOL;
  60.   fwcVariable   : WideChar;
  61.   fwcSlash      : WideChar;
  62.   fwsFileMask   : WideString;
  63.   fwsSearchPath : WideString;
  64.  
  65.   function ScanDirectory(var wsSearchPath: WideString): BOOL;
  66.   var
  67.     fuiPathLength: UINT;
  68.     hSearch      : UINT;
  69.     fd           : _WIN32_FIND_DATAW;
  70.   begin
  71.     fuiPathLength := Length(wsSearchPath);
  72.     hSearch := FindFirstFileW(PWideChar(WideString(wsSearchPath + fwsFileMask)), fd);
  73.     if hSearch <> INVALID_HANDLE_VALUE then
  74.       try
  75.         repeat
  76.           ProcessMessages; //PROCESS-MESSAGES
  77.           if (Pointer(@pFileCallback) <> nil) and
  78.              ((fd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0) and
  79.              (WideString(fd.cFileName) <> WideString('.')) and
  80.              (fd.cFileName <> '..') then
  81.             fbSearchAbort := (not pFileCallback(PWideChar(WideString(wsSearchPath + WideString(fd.cFileName))), fd));
  82.           if (pbSearchAbort <> nil) and (not fbSearchAbort) then
  83.             fbSearchAbort := pbSearchAbort^;
  84.         until (not FindNextFileW(hSearch, fd)) or (fbSearchAbort);
  85.       finally
  86.         FindClose(hSearch);
  87.       end;
  88.  
  89.     ProcessMessages; //PROCESS-MESSAGES
  90.     Result := (not fbSearchAbort);
  91.     if (not Result) or (not bRecursive) then
  92.       Exit;
  93.  
  94.     hSearch := FindFirstFileW(PWideChar(WideString(wsSearchPath + DEFAUL_FILE_MASK)), fd);
  95.     if hSearch <> INVALID_HANDLE_VALUE then
  96.       try
  97.         repeat
  98.           ProcessMessages; //PROCESS-MESSAGES
  99.           if ((fd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0) and
  100.              (WideString(fd.cFileName) <> WideString('.')) and
  101.              (fd.cFileName <> '..') then
  102.           begin
  103.             wsSearchPath := WideString(wsSearchPath + WideString(fd.cFileName) + fwcSlash);
  104.             if (Pointer(@pDirectoryCallback) <> nil) then
  105.               fbSearchAbort := (not pDirectoryCallback(PWideChar(wsSearchPath), fd));
  106.             if (pbSearchAbort <> nil) and (not fbSearchAbort) then
  107.               fbSearchAbort := pbSearchAbort^;
  108.             if (fbSearchAbort) then
  109.             begin
  110.               Result := false;
  111.               Break;
  112.             end;
  113.             Result := ScanDirectory(wsSearchPath);
  114.             SetLength(wsSearchPath, fuiPathLength);
  115.           end;
  116.         until (not FindNextFileW(hSearch, fd)) or (fbSearchAbort);
  117.       finally
  118.         FindClose(hSearch);
  119.       end;
  120.   end;
  121.  
  122. begin
  123.   fwcSlash := '\';
  124.   if (pbSearchAbort <> nil) then
  125.     fbSearchAbort := pbSearchAbort^
  126.   else
  127.     fbSearchAbort := false;
  128.   if lpFileMask = nil then
  129.     fwsFileMask := DEFAUL_FILE_MASK
  130.   else
  131.     fwsFileMask := WideString(lpFileMask);
  132.   if (lpSearchPath = nil) or (lpSearchPath = '') then
  133.   begin
  134.     for fwcVariable := 'A' to 'Z' do
  135.     begin
  136.       fwsSearchPath := WideString(fwcVariable + WideString(':') + fwcSlash);
  137.       Result := ScanDirectory(fwsSearchPath);
  138.       if not Result then
  139.         Break;
  140.     end;
  141.   end
  142.   else
  143.   begin
  144.     fwsSearchPath := WideString(lpSearchPath);
  145.     if (Pos('/', fwsSearchPath) <> 0) then
  146.       fwcSlash := '/';
  147.     if not (fwsSearchPath[Length(fwsSearchPath)] in [WideChar('\'), WideChar('/')]) then
  148.       fwsSearchPath := WideString(fwsSearchPath + fwcSlash);
  149.     Result := ScanDirectory(fwsSearchPath);
  150.   end;
  151. end;
  152.  
  153. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement