Advertisement
77wisher77

wLibFs tes Yami

Jun 8th, 2021
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. local PROGRAM_VERSION = "1.0"
  2.  
  3. ---Checks if a file exists and returns a bool
  4. ---@param filepath string
  5. ---@return boolean
  6. function exists(filepath)
  7.   local isExist = fs.exists(filepath)
  8.   return isExist
  9. end
  10.  
  11. ---Opens a given file, in the given mode and returns a handle
  12. ---@param filepath string
  13. ---@param mode string
  14. ---@return table
  15. function open(filepath, mode)
  16.   local handle = fs.open(filepath, mode)
  17.   return handle
  18. end
  19.  
  20. ---Closes an open file
  21. ---@param handle table
  22. function close(handle)
  23.   handle.close()
  24. end
  25.  
  26. ---Creates a new file with the given names,
  27. ---The file will be empty
  28. ---@param filepath string
  29. ---@param close boolean --Optional, Default = false
  30. function newFileBlank(filepath, close)
  31.   local handle = fs.open(filepath, "w")
  32.   if close then
  33.     handle.close()
  34.   end
  35. end
  36.  
  37. ---Opens a new file for writing,
  38. --- input is the string to write
  39. ---  close is a boolean, true will close the file, otherwise the handle is returned
  40. ---@param handle table --optional
  41. ---@param filepath string
  42. ---@param input string
  43. ---@param mode string --1 = write, 2 = writeLine
  44. ---@param close boolean --Optional, Default = false
  45. ---@return table
  46. function write(handle, filepath, input, mode, close)
  47.   handle =  handle or fs.open(filepath, "w")
  48.   if tonumber(mode) == 1 then
  49.     handle.write(input)
  50.   elseif tonumber(mode) == 2 then
  51.     handle.writeLine(input)
  52.   else
  53.     print("Error writing to file, did you specify the correct mode?")
  54.   end
  55.   if close then
  56.     fs.close()
  57.   else
  58.     return handle
  59.   end
  60. end
  61.  
  62. ------Opens a new file for appending, Works the same as wLibFs.write,
  63. --- input is the string to write
  64. ---  close is a boolean, true will close the file, otherwise the handle is returned
  65. ---@param handle table --optional
  66. ---@param filepath string
  67. ---@param input string
  68. ---@param mode string --1 = write, 2 = writeLine
  69. ---@param close boolean --Optional, Default = false
  70. ---@return table
  71. function append(handle, filepath, input, mode, close)
  72.   handle =  handle or fs.open(filepath, "a")
  73.   if tonumber(mode) == 1 then
  74.     handle.write(input)
  75.   elseif tonumber(mode) == 2 then
  76.     handle.writeLine(input)
  77.   else
  78.     print("Error writing to file, did you specify the correct mode?")
  79.   end
  80.   if close then
  81.     fs.close()
  82.   else
  83.     return handle
  84.   end
  85. end
  86.  
  87. ---Reads a file and returns the char/Line and if not closed the handle
  88. ---@param handle table --optional
  89. ---@param filepath any
  90. ---@param mode any --1 = read, 2 = readLine
  91. ---@param position any --Optional, Default = 1, line or char # to read
  92. ---@param close any --Optional, Default = false
  93. ---@return string
  94. ---@return table
  95. function read(handle, filepath, mode, position, close)
  96.   handle = handle or fs.open(filepath, "r")
  97.   position = position or 1
  98.   local varRead
  99.   if tonumber(mode) == 1 then
  100.     for i = 1, position, 1 do
  101.       if i ~= position then
  102.         handle.read()
  103.       elseif i == position then
  104.         varRead = handle.read()
  105.       end
  106.     end
  107.     handle.read()
  108.   elseif tonumber(mode) == 2 then
  109.     for i = 1, position, 1 do
  110.       if i ~= position then
  111.         handle.readLine()
  112.       elseif i == position then
  113.         varRead = handle.readLine()
  114.       end
  115.     end
  116.   else
  117.     print("Error reading the file, did you set it to the right mode?")
  118.   end
  119.   if close then
  120.     handle.close()
  121.     return varRead
  122.   else
  123.     return varRead, handle
  124.   end
  125. end
  126.  
  127. return {
  128.   exists = exists,
  129.   open = open,
  130.   close = close,
  131.   newFileBlank = newFileBlank,
  132.   write = write,
  133.   append = append,
  134.   read = read
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement