Skip to content

Commit

Permalink
Added SaveFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamashi committed Feb 11, 2022
1 parent 8d4dec4 commit 900ac3e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions Code/core/include/Filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ namespace TiltedPhoques
{
std::filesystem::path GetPath() noexcept;
String LoadFile(const std::filesystem::path& acPath) noexcept;
bool SaveFile(const std::filesystem::path& acPath, const String& acData) noexcept;
}
10 changes: 10 additions & 0 deletions Code/core/src/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ namespace TiltedPhoques
return content;
}

bool SaveFile(const std::filesystem::path& acPath, const String& acData) noexcept
{
std::ofstream out(acPath, std::ios::binary);
if (!out.is_open())
return false;

out.write(acData.data(), acData.size());

return true;
}
}
18 changes: 18 additions & 0 deletions Code/tests/src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <cstring>
#include <iostream>

#include "Filesystem.hpp"

using namespace TiltedPhoques;

TEST_CASE("Outcome saves the result and errors", "[core.outcome]")
Expand Down Expand Up @@ -817,3 +819,19 @@ TEST_CASE("Vector benchmark")
std::cout << "Tps: " << tpScratchElapsed.count() << std::endl;
}
}

TEST_CASE("Filesystem")
{
const String cData = "Hello There!\nGeneral Kenobi!";
const auto cPath = std::filesystem::current_path() / "test_file.txt";

WHEN("Saving a file")
{
REQUIRE(SaveFile(cPath, cData));

THEN("Loading it")
{
REQUIRE(LoadFile(cPath).size() == cData.size());
}
}
}

0 comments on commit 900ac3e

Please sign in to comment.