mirror of
https://github.com/CDevJoud/Minecraft-Community-Edition.git
synced 2026-07-18 00:30:46 +00:00
FileInputStream
This commit is contained in:
@@ -1,15 +1,44 @@
|
||||
#include "FileInputStream.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
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<char *>(buffer), static_cast<std::streamsize>(size));
|
||||
return stream.gcount();
|
||||
}
|
||||
|
||||
size_t FileInputStream::seek(const size_t position) {
|
||||
stream.seekg(static_cast<std::streamsize>(position));
|
||||
return position;
|
||||
}
|
||||
|
||||
size_t FileInputStream::tell() {
|
||||
return stream.tellg();
|
||||
}
|
||||
|
||||
size_t FileInputStream::getSize() {
|
||||
return std::filesystem::file_size(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "InputStream.hpp"
|
||||
|
||||
#include <EASTL/string_view.h>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../InputStream.hpp"
|
||||
#include "InputStream.hpp"
|
||||
|
||||
namespace mce {
|
||||
InputStream::~InputStream() {}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
$<TARGET_FILE_DIR:${target}>/TestFiles
|
||||
)
|
||||
endfunction()
|
||||
|
||||
DeclareTest(InputStream)
|
||||
DeclareTest(Logging)
|
||||
|
||||
CopyFilesFor(InputStream)
|
||||
|
||||
|
||||
@@ -1,5 +1,34 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
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<char> 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<char> fileContent(testStream.getSize() + 1, 0);
|
||||
size_t bytesRead = testStream.read(fileContent.data(), testStream.getSize() + 2);
|
||||
|
||||
EXPECT_EQ(bytesRead, testStream.getSize());
|
||||
}
|
||||
1
Tests/TestFiles/test.txt
Normal file
1
Tests/TestFiles/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
this is some data
|
||||
Reference in New Issue
Block a user