mirror of
https://github.com/EngineerSmith/nativefs.git
synced 2025-11-08 23:15:02 +00:00
refactor
This commit is contained in:
30
nativefs.lua
30
nativefs.lua
@@ -5,20 +5,20 @@ local File = {
|
|||||||
getBuffer = function(self) return self._bufferMode, self._bufferSize end,
|
getBuffer = function(self) return self._bufferMode, self._bufferSize end,
|
||||||
getFilename = function(self) return self._name end,
|
getFilename = function(self) return self._name end,
|
||||||
getMode = function(self) return self._mode end,
|
getMode = function(self) return self._mode end,
|
||||||
isEOF = function(self) return not self:isOpen() or C.feof(self._handle) ~= 0 or self:tell() == self:getSize() end,
|
|
||||||
isOpen = function(self) return self._mode ~= 'c' and self._handle ~= nil end,
|
isOpen = function(self) return self._mode ~= 'c' and self._handle ~= nil end,
|
||||||
}
|
}
|
||||||
|
|
||||||
local fopen, getcwd, chdir, unlink, mkdir, rmdir
|
local fopen, getcwd, chdir, unlink, mkdir, rmdir
|
||||||
local BUFFERMODE, MODEMAP
|
local BUFFERMODE, MODEMAP
|
||||||
local ByteArray = ffi.typeof('unsigned char[?]')
|
local ByteArray = ffi.typeof('unsigned char[?]')
|
||||||
|
local function _ptr(p) return p ~= nil and p or nil end
|
||||||
|
|
||||||
function File:open(mode)
|
function File:open(mode)
|
||||||
if self._mode ~= 'c' then return false, "File " .. self._name .. " is already open" end
|
if self._mode ~= 'c' then return false, "File " .. self._name .. " is already open" end
|
||||||
if not MODEMAP[mode] then return false, "Invalid open mode for " .. self._name .. ": " .. mode end
|
if not MODEMAP[mode] then return false, "Invalid open mode for " .. self._name .. ": " .. mode end
|
||||||
|
|
||||||
local handle = fopen(self._name, MODEMAP[mode])
|
local handle = _ptr(fopen(self._name, MODEMAP[mode]))
|
||||||
if handle == nil then return false, "Could not open " .. self._name .. " in mode " .. mode end
|
if not handle then return false, "Could not open " .. self._name .. " in mode " .. mode end
|
||||||
|
|
||||||
if C.setvbuf(handle, nil, BUFFERMODE[self._bufferMode], self._bufferSize) ~= 0 then
|
if C.setvbuf(handle, nil, BUFFERMODE[self._bufferMode], self._bufferSize) ~= 0 then
|
||||||
self._bufferMode, self._bufferSize = 'none', 0
|
self._bufferMode, self._bufferSize = 'none', 0
|
||||||
@@ -161,19 +161,23 @@ function File:seek(pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function File:tell()
|
function File:tell()
|
||||||
if self._handle == nil then return nil, "Invalid position" end
|
if not self._handle then return nil, "Invalid position" end
|
||||||
return self._handle and tonumber(C.ftell(self._handle)) or -1
|
return tonumber(C.ftell(self._handle))
|
||||||
end
|
end
|
||||||
|
|
||||||
function File:flush()
|
function File:flush()
|
||||||
if self._handle == nil then return false, "File is not open" end
|
if self._mode ~= 'w' and self._mode ~= 'a' then
|
||||||
|
return nil, "File is not opened for writing"
|
||||||
|
end
|
||||||
return C.fflush(self._handle) == 0
|
return C.fflush(self._handle) == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function File:isEOF()
|
||||||
|
return not self:isOpen() or C.feof(self._handle) ~= 0 or self:tell() == self:getSize()
|
||||||
|
end
|
||||||
|
|
||||||
function File:release()
|
function File:release()
|
||||||
if self._mode ~= 'c' then
|
if self._mode ~= 'c' then self:close() end
|
||||||
self:close()
|
|
||||||
end
|
|
||||||
self._handle = nil
|
self._handle = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -308,8 +312,8 @@ function nativefs.remove(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function withTempMount(dir, fn)
|
local function withTempMount(dir, fn)
|
||||||
local mountPoint = loveC.PHYSFS_getMountPoint(dir)
|
local mountPoint = _ptr(loveC.PHYSFS_getMountPoint(dir))
|
||||||
if mountPoint ~= nil then return fn(ffi.string(mountPoint)) end
|
if mountPoint then return fn(ffi.string(mountPoint)) end
|
||||||
if not nativefs.mount(dir, '__nativefs__temp__') then return false, "Could not mount " .. dir end
|
if not nativefs.mount(dir, '__nativefs__temp__') then return false, "Could not mount " .. dir end
|
||||||
local a, b = fn('__nativefs__temp__')
|
local a, b = fn('__nativefs__temp__')
|
||||||
nativefs.unmount(dir)
|
nativefs.unmount(dir)
|
||||||
@@ -432,8 +436,8 @@ else
|
|||||||
rmdir = function(path) return ffi.C.rmdir(path) == 0 end
|
rmdir = function(path) return ffi.C.rmdir(path) == 0 end
|
||||||
|
|
||||||
getcwd = function()
|
getcwd = function()
|
||||||
local cwd = C.getcwd(nameBuffer, MAX_PATH)
|
local cwd = _ptr(C.getcwd(nameBuffer, MAX_PATH))
|
||||||
return cwd ~= nil and ffi.string(cwd) or nil
|
return cwd and ffi.string(cwd) or nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ local function notFailed(ok, err)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function hasFailed(expected, ok, err)
|
local function hasFailed(expected, ok, err)
|
||||||
equals(ok, false)
|
equals(ok or false, false)
|
||||||
if expected then
|
if expected then
|
||||||
equals(err, expected)
|
equals(err, expected)
|
||||||
end
|
end
|
||||||
@@ -334,8 +334,9 @@ end
|
|||||||
|
|
||||||
function test_File_tell()
|
function test_File_tell()
|
||||||
local f = fs.newFile(testFile1)
|
local f = fs.newFile(testFile1)
|
||||||
local ok, err = f:tell()
|
|
||||||
equals(ok, nil)
|
hasFailed("Invalid position", f:tell())
|
||||||
|
|
||||||
f:open('r')
|
f:open('r')
|
||||||
f:read(172)
|
f:read(172)
|
||||||
equals(f:tell(), 172)
|
equals(f:tell(), 172)
|
||||||
@@ -344,9 +345,13 @@ end
|
|||||||
|
|
||||||
function test_File_flush()
|
function test_File_flush()
|
||||||
local f = fs.newFile('data/write.test')
|
local f = fs.newFile('data/write.test')
|
||||||
|
|
||||||
|
hasFailed("File is not opened for writing", f:flush())
|
||||||
|
|
||||||
f:open('w')
|
f:open('w')
|
||||||
f:write('hello')
|
f:write('hello')
|
||||||
notFailed(f:flush())
|
notFailed(f:flush())
|
||||||
|
|
||||||
f:close()
|
f:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user