Advertisement
ndfjay

fLib

Jul 19th, 2013
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. function exists(path)
  2.     local file = assert(io.open(path, "r"))
  3.     if file ~= nil then
  4.         file:close()
  5.         return true
  6.     end
  7.    
  8.     return false
  9. end
  10.  
  11. function getTable(path)
  12.     if exists(path) then
  13.         local file = io.open(path, "r")
  14.         local lines = {}
  15.         local i = 1
  16.         local line = file:read("*l")
  17.         while line ~= nil do
  18.             lines[i] = line
  19.             line = file:read("*l")
  20.             i = i + 1
  21.         end
  22.         file:close()
  23.         return lines
  24.     end
  25.     return {}
  26. end
  27.  
  28. function getLine(path, n)
  29.     if exists(path) then
  30.         local lines = getTable(path)
  31.         return lines[n]
  32.     end
  33.     return ""
  34. end
  35.  
  36. function getText(path)
  37.     if exists(path) then
  38.         local file = assert(io.open(path, "r"))
  39.         return file:read("*a")
  40.     end
  41.     return ""
  42. end
  43.  
  44. function fappend(path, text)
  45.     local file = assert(io.open(path, "a"))
  46.     file:write(text.."\n")
  47.     file:close()
  48. end
  49.  
  50. function fwrite(path, text)
  51.     local file = assert(io.open(path, "w"))
  52.     file:write(text)
  53.     file:close()
  54. end
  55.  
  56. function fwriteAtStart(path, text)
  57.     local _text = getText(path)
  58.     fwrite(path, text.."\n".._text)
  59. end
  60.  
  61. function fwriteFromTable(path, t)
  62.     local text = ""
  63.     for _, line in pairs(t) do
  64.         text = text..line.."\n"
  65.     end
  66.     fwrite(path, text)
  67. end
  68.  
  69. function fappendFromTable(path, t)
  70.     local text = ""
  71.     for _, line in pairs(t) do
  72.         text = text..line.."\n"
  73.     end
  74.     fappend(path, text)
  75. end
  76.  
  77. function fwriteAtStartFromTable(path, t)
  78.     local text = ""
  79.     for _, line in pairs(t) do
  80.         text = text..line.."\n"
  81.     end
  82.     fwriteAtStart(path, text)
  83. end
  84.  
  85. function replaceLine(path, n, text)
  86.     local lines = getTable(path)
  87.     lines[n] = text
  88.     fwriteFromTable(path, lines)
  89. end
  90.  
  91. function getName(path)
  92.     if exists(path) then
  93.         local lastSlashPos = 1
  94.         for i = 1, path:len() do
  95.             if path:sub(i, i) == "/" then
  96.                 lastSlashPos = i
  97.             end
  98.         end
  99.        
  100.         return path:sub(lastSlashPos + 1)
  101.     end
  102.     return ""
  103. end
  104.  
  105. function getPath(path)
  106.     if exists(path) then
  107.         local lastSlashPos = 1
  108.         for i = 1, path:len() do
  109.             if path:sub(i, i) == "/" then
  110.                 lastSlashPos = i
  111.             end
  112.         end
  113.        
  114.         return path:sub(1, lastSlashPos)
  115.     end
  116.     return ""
  117. end
  118.  
  119. function fremove(path)
  120.     os.remove(path)
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement