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

Merge pull request #253 from libretro/love.filesystem.write

feat: Add love.filesystem.write()
parents 334e08af dc2e29fb
......@@ -4,6 +4,11 @@ All notable changes to [ChaiLove](https://github.com/RobLoach/ChaiLove) will be
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## 0.22.0 - Development
### Features
- `love.filesystem.createDirectory()` to make directories
- `love.filesystem.write()` to write a file in the save directory
## 0.21.0 - 2018-05-02
### Chores
- Switched to [libretro-deps](https://github.com/libretro/libretro-deps) for freetype and zlib
......
......@@ -287,6 +287,26 @@ FileInfo filesystem::getInfo(const std::string& path) {
return fileInfo;
}
bool filesystem::createDirectory(const std::string& name) {
int ret = PHYSFS_mkdir(name.c_str());
if (ret == 0) {
std::cout << "[ChaiLove] [filesystem] Failed to create directory: " << getLastError() << std::endl;
return false;
}
return true;
}
bool filesystem::write(const std::string& name, const std::string& data) {
PHYSFS_File* file = PHYSFS_openWrite(name.c_str());
if (file == NULL) {
std::cout << "[ChaiLove] [filesystem] Error opening file for writing: " << getLastError() << std::endl;
return false;
}
const char* buffer = data.c_str();
int bytesWritten = PHYSFS_writeBytes(file, (const void*)buffer, data.size());
return bytesWritten >= 0;
}
std::string filesystem::getLastError() {
const char* charErr = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
if (charErr) {
......
......@@ -109,6 +109,27 @@ class filesystem {
*/
bool isSymlink(const std::string& filename);
/**
* Recursively creates a directory in the save directory.
*
* When called with "a/b" it creates both "a" and "a/b", if they don't exist already.
*
* @param name The directory to create.
*
* @return True if the directory was created, false if not.
*/
bool createDirectory(const std::string& name);
/**
* Write data to a file in the save directory. If the file existed already, it will be completely replaced by the new contents.
*
* @param name The name (and path) of the file.
* @param data The string data to write to the file.
*
* @return How many bytes to write.
*/
bool write(const std::string& name, const std::string& data);
/**
* Iterate over the lines in a file, with the given delimiter.
*
......
......@@ -264,8 +264,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::createDirectory), "createDirectory");
chai.add(fun(&filesystem::isSymlink), "isSymlink");
chai.add(fun(&filesystem::isFile), "isFile");
chai.add(fun(&filesystem::write), "write");
chai.add(fun(&filesystem::exists), "exists");
chai.add(fun(&filesystem::getInfo), "getInfo");
chai.add(fun(&filesystem::getDirectoryItems), "getDirectoryItems");
......
/test
/createDirectoryTest
......@@ -43,3 +43,11 @@ assert_equal(theLines[0], "// getDirectoryItems()", "love.filesystem.lines()")
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")
// createDirectory()
var createDirectoryReturn = love.filesystem.createDirectory("test/createDirectoryTest")
assert(createDirectoryReturn, "love.filesystem.createDirectory()")
// write()
var writeFileReturn = love.filesystem.write("test/createDirectoryTest/test.md", "# Test\n\nHello World!");
assert(writeFileReturn, "love.filesystem.write()")
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