Advertisement
SkyCrafter0

sUtils.lua

Nov 13th, 2020
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.40 KB | None | 0 0
  1. local sUtils = {}
  2.  
  3. function sUtils.contains(t, value)
  4.   for i = 1, #t do
  5.     if t[i] == value then
  6.       return true
  7.     end
  8.   end
  9.   return false
  10. end
  11.  
  12. function sUtils.getFile(sFilename, sNetAddress, nFails)
  13.   nFails = nFails or 0
  14.   if nFails > 5 then
  15.     error("Failed too many times. Stopping.", -1)
  16.   end
  17.  
  18.   local h, err = http.get(sNetAddress)
  19.   if h then
  20.     local fh, err2 = io.open(sFilename, 'w')
  21.     if fh then
  22.       fh:write(h:readAll()):close()
  23.       h.close()
  24.     else
  25.       h.close()
  26.       printError("Failed to write file: " .. err2)
  27.       getFile(sFilename, sNetAddress, nFails + 1)
  28.     end
  29.   else
  30.     printError("Failed to connect: " .. err)
  31.     getFile(sFilename, sNetAddress, nFails + 1)
  32.   end
  33. end
  34.  
  35. function sUtils.split (inputstr, sep)
  36.   if sep == nil then
  37.           sep = "%s"
  38.   end
  39.   local t={}
  40.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  41.           table.insert(t, str)
  42.   end
  43.   return t
  44. end
  45.  
  46. function sUtils.countLines(path)
  47.   local lines = 0
  48.   for _ in io.lines(path) do lines = lines + 1 end
  49.   return lines
  50. end
  51.  
  52. function sUtils.getSize(path)
  53.   local size = 0
  54.   local files = fs.list(path)
  55.   for i=1,#files do
  56.     if fs.isDir(fs.combine(path, files[i])) then
  57.       size = size + file.getSize(fs.combine(path, files[i]))
  58.     else
  59.       size = size + fs.getSize(fs.combine(path, files[i]))
  60.     end
  61.   end
  62.   return size
  63. end
  64.  
  65. return sUtils
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement