Advertisement
Le_JuiceBOX

[Module] fileHelper.lua

Apr 6th, 2024 (edited)
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.03 KB | None | 0 0
  1. -- cJdTak4C
  2.  
  3. local module = {}
  4.  
  5. module.setLines = function(filePath,lines)
  6.     if filePath == nil then printError("Filepath is nil"); end
  7.     if fs.isDir(filePath) then return nil end
  8.     local file = fs.open( filePath, "w" )
  9.     local data = ""
  10.     for i = 1, #lines do
  11.         data = data..lines[i]
  12.         if i ~= #lines then
  13.             data = data.."\n"
  14.         end
  15.     end
  16.     file.write(data)
  17.     file.close()
  18. end
  19.  
  20. module.getLines = function(filePath)
  21.     if filePath == nil then printError("Filepath is nil"); end
  22.     if fs.exists(filePath) == false then return nil end
  23.     if fs.isDir(filePath) then return nil end
  24.     local fileHandle = fs.open(filePath, 'r')
  25.  
  26.     -- Validate handle.
  27.     if not fileHandle then
  28.         return
  29.     end
  30.  
  31.     local fileContents = fileHandle.readAll()
  32.     fileHandle.close()
  33.  
  34.  
  35.     local lines = {}
  36.  
  37.     local i = 1
  38.     for line in fileContents:gmatch("[^\n]+") do
  39.         lines[i] = string.gsub(line,"\n","")
  40.         i = i + 1
  41.     end
  42.     return lines
  43. end
  44.  
  45. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement