diff --git a/Minecraft-Community-Edition/IO/Stream/FileInputStream.cpp b/Minecraft-Community-Edition/IO/Stream/FileInputStream.cpp index 1c7059b..44bc5b0 100644 --- a/Minecraft-Community-Edition/IO/Stream/FileInputStream.cpp +++ b/Minecraft-Community-Edition/IO/Stream/FileInputStream.cpp @@ -1,15 +1,44 @@ #include "FileInputStream.hpp" +#include + namespace mce { - size_t FileInputStream::read(void* buffer, size_t size) { + FileInputStream::FileInputStream(const eastl::string_view file) { + open(file); } - size_t FileInputStream::seek(size_t position) { + bool FileInputStream::isOpen() const { + return stream.is_open(); + } + + bool FileInputStream::eof() const { + return stream.eof(); + } + + void FileInputStream::close() { + stream.close(); + } + + void FileInputStream::open(const eastl::string_view file) { + stream.open(file.data()); + fileName = file.data(); + } + + size_t FileInputStream::read(void* buffer, const size_t size) { + stream.read(static_cast(buffer), static_cast(size)); + return stream.gcount(); + } + + size_t FileInputStream::seek(const size_t position) { + stream.seekg(static_cast(position)); + return position; } size_t FileInputStream::tell() { + return stream.tellg(); } size_t FileInputStream::getSize() { + return std::filesystem::file_size(fileName); } } diff --git a/Minecraft-Community-Edition/IO/Stream/FileInputStream.hpp b/Minecraft-Community-Edition/IO/Stream/FileInputStream.hpp index 4ae5beb..65178e1 100644 --- a/Minecraft-Community-Edition/IO/Stream/FileInputStream.hpp +++ b/Minecraft-Community-Edition/IO/Stream/FileInputStream.hpp @@ -1,13 +1,50 @@ #pragma once +#include + #include "InputStream.hpp" +#include + namespace mce { class FileInputStream final : public InputStream { public: + FileInputStream() = default; + + /** + * @brief Constructs the stream and opens a file + * @param file The path (absolute or relative) to the file + */ + explicit FileInputStream(eastl::string_view file); + + /** + * @brief Opens a file to read from + * @param file The path (absolute or relative) to the file + */ + void open(eastl::string_view file); + + /** + * @return True if the file was successfully opened + */ + bool isOpen() const; + + /** + * @brief Checks if the stream is at the end of the file + * @return True if the file stream has the end-of-file flag (EOF). + */ + bool eof() const; + + /** + * @brief Closes the file + */ + void close(); + size_t read(void* buffer, size_t size) override; size_t seek(size_t position) override; size_t tell() override; size_t getSize() override; + private: + std::ifstream stream; + std::string fileName; }; } \ No newline at end of file diff --git a/Minecraft-Community-Edition/IO/Stream/InputStream.cpp b/Minecraft-Community-Edition/IO/Stream/InputStream.cpp index 302acab..fb1c18a 100644 --- a/Minecraft-Community-Edition/IO/Stream/InputStream.cpp +++ b/Minecraft-Community-Edition/IO/Stream/InputStream.cpp @@ -1,4 +1,4 @@ -#include "../InputStream.hpp" +#include "InputStream.hpp" namespace mce { InputStream::~InputStream() {} diff --git a/Minecraft-Community-Edition/IO/Stream/InputStream.hpp b/Minecraft-Community-Edition/IO/Stream/InputStream.hpp index 13e2bff..a4a9ea5 100644 --- a/Minecraft-Community-Edition/IO/Stream/InputStream.hpp +++ b/Minecraft-Community-Edition/IO/Stream/InputStream.hpp @@ -7,9 +7,30 @@ namespace mce { public: virtual ~InputStream() = 0; + /** + * @brief Reads from the input stream + * @param buffer The buffer to read the bytes into + * @param size The amount of bytes to read + * @return The amount of bytes written + */ virtual size_t read(void* buffer, size_t size) = 0; + + /** + * @brief Changes the current position the stream is reading + * @param position The position to seek the input stream to + * @return The position the input stream is at now + */ virtual size_t seek(size_t position) = 0; + + /** + * + * @return The current position the stream is at + */ virtual size_t tell() = 0; + + /** + * @return The size of the stream in bytes + */ virtual size_t getSize() = 0; }; } \ No newline at end of file diff --git a/Minecraft-Community-Edition/IO/Stream/OutputStream.hpp b/Minecraft-Community-Edition/IO/Stream/OutputStream.hpp index c45f39a..c4460b8 100644 --- a/Minecraft-Community-Edition/IO/Stream/OutputStream.hpp +++ b/Minecraft-Community-Edition/IO/Stream/OutputStream.hpp @@ -7,7 +7,11 @@ namespace mce { public: virtual ~OutputStream() = 0; + /** + * @brief Writes all pending operations to the file + */ virtual void flush() = 0; + virtual size_t write(void* buffer, size_t size) = 0; virtual size_t seek(size_t position) = 0; virtual size_t tell() = 0; diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 71bce01..33b2931 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -9,5 +9,16 @@ function(DeclareTest name) gtest_discover_tests(${name}) endfunction() +function(CopyFilesFor target) + add_custom_command(TARGET ${target} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_CURRENT_LIST_DIR}/TestFiles + $/TestFiles + ) +endfunction() + DeclareTest(InputStream) DeclareTest(Logging) + +CopyFilesFor(InputStream) + diff --git a/Tests/InputStream.cpp b/Tests/InputStream.cpp index cee654b..30b8699 100644 --- a/Tests/InputStream.cpp +++ b/Tests/InputStream.cpp @@ -1,5 +1,34 @@ #include -TEST(InputStream, reading) { +#include "IO/Stream/FileInputStream.hpp" +using namespace mce; + +TEST(InputStream, Reading) { + FileInputStream testStream("TestFiles/test.txt"); + + EXPECT_TRUE(testStream.isOpen()); + EXPECT_FALSE(testStream.eof()); + + std::vector fileContent(testStream.getSize() + 1, 0); + size_t bytesRead = testStream.read(fileContent.data(), testStream.getSize()); + + std::ifstream stream("TestFiles/test.txt"); + + std::string content; + while (std::getline(stream, content)) {} + + EXPECT_EQ(content, fileContent.data()); + EXPECT_EQ(bytesRead, content.size()); + + stream.close(); +} + +TEST(InputStream, ReadMoreThanFileHas) { + FileInputStream testStream("TestFiles/test.txt"); + + std::vector fileContent(testStream.getSize() + 1, 0); + size_t bytesRead = testStream.read(fileContent.data(), testStream.getSize() + 2); + + EXPECT_EQ(bytesRead, testStream.getSize()); } \ No newline at end of file diff --git a/Tests/TestFiles/test.txt b/Tests/TestFiles/test.txt new file mode 100644 index 0000000..82fa9da --- /dev/null +++ b/Tests/TestFiles/test.txt @@ -0,0 +1 @@ +this is some data \ No newline at end of file