mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-27 01:45:35 -05:00
Merge branch 'main' of https://github.com/geode-sdk/geode
This commit is contained in:
commit
5954b06bc4
1 changed files with 37 additions and 34 deletions
|
@ -21,12 +21,17 @@ using namespace geode::prelude;
|
||||||
using namespace geode::utils::file;
|
using namespace geode::utils::file;
|
||||||
|
|
||||||
Result<std::string> utils::file::readString(ghc::filesystem::path const& path) {
|
Result<std::string> utils::file::readString(ghc::filesystem::path const& path) {
|
||||||
|
if (!ghc::filesystem::exists(path))
|
||||||
|
return Err("File does not exist");
|
||||||
|
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
std::ifstream in(path.wstring(), std::ios::in | std::ios::binary);
|
std::ifstream in(path.wstring(), std::ios::in | std::ios::binary);
|
||||||
#else
|
#else
|
||||||
std::ifstream in(path.string(), std::ios::in | std::ios::binary);
|
std::ifstream in(path.string(), std::ios::in | std::ios::binary);
|
||||||
#endif
|
#endif
|
||||||
if (in) {
|
if (!in)
|
||||||
|
return Err("Unable to open file");
|
||||||
|
|
||||||
std::string contents;
|
std::string contents;
|
||||||
in.seekg(0, std::ios::end);
|
in.seekg(0, std::ios::end);
|
||||||
contents.resize((const size_t)in.tellg());
|
contents.resize((const size_t)in.tellg());
|
||||||
|
@ -34,35 +39,33 @@ Result<std::string> utils::file::readString(ghc::filesystem::path const& path) {
|
||||||
in.read(&contents[0], contents.size());
|
in.read(&contents[0], contents.size());
|
||||||
in.close();
|
in.close();
|
||||||
return Ok(contents);
|
return Ok(contents);
|
||||||
}
|
|
||||||
return Err("Unable to open file");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<json::Value> utils::file::readJson(ghc::filesystem::path const& path) {
|
Result<json::Value> utils::file::readJson(ghc::filesystem::path const& path) {
|
||||||
|
|
||||||
auto str = utils::file::readString(path);
|
auto str = utils::file::readString(path);
|
||||||
|
if (!str)
|
||||||
if (str) {
|
return Err(str.unwrapErr());
|
||||||
try {
|
try {
|
||||||
return Ok(json::parse(str.value()));
|
return Ok(json::parse(str.value()));
|
||||||
} catch(std::exception const& e) {
|
|
||||||
return Err("Unable to parse JSON: " + std::string(e.what()));
|
|
||||||
}
|
}
|
||||||
} else {
|
catch(std::exception const& e) {
|
||||||
return Err("Unable to open file");
|
return Err("Unable to parse JSON: " + std::string(e.what()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<ByteVector> utils::file::readBinary(ghc::filesystem::path const& path) {
|
Result<ByteVector> utils::file::readBinary(ghc::filesystem::path const& path) {
|
||||||
|
if (!ghc::filesystem::exists(path))
|
||||||
|
return Err("File does not exist");
|
||||||
|
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
std::ifstream in(path.wstring(), std::ios::in | std::ios::binary);
|
std::ifstream in(path.wstring(), std::ios::in | std::ios::binary);
|
||||||
#else
|
#else
|
||||||
std::ifstream in(path.string(), std::ios::in | std::ios::binary);
|
std::ifstream in(path.string(), std::ios::in | std::ios::binary);
|
||||||
#endif
|
#endif
|
||||||
if (in) {
|
if (!in)
|
||||||
return Ok(ByteVector(std::istreambuf_iterator<char>(in), {}));
|
|
||||||
}
|
|
||||||
return Err("Unable to open file");
|
return Err("Unable to open file");
|
||||||
|
|
||||||
|
return Ok(ByteVector(std::istreambuf_iterator<char>(in), {}));
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> utils::file::writeString(ghc::filesystem::path const& path, std::string const& data) {
|
Result<> utils::file::writeString(ghc::filesystem::path const& path, std::string const& data) {
|
||||||
|
@ -72,14 +75,14 @@ Result<> utils::file::writeString(ghc::filesystem::path const& path, std::string
|
||||||
#else
|
#else
|
||||||
file.open(path.string());
|
file.open(path.string());
|
||||||
#endif
|
#endif
|
||||||
if (file.is_open()) {
|
if (!file.is_open()) {
|
||||||
file << data;
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
file.close();
|
file.close();
|
||||||
return Err("Unable to open file");
|
return Err("Unable to open file");
|
||||||
|
}
|
||||||
|
|
||||||
|
file << data;
|
||||||
|
file.close();
|
||||||
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> utils::file::writeBinary(ghc::filesystem::path const& path, ByteVector const& data) {
|
Result<> utils::file::writeBinary(ghc::filesystem::path const& path, ByteVector const& data) {
|
||||||
|
@ -89,14 +92,14 @@ Result<> utils::file::writeBinary(ghc::filesystem::path const& path, ByteVector
|
||||||
#else
|
#else
|
||||||
file.open(path.string(), std::ios::out | std::ios::binary);
|
file.open(path.string(), std::ios::out | std::ios::binary);
|
||||||
#endif
|
#endif
|
||||||
if (file.is_open()) {
|
if (!file.is_open()) {
|
||||||
file.write(reinterpret_cast<char const*>(data.data()), data.size());
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
file.close();
|
file.close();
|
||||||
return Err("Unable to open file");
|
return Err("Unable to open file");
|
||||||
|
}
|
||||||
|
|
||||||
|
file.write(reinterpret_cast<char const*>(data.data()), data.size());
|
||||||
|
file.close();
|
||||||
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> utils::file::createDirectory(ghc::filesystem::path const& path) {
|
Result<> utils::file::createDirectory(ghc::filesystem::path const& path) {
|
||||||
|
|
Loading…
Reference in a new issue