Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mmyster/feature/file system kit/tell #534

Merged
merged 2 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/interface/platform/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ struct File {

virtual auto size() -> size_t = 0;

virtual auto tell() -> size_t = 0;

[[nodiscard]] virtual auto is_open() const -> bool = 0;
};

Expand Down
2 changes: 2 additions & 0 deletions libs/FileSystemKit/include/FileSystemKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class FileSystemKit
void rewind() final;
auto size() -> size_t final;

auto tell() -> size_t final;

[[nodiscard]] auto is_open() const -> bool final;

private:
Expand Down
8 changes: 8 additions & 0 deletions libs/FileSystemKit/source/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,11 @@ auto FileSystemKit::File::is_open() const -> bool
{
return _file != nullptr;
}

auto FileSystemKit::File::tell() -> size_t
{
if (_file == nullptr) {
return 0;
}
return std::ftell(_file.get());
}
26 changes: 26 additions & 0 deletions libs/FileSystemKit/tests/File_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,29 @@ TEST_F(FileTest, seek)
ASSERT_EQ(3, bytes_read);
ASSERT_EQ(expected_data, output_data);
}

TEST_F(FileTest, tellNoFile)
{
auto pos = file.tell();
ASSERT_EQ(0, pos);
}

TEST_F(FileTest, tellEmptyFile)
{
file.open(tempFilename);
auto pos = file.tell();
ASSERT_EQ(0, pos);
}

TEST_F(FileTest, tellFile)
{
auto input_data = std::to_array<uint8_t>({0x61, 0x62, 0x63, 0x64, 0x65, 0x66}); // "abcdef"

file.open(tempFilename, "w");
file.write(input_data);
file.seek(3);

auto actual_pos = file.tell();

ASSERT_EQ(3, actual_pos);
}
2 changes: 1 addition & 1 deletion spikes/lk_filesystem_kit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ auto main() -> int
log_info("Data : %s", buffer.data());

file.seek(seek_temp);
log_info("Position indicator set to %d", seek_temp);
log_info("Position indicator set to %d", file.tell());
log_info("Reading...");
if (auto bytes = file.read(buffer); bytes != (size - seek_temp)) {
log_error("Fail to read file");
Expand Down