Advertisement
Guest User

Untitled

a guest
Jun 6th, 2011
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.55 KB | None | 0 0
  1. local ffi = require "ffi"
  2. ffi.cdef[[
  3. #pragma pack(push)
  4. #pragma pack(1)
  5.   struct WIN32_FIND_DATAA
  6.   {
  7.     uint32_t dwFileAttributes;
  8.     uint64_t ftCreationTime;
  9.     uint64_t ftLastAccessTime;
  10.     uint64_t ftLastWriteTime;
  11.     struct
  12.     {
  13.       union
  14.       {
  15.         uint64_t packed;
  16.         struct
  17.         {
  18.           uint32_t high;
  19.           uint32_t low;
  20.         };
  21.       };
  22.     } nFileSize;
  23.     uint32_t dwReserved[2];
  24.     char cFileName[260];
  25.     char cAlternateFileName[14];
  26.   };
  27. #pragma pack(pop)
  28.   void* FindFirstFileA(const char* pattern, struct WIN32_FIND_DATAA* fd);
  29.   bool FindNextFileA(void* ff, struct WIN32_FIND_DATAA* fd);
  30.   bool FindClose(void* ff);
  31. ]]
  32.  
  33. local WIN32_FIND_DATAA = ffi.typeof("struct WIN32_FIND_DATAA")
  34. local INVALID_HANDLE = ffi.cast("void*", -1)
  35. local forFiles = function(path, pattern)
  36.   if not path:sub(-1):find("[\\/]") then
  37.     path = path .. "/"
  38.   end
  39.   local fd = ffi.new(WIN32_FIND_DATAA)
  40.   local tFiles = {}
  41.   local hFile = ffi.C.FindFirstFileA(path .. pattern, fd)
  42.   if hFile ~= INVALID_HANDLE then
  43.     ffi.gc(hFile, ffi.C.FindClose)
  44.     repeat
  45.       fd.nFileSize.low, fd.nFileSize.high = fd.nFileSize.high, fd.nFileSize.low
  46.       tFiles[ffi.string(fd.cFileName)] = {
  47.         attrib = fd.dwFileAttributes,
  48.         time_create = fd.ftCreationTime,
  49.         time_access = fd.ftLastAccessTime,
  50.         time_write = fd.ftLastWriteTime,
  51.         size = fd.nFileSize.packed,
  52.       }
  53.     until not ffi.C.FindNextFileA(hFile, fd)
  54.     ffi.C.FindClose(ffi.gc(hFile, nil))
  55.   end
  56.   return tFiles
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement