Advertisement
Guest User

Untitled

a guest
Jun 6th, 2011
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1. local forFiles = function(path,pattern)
  2.     if not path:sub(-1,-1):match("[\\/]") then
  3.         path = path.."/"
  4.     end
  5.     local finddata_t = alien.defstruct{
  6.         {"attrib","uint"},
  7.         {"time_create","long"},
  8.         {"time_access","long"},
  9.         {"time_write","long"},
  10.         {"size","ulong"},
  11.         {"name","char"},
  12.     }
  13.     finddata_t.size = finddata_t.size+260
  14.     local _finddata_t = finddata_t:new()
  15.  
  16.     local function getfilename(fd)
  17.         local out = {}
  18.         local offset = finddata_t.offsets.name
  19.         local buf = fd()
  20.         for i = offset+1, offset+260 do
  21.             local c = buf:get(i, "char")
  22.             if c > 0 then out[#out+1] = string.char(c)
  23.             else break end
  24.         end
  25.         return table.concat(out)
  26.     end
  27.  
  28.     local _findfirst = alien.MSVCRT._findfirst
  29.     local _findnext = alien.MSVCRT._findnext
  30.     local _findclose = alien.MSVCRT._findclose
  31.  
  32.     _findfirst:types{ret="long",abi="cdecl", "string","pointer"}
  33.     _findnext:types{ ret="int", abi="cdecl", "long",  "pointer"}
  34.     _findclose:types{ret="int", abi="cdecl", "long"}
  35.  
  36.     hFile = _findfirst(path..pattern,_finddata_t());
  37.     local tFiles = {}
  38.     if hFile ~= -1 then
  39.         tFiles[getfilename(_finddata_t)] = {
  40.             ["attrib"] = _finddata_t.attrib,
  41.             ["time_create"] = _finddata_t.time_create,
  42.             ["time_access"] = _finddata_t.time_access,
  43.             ["time_write"] = _finddata_t.time_write,
  44.             ["size"] = _finddata_t.size,
  45.         }
  46.         local iRes = _findnext(hFile,_finddata_t())
  47.         while iRes == 0 do
  48.             tFiles[getfilename(_finddata_t)] = {
  49.                 ["attrib"] = _finddata_t.attrib,
  50.                 ["time_create"] = _finddata_t.time_create,
  51.                 ["time_access"] = _finddata_t.time_access,
  52.                 ["time_write"] = _finddata_t.time_write,
  53.                 ["size"] = _finddata_t.size,
  54.             }
  55.             iRes = _findnext(hFile,_finddata_t())
  56.         end
  57.         _findclose(hFile)
  58.     end
  59.     return tFiles
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement