Unverified Commit b3159109 authored by RobLoach's avatar RobLoach Committed by GitHub
Browse files

Add lutro.filesystem.getDirectoryItems (#176)

* Add lutro.filesystem.getDirectoryItems

* Add more tests
parent dd01fd70
......@@ -3,6 +3,8 @@
#include <compat/strl.h>
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <vfs/vfs_implementation.h>
#include <string/stdstring.h>
#if WANT_PHYSFS
#include "physfs.h"
......@@ -25,6 +27,7 @@ int lutro_filesystem_preload(lua_State *L)
{ "isDirectory", fs_isDirectory },
{ "isFile", fs_isFile },
{ "createDirectory", fs_createDirectory },
{ "getDirectoryItems", fs_getDirectoryItems },
{NULL, NULL}
};
......@@ -270,3 +273,55 @@ int fs_createDirectory(lua_State *L)
lua_pushboolean(L, res);
return 1;
}
int fs_getDirectoryItems(lua_State *L)
{
// Validate number of arguments.
int n = lua_gettop(L);
if (n != 1) {
return luaL_error(L, "lutro.filesystem.getDirectoryItems requires 1 argument, %d given.", n);
}
// Get the full resolved path to the desired directory.
const char *path = luaL_checkstring(L, 1);
char fullpath[PATH_MAX_LENGTH];
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
// Make sure it's a directory.
if (!path_is_directory(fullpath)) {
return luaL_error(L, "The given directory of '%s' is not a directory.", path);
}
// Open up the directory.
libretro_vfs_implementation_dir* dir = retro_vfs_opendir_impl(fullpath, true);
if (!dir) {
retro_vfs_closedir_impl(dir);
return luaL_error(L, "Failed to open the '%s' directory.", path);
}
// Prepare the output table.
lua_newtable(L);
int index = 0;
// Iterate through each directory entry, ignoring the current and previous directories.
while (retro_vfs_readdir_impl(dir)) {
const char * currentDir = retro_vfs_dirent_get_name_impl(dir);
if (currentDir == NULL) {
break;
}
if (string_is_equal(currentDir, ".") || string_is_equal(currentDir, "..")) {
continue;
}
// Add the entry to the table.
lua_pushnumber(L, index++);
lua_pushstring(L, currentDir);
lua_settable(L, -3);
}
// Finally, close the opened directory, and return the table.
retro_vfs_closedir_impl(dir);
return 1;
}
......@@ -22,5 +22,6 @@ int fs_isFile(lua_State *L);
int fs_isDirectory(lua_State *L);
int fs_createDirectory(lua_State *L);
int fs_getUserDirectory(lua_State *L);
int fs_getDirectoryItems(lua_State *L);
#endif // FILESYSTEM_H
function lutro.filesystem.setRequirePathTest()
local paths = lutro.filesystem.getRequirePath()
paths = paths .. ';/lutro.filesystem.setRequirePathTest/?.lua'
lutro.filesystem.setRequirePath(paths)
local paths = lutro.filesystem.getRequirePath()
paths = paths .. ';/lutro.filesystem.setRequirePathTest/?.lua'
lutro.filesystem.setRequirePath(paths)
local paths = lutro.filesystem.getRequirePath()
local index = string.find(paths, 'setRequirePathTest')
unit.assertTrue(index > 1)
local paths = lutro.filesystem.getRequirePath()
local index = string.find(paths, 'setRequirePathTest')
unit.assertTrue(index > 1)
end
function lutro.filesystem.getRequirePathTest()
local paths = lutro.filesystem.getRequirePath()
unit.assertIsString(paths)
unit.assertEquals(paths, package.path)
local paths = lutro.filesystem.getRequirePath()
unit.assertIsString(paths)
unit.assertEquals(paths, package.path)
end
function lutro.filesystem.getUserDirectoryTest()
local homeDir = lutro.filesystem.getUserDirectory()
-- @todo Find out how to make os.getenv('HOME') work on Windows?
local luaHomeDir = os.getenv("HOME")
unit.assertEquals(homeDir, luaHomeDir)
local homeDir = lutro.filesystem.getUserDirectory()
-- @todo Find out how to make os.getenv('HOME') work on Windows?
local luaHomeDir = os.getenv("HOME")
unit.assertEquals(homeDir, luaHomeDir)
end
function lutro.filesystem.getDirectoryItemsTest()
-- Get the list of all current directory files.
local files = lutro.filesystem.getDirectoryItems(".")
-- Expect to find a main.lua file.
local foundMainLua = false
for k, file in ipairs(files) do
if file == "main.lua" then
foundMainLua = true
end
end
unit.assertTrue(foundMainLua)
end
function lutro.filesystem.isFileTest()
unit.assertEquals(lutro.filesystem.isFile("main.lua"), true)
unit.assertEquals(lutro.filesystem.isFile("missingFile.txt"), false)
end
function lutro.filesystem.isDirectoryTest()
unit.assertEquals(lutro.filesystem.isDirectory("."), true)
unit.assertEquals(lutro.filesystem.isDirectory("NotADirectory"), false)
end
return {
lutro.filesystem.setRequirePathTest,
lutro.filesystem.getRequirePathTest,
lutro.filesystem.getUserDirectoryTest
lutro.filesystem.getUserDirectoryTest,
lutro.filesystem.getDirectoryItemsTest,
lutro.filesystem.isFileTest,
lutro.filesystem.isDirectoryTest
}
\ No newline at end of file
function lutro.window.minimizeTest()
lutro.window.minimize()
lutro.window.minimize()
end
function lutro.window.maximizeTest()
lutro.window.maximize()
lutro.window.maximize()
end
function lutro.window.getTitleTest()
local title = lutro.window.getTitle()
unit.assertIsString(title)
unit.assertEquals(title, "Lutro")
local title = lutro.window.getTitle()
unit.assertIsString(title)
unit.assertEquals(title, "Lutro")
end
function lutro.window.setTitleTest()
lutro.window.setTitle("Hello World!")
lutro.window.setTitle("Hello World!")
end
function lutro.window.setPositionTest()
lutro.window.setPosition(1, 1)
lutro.window.setPosition(100, 200, 2)
lutro.window.setPosition(1, 1)
lutro.window.setPosition(100, 200, 2)
end
function lutro.window.getPositionTest()
local x, y, display = lutro.window.getPosition()
unit.assertIsNumber(x)
unit.assertIsNumber(y)
unit.assertIsNumber(display)
unit.assertEquals(x, 0)
unit.assertEquals(y, 0)
unit.assertEquals(display, 1)
local x, y, display = lutro.window.getPosition()
unit.assertIsNumber(x)
unit.assertIsNumber(y)
unit.assertIsNumber(display)
unit.assertEquals(x, 0)
unit.assertEquals(y, 0)
unit.assertEquals(display, 1)
end
function lutro.window.requestAttentionTest()
lutro.window.requestAttention()
lutro.window.requestAttention("Test")
lutro.window.requestAttention()
lutro.window.requestAttention("Test")
end
function lutro.window.getDisplayNameTest()
local display = lutro.window.getDisplayName()
unit.assertIsString(display)
unit.assertEquals(display, "libretro")
local display = lutro.window.getDisplayName()
unit.assertIsString(display)
unit.assertEquals(display, "libretro")
display = lutro.window.getDisplayName(3)
unit.assertIsString(display)
unit.assertEquals(display, "libretro")
display = lutro.window.getDisplayName(3)
unit.assertIsString(display)
unit.assertEquals(display, "libretro")
end
function lutro.window.setDisplaySleepEnabledTest()
lutro.window.setDisplaySleepEnabled(true)
lutro.window.setDisplaySleepEnabled(true)
end
function lutro.window.isDisplaySleepEnabledTest()
local sleep = lutro.window.isDisplaySleepEnabled()
unit.assertIsBoolean(sleep)
end
return {
......@@ -59,5 +64,6 @@ return {
lutro.window.getPositionTest,
lutro.window.requestAttentionTest,
lutro.window.getDisplayNameTest,
lutro.window.setDisplaySleepEnabledTest
lutro.window.setDisplaySleepEnabledTest,
lutro.window.isDisplaySleepEnabledTest
}
\ No newline at end of file
......@@ -23,6 +23,7 @@ int lutro_window_preload(lua_State *L)
{ "requestAttention", win_requestAttention },
{ "getDisplayName", win_getDisplayName },
{ "setDisplaySleepEnabled", win_setDisplaySleepEnabled },
{ "isDisplaySleepEnabled", win_isDisplaySleepEnabled },
{NULL, NULL}
};
......@@ -246,3 +247,20 @@ int win_setDisplaySleepEnabled(lua_State *L)
// Ignore setting the display sleep.
return 0;
}
/**
* lutro.window.isDisplaySleepEnabled
*
* https://love2d.org/wiki/love.window.isDisplaySleepEnabled
*/
int win_isDisplaySleepEnabled(lua_State *L)
{
int n = lua_gettop(L);
if (n != 0)
return luaL_error(L, "lutro.window.isDisplaySleepEnabled expects 1 arguments, %d given.", n);
// Lutro does not support sleeping displays.
lua_pushboolean(L, 0);
return 1;
}
......@@ -23,5 +23,6 @@ int win_getPosition(lua_State *L);
int win_requestAttention(lua_State *L);
int win_getDisplayName(lua_State *L);
int win_setDisplaySleepEnabled(lua_State *L);
int win_isDisplaySleepEnabled(lua_State *L);
#endif // WINDOW_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment