Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local binaryFile = {
- buffer = {},
- file = fileName,
- position = 1,
- write = function(self, ...)
- for i,v in ipairs(arg) do
- table.insert(self.buffer, self.position, v)
- self:seek("cur", 1)
- end
- end,
- read = function(self, bytes)
- bytes = bytes or 1
- local data = {unpack(self.buffer, self.position, self.position+bytes)}
- self:seek("cur", bytes+1)
- return data
- end,
- seek = function(self, whence, offset)
- if whence == "set" then
- whence = 1
- elseif whence == "cur" then
- whence = self.position
- elseif whence == "end" then
- whence = #self.buffer+1
- else
- whence = self.position
- end
- if not offset then
- offset = 0
- end
- if whence + offset > #self.buffer+1 then
- return false, "whence + offset > length of file"
- elseif whence+offset < 0 then
- return false, "whence + offset < 0"
- else
- self.position = whence+offset
- return self.position
- end
- end,
- flush = function(self)
- local handle = fs.open(self.file, "wb")
- for i,v in ipairs(self.buffer) do
- handle.write(v)
- end
- handle.close()
- end,
- update = function(self)
- if fs.exists(self.file) then
- local handle = fs.open(self.file, "rb")
- self.buffer = {}
- while true do
- local byte = handle.read()
- if byte then
- table.insert(self.buffer, byte)
- else
- return
- end
- end
- handle.close()
- return true
- end
- return false
- end,
- readAll = function(self)
- self:seek("end")
- return self.buffer
- end,
- clear = function(self)
- self.buffer = {}
- end,
- bytes = function(self)
- local i = 1
- return function()
- if i > #self.buffer then
- return
- end
- data = self.buffer[i]
- i = i+1
- return data
- end
- end,
- new = function(self, file)
- local object = {["file"] = file}
- setmetatable(object, self)
- self.__index = self
- object:update()
- return object
- end
- }
- local textFile = {
- position = 1,
- bufmode = 1,
- file = fileName,
- internalBuffer = "",
- writeToBuffer = function(self, data)
- local beforePosition = string.sub(self.internalBuffer, 1, self.position-1)
- local afterPosition = string.sub(self.internalBuffer, self.position)
- self.internalBuffer = beforePosition..data..afterPosition
- end,
- update = function(self)
- if fs.exists(self.file) then
- local handle = fs.open(self.file, "r")
- self.internalBuffer = handle.readAll()
- handle.close()
- return true
- end
- return false
- end,
- seek = function(self, whence, offset)
- if whence == "set" then
- whence = 1
- elseif whence == "cur" then
- whence = self.position
- elseif whence == "end" then
- whence = #self.internalBuffer+1
- else
- whence = self.position
- end
- if not offset then
- offset = 0
- end
- if whence + offset > #self.internalBuffer+1 then
- return false, "whence + offset > length of file"
- elseif whence+offset < 0 then
- return false, "whence + offset < 0"
- else
- self.position = whence+offset
- return self.position
- end
- end,
- flush = function(self)
- local internalHandle = fs.open(self.file, "w")
- internalHandle.write(self.internalBuffer)
- internalHandle.close()
- end,
- write = function(self, ...)
- for i,v in ipairs(arg) do
- self:writeToBuffer(v)
- if self.seekAfterWrite then
- self:seek("cur", #v+1)
- end
- end
- end,
- clear = function(self)
- self.internalBuffer = ""
- end,
- writeLine = function(self, ...)
- for i,v in ipairs(arg) do
- self:writeToBuffer(v.."\n")
- self:seek("cur", #v+2)
- end
- end,
- readLine = function(self)
- local nextNewLine = string.find(self.internalBuffer, "\n", self.position)
- if nextNewLine then
- local line = string.sub(self.internalBuffer, self.position, nextNewLine)
- self.position = nextNewLine+1
- return line
- end
- end,
- readAll = function(self)
- self:seek("end")
- return string.sub(self.internalBuffer, self.position)
- end,
- read = function(self, chars)
- chars = chars or 1
- local data = string.sub(self.internalBuffer, self.position, self.position+chars)
- self:seek("cur", chars+1)
- return data
- end,
- lines = function(self)
- local oldPos = self:seek()
- return function()
- local line = self:readLine()
- if line == nil then
- self:seek("set", oldPos)
- end
- return line
- end
- end,
- new = function(self, file)
- local object = {["file"] = file}
- setmetatable(object, self)
- self.__index = self
- object:update()
- return object
- end
- }
- function openBinary(fileName)
- return binaryFile:new(fileName)
- end
- function open(fileName)
- return textFile:new(fileName)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement