From 5407c65e2ca7bba6e98566b9d37408d6d4586ade Mon Sep 17 00:00:00 2001 From: Mourad Latoundji Date: Sun, 20 Feb 2022 19:52:46 +0100 Subject: [PATCH 1/2] :sparkles: (file_system_kit): Add 'tell' function --- include/interface/platform/File.h | 2 ++ libs/FileSystemKit/include/FileSystemKit.h | 2 ++ libs/FileSystemKit/source/File.cpp | 8 +++++++ libs/FileSystemKit/tests/File_test.cpp | 26 ++++++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/include/interface/platform/File.h b/include/interface/platform/File.h index e513fdae46..93c17f8c7d 100644 --- a/include/interface/platform/File.h +++ b/include/interface/platform/File.h @@ -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; }; diff --git a/libs/FileSystemKit/include/FileSystemKit.h b/libs/FileSystemKit/include/FileSystemKit.h index 78a7575d0b..8bb1912ce3 100644 --- a/libs/FileSystemKit/include/FileSystemKit.h +++ b/libs/FileSystemKit/include/FileSystemKit.h @@ -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: diff --git a/libs/FileSystemKit/source/File.cpp b/libs/FileSystemKit/source/File.cpp index b8a51477fd..7cabb13136 100644 --- a/libs/FileSystemKit/source/File.cpp +++ b/libs/FileSystemKit/source/File.cpp @@ -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()); +} diff --git a/libs/FileSystemKit/tests/File_test.cpp b/libs/FileSystemKit/tests/File_test.cpp index afbd8c74dd..33df31f020 100644 --- a/libs/FileSystemKit/tests/File_test.cpp +++ b/libs/FileSystemKit/tests/File_test.cpp @@ -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({0x61, 0x62, 0x63, 0x64, 0x65, 0x66}); // "abcdef" + + file.open(tempFilename); + file.write(input_data); + file.seek(3); + + auto actual_pos = file.tell(); + + ASSERT_EQ(3, actual_pos); +} From aab350582697dfe47eacc7bc87b3d48fe12f40dc Mon Sep 17 00:00:00 2001 From: Mourad Latoundji Date: Tue, 8 Mar 2022 10:03:03 +0100 Subject: [PATCH 2/2] :art: (spikes): Update spike lk_filesystem_kit --- libs/FileSystemKit/tests/File_test.cpp | 2 +- spikes/lk_filesystem_kit/main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/FileSystemKit/tests/File_test.cpp b/libs/FileSystemKit/tests/File_test.cpp index 33df31f020..ff10f61424 100644 --- a/libs/FileSystemKit/tests/File_test.cpp +++ b/libs/FileSystemKit/tests/File_test.cpp @@ -459,7 +459,7 @@ TEST_F(FileTest, tellFile) { auto input_data = std::to_array({0x61, 0x62, 0x63, 0x64, 0x65, 0x66}); // "abcdef" - file.open(tempFilename); + file.open(tempFilename, "w"); file.write(input_data); file.seek(3); diff --git a/spikes/lk_filesystem_kit/main.cpp b/spikes/lk_filesystem_kit/main.cpp index cd5bfdc62f..89eb39d1be 100644 --- a/spikes/lk_filesystem_kit/main.cpp +++ b/spikes/lk_filesystem_kit/main.cpp @@ -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");