Skip to content

Commit

Permalink
✨ (file): Add clear file
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Dec 9, 2022
1 parent d249709 commit 34ac265
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/interface/platform/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct File {

virtual void seek(size_t pos, int origin) = 0;

virtual void clear() = 0;
virtual void rewind() = 0;

virtual auto size() -> std::size_t = 0;
Expand Down
4 changes: 4 additions & 0 deletions libs/FileManagerKit/include/FileManagerKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ struct File : public interface::File, public mbed::NonCopyable<File> {
auto write(const char *data, uint32_t size) -> std::size_t final;

void seek(size_t pos, int origin = SEEK_SET) final;

void clear() final;
void rewind() final;

auto size() -> std::size_t final;

auto tell() -> std::size_t final;
Expand All @@ -57,6 +60,7 @@ struct File : public interface::File, public mbed::NonCopyable<File> {

private:
std::unique_ptr<FILE, decltype(&fclose)> _file {nullptr, &fclose};
std::filesystem::path _path;
};

auto create_directory(const std::filesystem::path &path) -> bool;
Expand Down
8 changes: 8 additions & 0 deletions libs/FileManagerKit/source/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ File::File(const std::filesystem::path &path, const char *mode)

auto File::open(const char *path, const char *mode) -> bool
{
_path = path;
_file.reset(std::fopen(path, mode));
return is_open();
}

auto File::open(const std::filesystem::path &path, const char *mode) -> bool
{
_path = path;
_file.reset(std::fopen(path.c_str(), mode));
return is_open();
}
Expand All @@ -49,6 +51,7 @@ void File::close()
return;
}

_path.clear();
_file.reset(nullptr);
}

Expand Down Expand Up @@ -97,6 +100,11 @@ void File::seek(size_t pos, int origin)
std::fseek(_file.get(), static_cast<long>(pos), origin);
}

void File::clear()
{
reopen(_path, "w+");
}

void File::rewind()
{
seek(0);
Expand Down
27 changes: 27 additions & 0 deletions libs/FileManagerKit/tests/File_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,3 +864,30 @@ TEST_F(FileTest, clearError)

ASSERT_FALSE(error_after_clear);
}

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

writeTempFile(input_data);

file.open(tempFilename);
auto size = file.size();
EXPECT_NE(0, size);

file.clear();

size = file.size();
EXPECT_EQ(0, size);

file.close();
}

TEST_F(FileTest, clearNotExistingFile)
{
ASSERT_FALSE(file.is_open());

file.clear();

// nothing expected
}
1 change: 1 addition & 0 deletions tests/unit/mocks/mocks/leka/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class File : public interface::File

MOCK_METHOD(void, seek, (size_t, int), (override));

MOCK_METHOD(void, clear, (), (override));
MOCK_METHOD(void, rewind, (), (override));

MOCK_METHOD(size_t, size, (), (override));
Expand Down

0 comments on commit 34ac265

Please sign in to comment.