added File:type() and File:typeOf()

This commit is contained in:
Grump
2020-06-12 13:12:46 +02:00
parent e71f08c1e3
commit c84a058772
3 changed files with 16 additions and 10 deletions

View File

@@ -31,7 +31,7 @@ Link in this list point to their `love.filesystem` counterparts. All functions a
Functions that are not available in `love.filesystem`: Functions that are not available in `love.filesystem`:
* `nativefs.getDirectoryItemsInfo(path)` * `nativefs.getDirectoryItemsInfo(path)`
Returns a list of items in a directory that contains not only the names, but also the information returned by `getInfo` for each item. The return value is a list of files, in which each entry is a table as returned by [getInfo](https://love2d.org/wiki/love.filesystem.getInfo), with an additional `name` field for each file. Returns a list of items in a directory that contains not only the names, but also the information returned by `getInfo` for each item. The return value is a list of files and directories, in which each entry is a table as returned by [getInfo](https://love2d.org/wiki/love.filesystem.getInfo), with an additional `name` field for each entry.
* `nativefs.getDriveList()` * `nativefs.getDriveList()`
Returns a table of all populated drive letters on Windows (`{ 'C:/', 'D:/', ...}`). On systems that don't use drive letters, a table with the single entry `/` is returned. Returns a table of all populated drive letters on Windows (`{ 'C:/', 'D:/', ...}`). On systems that don't use drive letters, a table with the single entry `/` is returned.
@@ -58,6 +58,8 @@ Changes the working directory.
* [File:seek](https://love2d.org/wiki/\(File\):seek) * [File:seek](https://love2d.org/wiki/\(File\):seek)
* [File:tell](https://love2d.org/wiki/\(File\):tell) * [File:tell](https://love2d.org/wiki/\(File\):tell)
* [File:flush](https://love2d.org/wiki/\(File\):flush) * [File:flush](https://love2d.org/wiki/\(File\):flush)
* [File:type](https://love2d.org/wiki/Object:type)
* [File:typeOf](https://love2d.org/wiki/Object:typeOf)
* [File:release](https://love2d.org/wiki/Object:release) * [File:release](https://love2d.org/wiki/Object:release)
Function names in this list are links to their LÖVE `File` counterparts. `File` is designed to work the same way as LÖVE's `File` class. Function names in this list are links to their LÖVE `File` counterparts. `File` is designed to work the same way as LÖVE's `File` class.

View File

@@ -41,17 +41,13 @@ function File:setBuffer(mode, size)
return false, "Invalid buffer mode " .. mode .. " (expected 'none', 'full', or 'line')" return false, "Invalid buffer mode " .. mode .. " (expected 'none', 'full', or 'line')"
end end
if mode == 'line' or mode == 'full' then if mode == 'none' then
size = math.max(2, size or 2) -- Windows requires buffer to be at least 2 bytes
else
size = math.max(0, size or 0) size = math.max(0, size or 0)
end else
if self._mode == 'c' then size = math.max(2, size or 2) -- Windows requires buffer to be at least 2 bytes
self._bufferMode, self._bufferSize = mode, size
return true
end end
local success = C.setvbuf(self._handle, nil, bufferMode, size) == 0 local success = self._mode == 'c' or C.setvbuf(self._handle, nil, bufferMode, size) == 0
if success then if success then
self._bufferMode, self._bufferSize = mode, size self._bufferMode, self._bufferSize = mode, size
return true return true
@@ -75,7 +71,7 @@ function File:getSize()
else else
self:seek(pos) self:seek(pos)
end end
return size; return size
end end
function File:read(containerOrBytes, bytes) function File:read(containerOrBytes, bytes)
@@ -181,6 +177,10 @@ function File:release()
self._handle = nil self._handle = nil
end end
function File:type() return 'File' end
function File:typeOf(t) return t == 'File' end
File.__index = File File.__index = File
----------------------------------------------------------------------------- -----------------------------------------------------------------------------

View File

@@ -29,6 +29,10 @@ local testFile2, testSize2 = 'data/𠆢ßЩ.txt', 450
function test_fs_newFile() function test_fs_newFile()
local file = fs.newFile('test.file') local file = fs.newFile('test.file')
notEquals(file, nil) notEquals(file, nil)
equals(file:type(), 'File')
equals(file:typeOf('File'), true)
equals(file:getMode(), 'c')
equals(file:isEOF(), true)
equals(file:getFilename(), 'test.file') equals(file:getFilename(), 'test.file')
end end