Verified Commit 98d021dc authored by RobLoach's avatar RobLoach
Browse files

filesystem: Add love.filesystem.getInfo()

parent 487d279e
......@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [Semantic Versioning](https://semver.org/) checks
- Use `t.version = "0.18.0"` in `conf(t)` to specify which ChaiLove version you're targeting
- `love.graphics.getDimensions()`
- `love.filesystem.getInfo()`
### Updated
- `love.math.compress()` is now `love.data.compress()`
......
......@@ -3,6 +3,7 @@ SOURCES_CXX += $(wildcard \
src/*.cpp \
src/love/*.cpp \
src/love/Types/Audio/*.cpp \
src/love/Types/FileSystem/*.cpp \
src/love/Types/Graphics/*.cpp \
src/love/Types/Input/*.cpp \
src/love/Types/Config/*.cpp \
......
#include "FileInfo.h"
namespace love {
namespace Types {
namespace FileSystem {
// Nothing.
} // namespace FileSystem
} // namespace Types
} // namespace love
#ifndef SRC_LOVE_TYPES_FILESYSTEM_FILEINFO_H_
#define SRC_LOVE_TYPES_FILESYSTEM_FILEINFO_H_
#include <string>
namespace love {
namespace Types {
namespace FileSystem {
/**
* @brief Information about a file.
*/
struct FileInfo {
/**
* @brief Creates a basic FileInfo object, with no values set.
*/
FileInfo() : type(""), size(-1), modtime(-1) {
// Nothing.
}
FileInfo(const std::string& theType) : type(theType), size(-1), modtime(-1) {
// Nothing.
}
FileInfo(const std::string& theType, int theSize) : type(theType), size(theSize), modtime(-1) {
// Nothing.
}
FileInfo(const std::string& theType, int theSize, int theModtime) : type(theType), size(theSize), modtime(theModtime) {
// Nothing.
}
/**
* @brief The type of the object at the path (file, directory, symlink, empty, etc.).
*/
std::string type;
/**
* @brief The size in bytes of the file, or -1 if it can't be determined.
*/
int size;
/**
* @brief The file's last modification time in seconds since the unix epoch, or nil if it can't be determined.
*/
int modtime;
};
} // namespace FileSystem
} // namespace Types
} // namespace love
#endif // SRC_LOVE_TYPES_FILESYSTEM_FILEINFO_H_
#include <string>
#include <iostream>
#include "physfs.h"
#include "filesystem.h"
#include <physfsrwops.h>
#include <filesystem/path.h>
#include "physfsrwops.h"
#include "filesystem/path.h"
#include "../ChaiLove.h"
#include <iostream>
#include "Types/FileSystem/FileInfo.h"
using love::Types::FileSystem::FileInfo;
namespace love {
......@@ -214,6 +218,14 @@ bool filesystem::isFile(const std::string& filename) {
return stat.filetype == PHYSFS_FILETYPE_REGULAR;
}
bool filesystem::isSymlink(const std::string& filename) {
PHYSFS_Stat stat;
if (PHYSFS_stat(filename.c_str(), &stat) == 0) {
return false;
}
return stat.filetype == PHYSFS_FILETYPE_SYMLINK;
}
std::vector<std::string> filesystem::lines(const std::string& filename) {
return lines(filename, "\n");
}
......@@ -236,4 +248,31 @@ std::vector<std::string> filesystem::lines(const std::string& filename, const st
return strings;
}
FileInfo filesystem::getInfo(const std::string& path) {
FileInfo fileInfo;
PHYSFS_Stat stat;
if (PHYSFS_stat(path.c_str(), &stat) == 0) {
return fileInfo;
}
switch(stat.filetype) {
case PHYSFS_FILETYPE_REGULAR:
fileInfo.type = "file";
break;
case PHYSFS_FILETYPE_DIRECTORY:
fileInfo.type = "directory";
break;
case PHYSFS_FILETYPE_SYMLINK:
fileInfo.type = "symlink";
break;
default:
fileInfo.type = "other";
break;
}
fileInfo.modtime = stat.modtime;
fileInfo.size = stat.filesize;
return fileInfo;
}
} // namespace love
......@@ -3,8 +3,12 @@
#include <string>
#include <vector>
#include "SDL.h"
#include "physfs.h"
#include "Types/FileSystem/FileInfo.h"
using love::Types::FileSystem::FileInfo;
namespace love {
/**
......@@ -45,6 +49,12 @@ class filesystem {
*/
int getSize(const std::string& file);
/**
* @brief Gets information about the specified file or directory.
*
* @return A FileInfo object representing information about the path.
*/
FileInfo getInfo(const std::string& path);
/**
* @brief Unmounts a zip file or folder previously mounted with filesystem::unmount.
......@@ -74,6 +84,11 @@ class filesystem {
*/
bool isFile(const std::string& filename);
/**
* @brief Checks whether something is a symlink.
*/
bool isSymlink(const std::string& filename);
/**
* @brief Iterate over the lines in a file.
*/
......
......@@ -121,6 +121,16 @@ script::script(const std::string& file) {
chai.add(constructor<Point(float)>(), "Point");
chai.add(constructor<Point(float, float)>(), "Point");
// FileInfo Object.
chai.add(user_type<FileInfo>(), "FileInfo");
chai.add(fun(&FileInfo::type), "type");
chai.add(fun(&FileInfo::size), "size");
chai.add(fun(&FileInfo::modtime), "modtime");
chai.add(constructor<FileInfo()>(), "FileInfo");
chai.add(constructor<FileInfo(const std::string&)>(), "FileInfo");
chai.add(constructor<FileInfo(const std::string&, int)>(), "FileInfo");
chai.add(constructor<FileInfo(const std::string&, int, int)>(), "FileInfo");
// Color Object.
chai.add(user_type<Color>(), "Color");
chai.add(fun(&Color::r), "r");
......@@ -253,8 +263,10 @@ script::script(const std::string& file) {
chai.add(fun(&filesystem::unmount), "unmount");
chai.add(fun(&filesystem::read), "read");
chai.add(fun(&filesystem::isDirectory), "isDirectory");
chai.add(fun(&filesystem::isSymlink), "isSymlink");
chai.add(fun(&filesystem::isFile), "isFile");
chai.add(fun(&filesystem::exists), "exists");
chai.add(fun(&filesystem::getInfo), "getInfo");
chai.add(fun(&filesystem::getDirectoryItems), "getDirectoryItems");
chai.add(fun(&filesystem::mount), "mount");
chai.add(fun<int, filesystem, const std::string&>(&filesystem::getSize), "getSize");
......
......@@ -20,3 +20,15 @@ def assert_not_equal(actual, expected, message) {
def assert(value, message) {
assert_equal(value, true, message)
}
def assert_greater(actual, expected, message) {
if (actual > expected) {
print(" ✓ " + message)
}
else {
print(" ✗ " + message)
print("\n\t" + to_string(actual) + " !> " + to_string(expected) + "\n")
failure = "Failed on " + message + " when asserting that " + to_string(actual) + " is greater than " + to_string(expected)
}
}
......@@ -30,3 +30,9 @@ assert_equal(love.filesystem.getDirectoryItems("assets").size(), 2, "love.filesy
var theLines = love.filesystem.lines("filesystem.chai")
assert_equal(theLines[0], "// Filesystem", "love.filesystem.lines()")
assert_not(love.filesystem.isSymlink("keyboard.chai"), "love.filesystem.isSymlink()")
var fileInfo = love.filesystem.getInfo("keyboard.chai")
assert_equal(fileInfo.type, "file", "love.filesystem.getInfo().type")
assert_greater(fileInfo.size, 5, "love.filesystem.getInfo().size")
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