-- public domain 20080404 lua@ztact.com pcall (require, 'fs') pcall (require, 'lfs') pcall (require, 'posix') pcall (require, 'socket') local assert, getfenv, ipairs, pairs, type = assert, getfenv, ipairs, pairs, type local io, math, os, string, table = io, math, os, string, table local fs, lfs, posix, socket = fs, lfs, posix, socket local append = table.insert local print = print module ((...) or 'pzp') ----------------------------------------- module pzp function print_r (mixed) ------------------------------------------- print_r if type (mixed) == 'string' then print (mixed) end if type (mixed) == 'table' then for k,v in pairs (mixed) do print (k, v) end end end function import_to (G) -- - - - - - - - - - - - - - - - - - - - - import_to for k,v in pairs (getfenv ()) do if k ~= import_to and type (v) == 'function' then G[k] =v end end end function array_keys (t) ----------------------------------------- array_keys local keys = {} for k,v in pairs (t) do append (keys, k) end return keys end function asort (t) -- - - - - - - - - - - - - - - - - - - - - - - - - asort local keys = {} for k,v in pairs (t) do append (keys, k) end table.sort (keys, function (a, b) return t[a] < t[b] end) return keys end function arsort (t) -- - - - - - - - - - - - - - - - - - - - - - - - arsort local keys = {} for k,v in pairs (t) do append (keys, k) end table.sort (keys, function (a, b) return t[a] > t[b] end) return keys end function chmod (path, mode) -- - - - - - - - - - - - - - - - - - - - - chmod fs.chmod (path, mode) end function copy (src, dst) -- - - - - - - - - - - - - - - - - - - - - - - copy local sf, df = assert (io.open (src, 'r')), assert (io.open (dst, 'w')) repeat local block = sf:read (0x10000) df:write (block or '') until not block sf:close () df:close () end function dirname (path) -- - - - - - - - - - - - - - - - - - - - - - dirname -- print (string.match (path, '^(.*)/[^/]*$')) return string.match (path, '^(.*)/[^/]*$') end function explode (needle, haystack, limit) ------------------------- explode local i, t = 1, {} while true do local j, k = string.find (haystack, needle, i, true) if j then table.insert (t, string.sub (haystack, i, j-1)) i = k+1 else table.insert (t, string.sub (haystack, i)) break end end return t end function file_exists (path) -- - - - - - - - - - - - - - - - - - file_exists end function filemtime (path) -- - - - - - - - - - - - - - - - - - - - filemtime return lfs.attributes (path, 'modification') end function in_array (needle, haystack) -- - - - - - - - - - - - - - - in_array for k,v in pairs (haystack) do if v == needle then return true end end return false end function is_file (path) -- - - - - - - - - - - - - - - - - - - - - - is_file return fs.is_file (path) end function is_dir (path) -- - - - - - - - - - - - - - - - - - - - - - - is_dir return fs.is_dir (path) end function md5_file (path) -- - - - - - - - - - - - - - - - - - - - - md5_file local md5sum = io.popen ('md5sum -b '..path, 'r') local md5 = md5sum:read (0x400) md5sum:close () md5 = string.match (md5, '^%S+') return md5 end function mkdir (path, mode, recursive) -- - - - - - - - - - - - - - - mkdir if recursive then local parent = dirname (path) if not is_dir (parent) then local dirs = {} repeat table.insert (dirs, 1, parent) parent = dirname (parent) until (not parent or is_dir (parent)) for i,dir in ipairs (dirs) do posix.mkdir (dir) chmod (dir, mode) end end end if not is_dir (path) then posix.mkdir (path) chmod (path, mode) end end function sleep (seconds) -- - - - - - - - - - - - - - - - - - - - - - sleep socket.sleep (math.max (0, seconds)) end function unlink (path) -- - - - - - - - - - - - - - - - - - - - - - - unlink os.remove (path) end