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

Add love.filesystem.newFileData() (#283)

parent cfd13009
......@@ -4,9 +4,10 @@ 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.24.0 - Unreleased
## 0.24.0 - 2018-09-03
### Features
- Update to use libretro-common audio
- Added `love.filesystem.newFileData()`
- Added `love.system.getClipboardText()`
- Added `love.system.setClipboardText()`
- Added `love.data.encode()`
......
#include "FileData.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "filesystem/path.h"
#include "../../../ChaiLove.h"
namespace love {
namespace Types {
namespace FileSystem {
FileData::FileData(const std::string& filepath) : m_filepath(filepath) {
// Nothing.
}
int FileData::getSize() {
ChaiLove* app = ChaiLove::getInstance();
return app->filesystem.getSize(m_filepath);
}
std::string FileData::getFilename() {
return m_filepath;
}
std::string FileData::getString() {
ChaiLove* app = ChaiLove::getInstance();
return app->filesystem.read(m_filepath);
}
std::string FileData::getExtension() {
::filesystem::path p(m_filepath.c_str());
std::string extension(p.extension());
return extension;
}
} // namespace FileSystem
} // namespace Types
} // namespace love
#ifndef SRC_LOVE_TYPES_FILESYSTEM_FILEDATA_H_
#define SRC_LOVE_TYPES_FILESYSTEM_FILEDATA_H_
#include <string>
namespace love {
namespace Types {
namespace FileSystem {
/**
* Information about a file.
*
* @see love.filesystem.newFileData
*/
class FileData {
public:
/**
* Creates a new FileData from a file on the storage device.
*
* @param filepath Path to the file.
*/
FileData(const std::string& filepath);
/**
* The size of the Data in bytes.
*/
int getSize();
/**
* Gets the filename of the FileData.
*/
std::string getFilename();
/**
* Gets the full Data as a string.
*/
std::string getString();
/**
* Gets the extension of the FileData.
*/
std::string getExtension();
std::string m_filepath;
};
} // namespace FileSystem
} // namespace Types
} // namespace love
#endif // SRC_LOVE_TYPES_FILESYSTEM_FILEDATA_H_
......@@ -10,6 +10,7 @@
#include "Types/FileSystem/FileInfo.h"
using love::Types::FileSystem::FileInfo;
using love::Types::FileSystem::FileData;
namespace love {
......@@ -320,6 +321,11 @@ FileInfo filesystem::getInfo(const std::string& path) {
return fileInfo;
}
FileData filesystem::newFileData(const std::string& filepath) {
FileData f(filepath);
return f;
}
bool filesystem::createDirectory(const std::string& name) {
int ret = PHYSFS_mkdir(name.c_str());
if (ret == 0) {
......
......@@ -7,8 +7,10 @@
#include "SDL.h"
#include "physfs.h"
#include "Types/FileSystem/FileInfo.h"
#include "Types/FileSystem/FileData.h"
using love::Types::FileSystem::FileInfo;
using love::Types::FileSystem::FileData;
namespace love {
/**
......@@ -88,7 +90,20 @@ class filesystem {
FileInfo getInfo(const std::string& path);
/**
* Unmounts a zip file or folder previously mounted with filesystem::unmount.
* Creates a new FileData from a file on the storage device.
*
* @param path Path to the file.
*
* @return The new FileData, or nil if an error occurred.
*
* @see love::Types::FileSystem::FileData
*/
FileData newFileData(const std::string& filepath);
/**
* Unmounts a zip file or folder previously mounted with love.filesystem.mount.
*
* @param archive The archive that was previously mounted with love.filesystem.mount.
*
* @return bool True, when unmounting was a success.
*/
......
......@@ -17,6 +17,7 @@ using love::Types::Input::Joystick;
using love::Types::Config::WindowConfig;
using love::Types::Config::ModuleConfig;
using love::Types::Audio::SoundData;
using love::Types::FileSystem::FileData;
using love::graphics;
namespace love {
......@@ -130,6 +131,14 @@ script::script(const std::string& file) {
chai.add(constructor<FileInfo(const std::string&, int)>(), "FileInfo");
chai.add(constructor<FileInfo(const std::string&, int, int)>(), "FileInfo");
// FileData Object.
chai.add(user_type<FileData>(), "FileData");
chai.add(fun(&FileData::getSize), "getSize");
chai.add(fun(&FileData::getFilename), "getFilename");
chai.add(fun(&FileData::getString), "getString");
chai.add(fun(&FileData::getExtension), "getExtension");
chai.add(constructor<FileData(const std::string&)>(), "FileData");
// Color Object.
chai.add(user_type<Color>(), "Color");
chai.add(fun(&Color::r), "r");
......@@ -268,6 +277,7 @@ script::script(const std::string& file) {
chai.add(fun(&filesystem::write), "write");
chai.add(fun(&filesystem::exists), "exists");
chai.add(fun(&filesystem::getInfo), "getInfo");
chai.add(fun(&filesystem::newFileData), "newFileData");
chai.add(fun(&filesystem::getDirectoryItems), "getDirectoryItems");
chai.add(fun(&filesystem::mount), "mount");
chai.add(fun<int, filesystem, const std::string&>(&filesystem::getSize), "getSize");
......
......@@ -42,8 +42,19 @@ assert_equal(theLines[0], "// getDirectoryItems()", "love.filesystem.lines()")
// getInfo()
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")
assert_not(fileInfo.is_var_null(), "love.filesystem.getInfo()")
assert_equal(fileInfo.type, "file", " .type")
assert_greater(fileInfo.size, 5, " .size")
// newFileData()
var loadedFileData = love.filesystem.newFileData("filesystem.chai")
assert_not(loadedFileData.is_var_null(), "love.filesystem.newFileData()")
assert_greater(loadedFileData.getSize(), 10, " .getSize()")
assert_equal(loadedFileData.getExtension(), "chai", " .getExtension()")
assert_equal(loadedFileData.getFilename(), "filesystem.chai", " .getFilename()")
var fileDataContents = loadedFileData.getString()
var fileDataFind = fileDataContents.find("// newFileData()")
assert_greater(fileDataFind, 50, " .getString()")
// createDirectory()
var createDirectoryReturn = love.filesystem.createDirectory("test/createDirectoryTest")
......
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