Advertisement
Snusmumriken

Lil fs lib

Sep 23rd, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.29 KB | None | 0 0
  1. local M = {}
  2.  
  3.  
  4. function M.readfile(path, mode)
  5.     mode = mode or 'rb'
  6.     local file, err = io.open(path, mode)
  7.     if not file then return nil, err end
  8.     local data = file:read'*all'
  9.     file:close()
  10.     return data
  11. end
  12.  
  13. function M.writefile(path, data, mode)
  14.     mode = mode or 'wb'
  15.     local file, err = io.open(path, mode)
  16.     if not file then return nil, err end
  17.     file:write(data)
  18.     return file:close()
  19. end
  20.  
  21. function M.appendfile(path, data, mode)
  22.     mode = mode or 'a'
  23.     local file, err = io.open(path, mode)
  24.     if not file then return nil, err end
  25.     file:write(data)
  26.     return file:close()
  27. end
  28.  
  29.  
  30. function M.isFile(path)
  31.   local file = io.open(path, "r")
  32.     return file and file:close() or false
  33. end
  34.  
  35. function M.isDirectory(path)
  36.   local file, err = io.open(path, "r")
  37.     return err == 'Permission denied' or file and not file:close()
  38. end
  39.  
  40. M.path = {}
  41. function M.path.normalslashes(path)
  42.     return string.gsub(path, "\\", "/")
  43. end
  44.  
  45. function M.path.endslash(path)
  46.     if string.sub(path, #path-1) ~= "/" then
  47.         return p .. "/"
  48.     else
  49.         return p
  50.     end
  51. end
  52.  
  53. function M.path.leaf(path)
  54.     path = M.path.normalslashes(path)
  55.  
  56.     local a = 1
  57.     local last = path
  58.  
  59.     while path do
  60.         path = string.find(path, "/", a+1)
  61.  
  62.         if path then
  63.             last = string.sub(path, a+1)
  64.         end
  65.     end
  66.     return last
  67. end
  68.  
  69. return M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement