Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PROGRAM_VERSION = "1.0"
- ---Checks if a file exists and returns a bool
- ---@param filepath string
- ---@return boolean
- function exists(filepath)
- local isExist = fs.exists(filepath)
- return isExist
- end
- ---Opens a given file, in the given mode and returns a handle
- ---@param filepath string
- ---@param mode string
- ---@return table
- function open(filepath, mode)
- local handle = fs.open(filepath, mode)
- return handle
- end
- ---Closes an open file
- ---@param handle table
- function close(handle)
- handle.close()
- end
- ---Creates a new file with the given names,
- ---The file will be empty
- ---@param filepath string
- ---@param close boolean --Optional, Default = false
- function newFileBlank(filepath, close)
- local handle = fs.open(filepath, "w")
- if close then
- handle.close()
- end
- end
- ---Opens a new file for writing,
- --- input is the string to write
- --- close is a boolean, true will close the file, otherwise the handle is returned
- ---@param handle table --optional
- ---@param filepath string
- ---@param input string
- ---@param mode string --1 = write, 2 = writeLine
- ---@param close boolean --Optional, Default = false
- ---@return table
- function write(handle, filepath, input, mode, close)
- handle = handle or fs.open(filepath, "w")
- if tonumber(mode) == 1 then
- handle.write(input)
- elseif tonumber(mode) == 2 then
- handle.writeLine(input)
- else
- print("Error writing to file, did you specify the correct mode?")
- end
- if close then
- fs.close()
- else
- return handle
- end
- end
- ------Opens a new file for appending, Works the same as wLibFs.write,
- --- input is the string to write
- --- close is a boolean, true will close the file, otherwise the handle is returned
- ---@param handle table --optional
- ---@param filepath string
- ---@param input string
- ---@param mode string --1 = write, 2 = writeLine
- ---@param close boolean --Optional, Default = false
- ---@return table
- function append(handle, filepath, input, mode, close)
- handle = handle or fs.open(filepath, "a")
- if tonumber(mode) == 1 then
- handle.write(input)
- elseif tonumber(mode) == 2 then
- handle.writeLine(input)
- else
- print("Error writing to file, did you specify the correct mode?")
- end
- if close then
- fs.close()
- else
- return handle
- end
- end
- ---Reads a file and returns the char/Line and if not closed the handle
- ---@param handle table --optional
- ---@param filepath any
- ---@param mode any --1 = read, 2 = readLine
- ---@param position any --Optional, Default = 1, line or char # to read
- ---@param close any --Optional, Default = false
- ---@return string
- ---@return table
- function read(handle, filepath, mode, position, close)
- handle = handle or fs.open(filepath, "r")
- position = position or 1
- local varRead
- if tonumber(mode) == 1 then
- for i = 1, position, 1 do
- if i ~= position then
- handle.read()
- elseif i == position then
- varRead = handle.read()
- end
- end
- handle.read()
- elseif tonumber(mode) == 2 then
- for i = 1, position, 1 do
- if i ~= position then
- handle.readLine()
- elseif i == position then
- varRead = handle.readLine()
- end
- end
- else
- print("Error reading the file, did you set it to the right mode?")
- end
- if close then
- handle.close()
- return varRead
- else
- return varRead, handle
- end
- end
- return {
- exists = exists,
- open = open,
- close = close,
- newFileBlank = newFileBlank,
- write = write,
- append = append,
- read = read
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement