| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title Os/Directory.cpp | ||
| 3 | // \brief common function implementation for Os::Directory | ||
| 4 | // ====================================================================== | ||
| 5 | #include <Fw/Types/Assert.hpp> | ||
| 6 | #include <Os/Directory.hpp> | ||
| 7 | |||
| 8 | namespace Os { | ||
| 9 | |||
| 10 | 2486 | Directory::Directory() | |
| 11 |
3/3✓ Branch 13 taken 39776 times.
✓ Branch 14 taken 2486 times.
✓ Branch 20 taken 2486 times.
|
42262 | : m_is_open(false), m_handle_storage(), m_delegate(*DirectoryInterface::getDelegate(m_handle_storage)) { |
| 12 | 2486 | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 13 | 2486 | } | |
| 14 | |||
| 15 | 4974 | Directory::~Directory() { | |
| 16 | 4972 | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 17 |
3/4✗ Branch 3 not taken.
✓ Branch 4 taken 2486 times.
✓ Branch 5 taken 223 times.
✓ Branch 6 taken 2263 times.
|
4972 | if (this->m_is_open) { |
| 18 | 446 | this->close(); | |
| 19 | } | ||
| 20 | 4972 | this->m_delegate.~DirectoryInterface(); | |
| 21 | 4974 | } | |
| 22 | |||
| 23 | // ------------------------------------------------------------ | ||
| 24 | // Directory operations delegating to implementation | ||
| 25 | // ------------------------------------------------------------ | ||
| 26 | ✗ | DirectoryHandle* Directory::getHandle() { | |
| 27 | ✗ | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 28 | ✗ | return this->m_delegate.getHandle(); | |
| 29 | } | ||
| 30 | |||
| 31 | 735 | Directory::Status Directory::open(const char* path, OpenMode mode) { | |
| 32 | 735 | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 33 | 735 | FW_ASSERT(path != nullptr); | |
| 34 | 735 | FW_ASSERT(mode >= 0 and mode < OpenMode::MAX_OPEN_MODE); | |
| 35 | 735 | Status status = this->m_delegate.open(path, mode); | |
| 36 |
2/2✓ Branch 0 taken 730 times.
✓ Branch 1 taken 5 times.
|
735 | if (status == Status::OP_OK) { |
| 37 | 730 | this->m_is_open = true; | |
| 38 | } | ||
| 39 | 735 | return status; | |
| 40 | } | ||
| 41 | |||
| 42 | 948 | bool Directory::isOpen() { | |
| 43 | 948 | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 44 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 948 times.
|
948 | return this->m_is_open; |
| 45 | } | ||
| 46 | 937 | Directory::Status Directory::rewind() { | |
| 47 | 937 | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 48 |
3/4✗ Branch 3 not taken.
✓ Branch 4 taken 937 times.
✓ Branch 5 taken 69 times.
✓ Branch 6 taken 868 times.
|
937 | if (not this->m_is_open) { |
| 49 | 69 | return Status::NOT_OPENED; | |
| 50 | } | ||
| 51 | 868 | return this->m_delegate.rewind(); | |
| 52 | } | ||
| 53 | |||
| 54 | 3979 | Directory::Status Directory::read(char* fileNameBuffer, FwSizeType bufSize) { | |
| 55 | 3979 | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 56 |
3/4✗ Branch 3 not taken.
✓ Branch 4 taken 3979 times.
✓ Branch 5 taken 82 times.
✓ Branch 6 taken 3897 times.
|
3979 | if (not this->m_is_open) { |
| 57 | 82 | return Status::NOT_OPENED; | |
| 58 | } | ||
| 59 | 3897 | FW_ASSERT(fileNameBuffer != nullptr); | |
| 60 | 3897 | FW_ASSERT(bufSize > 0); | |
| 61 | 3897 | Status status = this->m_delegate.read(fileNameBuffer, bufSize); | |
| 62 | 3897 | fileNameBuffer[bufSize - 1] = '\0'; // Guarantee null-termination | |
| 63 | 3897 | return status; | |
| 64 | } | ||
| 65 | |||
| 66 | 3770 | Directory::Status Directory::read(Fw::StringBase& filename) { | |
| 67 | 3770 | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 68 |
3/4✗ Branch 3 not taken.
✓ Branch 4 taken 3770 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 3769 times.
|
3770 | if (not this->m_is_open) { |
| 69 | 1 | return Status::NOT_OPENED; | |
| 70 | } | ||
| 71 | 3769 | return this->m_delegate.read(const_cast<char*>(filename.toChar()), filename.getCapacity()); | |
| 72 | } | ||
| 73 | |||
| 74 | 826 | void Directory::close() { | |
| 75 | 826 | FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0])); | |
| 76 | 826 | this->m_is_open = false; | |
| 77 | 826 | return this->m_delegate.close(); | |
| 78 | } | ||
| 79 | |||
| 80 | // ------------------------------------------------------------ | ||
| 81 | // Common functions built on top of OS-specific functions | ||
| 82 | // ------------------------------------------------------------ | ||
| 83 | |||
| 84 | 318 | Directory::Status Directory::getFileCount(FwSizeType& fileCount) { | |
| 85 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 318 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 318 times.
|
318 | if (not this->m_is_open) { |
| 86 | ✗ | return Status::NOT_OPENED; | |
| 87 | } | ||
| 88 | // Rewind to ensure we start from the beginning of the stream | ||
| 89 |
2/3✓ Branch 4 taken 318 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 318 times.
|
318 | if (this->rewind() != Status::OP_OK) { |
| 90 | ✗ | return Status::OTHER_ERROR; | |
| 91 | } | ||
| 92 | 318 | const FwSizeType loopLimit = std::numeric_limits<FwSizeType>::max(); | |
| 93 | 318 | FwSizeType count = 0; | |
| 94 | 318 | char unusedBuffer[1]; // buffer must have size but is unused | |
| 95 | 318 | Status readStatus = Status::OP_OK; | |
| 96 | 318 | fileCount = 0; | |
| 97 | // Count files by reading each file entry until there is NO_MORE_FILES | ||
| 98 |
1/2✓ Branch 0 taken 3810 times.
✗ Branch 1 not taken.
|
3810 | for (FwSizeType iter = 0; iter < loopLimit; ++iter) { |
| 99 |
1/1✓ Branch 4 taken 3810 times.
|
3810 | readStatus = this->read(unusedBuffer, sizeof(unusedBuffer)); |
| 100 |
2/2✓ Branch 0 taken 318 times.
✓ Branch 1 taken 3492 times.
|
3810 | if (readStatus == Status::NO_MORE_FILES) { |
| 101 | 318 | break; | |
| 102 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3492 times.
|
3492 | } else if (readStatus != Status::OP_OK) { |
| 103 | ✗ | return Status::OTHER_ERROR; | |
| 104 | } | ||
| 105 | 3492 | count++; | |
| 106 | } | ||
| 107 | 318 | fileCount = count; | |
| 108 |
2/3✓ Branch 4 taken 318 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 318 times.
|
318 | if (this->rewind() != Status::OP_OK) { |
| 109 | ✗ | return Status::OTHER_ERROR; | |
| 110 | } | ||
| 111 | 318 | return Status::OP_OK; | |
| 112 | } | ||
| 113 | |||
| 114 | 81 | Directory::Status Directory::readDirectory(Fw::ExternalArray<Fw::String>& filenameArray, FwSizeType& filenameCount) { | |
| 115 | 81 | FW_ASSERT(filenameArray.getElements() != nullptr); | |
| 116 | 81 | FW_ASSERT(filenameArray.getSize() > 0); | |
| 117 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 81 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 81 times.
|
81 | if (not this->m_is_open) { |
| 118 | ✗ | return Status::NOT_OPENED; | |
| 119 | } | ||
| 120 | // Rewind to ensure we start reading from the beginning of the stream | ||
| 121 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 81 times.
|
81 | if (this->rewind() != Status::OP_OK) { |
| 122 | ✗ | return Status::OTHER_ERROR; | |
| 123 | } | ||
| 124 | |||
| 125 | 81 | Status readStatus = Status::OP_OK; | |
| 126 | 81 | Status returnStatus = Status::OP_OK; | |
| 127 | FwSizeType index; | ||
| 128 | 81 | filenameCount = 0; | |
| 129 | // Iterate through the directory and read the filenames into the array | ||
| 130 |
2/2✓ Branch 2 taken 84 times.
✓ Branch 3 taken 1 times.
|
85 | for (index = 0; index < filenameArray.getSize(); index++) { |
| 131 | 84 | readStatus = this->read(filenameArray[index]); | |
| 132 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 4 times.
|
84 | if (readStatus == Status::NO_MORE_FILES) { |
| 133 | 80 | break; | |
| 134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | } else if (readStatus != Status::OP_OK) { |
| 135 | ✗ | return Status::OTHER_ERROR; | |
| 136 | } | ||
| 137 | } | ||
| 138 | 81 | filenameCount = index; | |
| 139 | |||
| 140 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 81 times.
|
81 | if (this->rewind() != Status::OP_OK) { |
| 141 | ✗ | return Status::OTHER_ERROR; | |
| 142 | } | ||
| 143 | |||
| 144 | 81 | return returnStatus; | |
| 145 | } | ||
| 146 | |||
| 147 | ✗ | Directory::Status Directory::readDirectory(Fw::String filenameArray[], | |
| 148 | const FwSizeType filenameArraySize, | ||
| 149 | FwSizeType& filenameCount) { | ||
| 150 | ✗ | Fw::ExternalArray<Fw::String> array(filenameArray, filenameArraySize); | |
| 151 | ✗ | return this->readDirectory(array, filenameCount); | |
| 152 | ✗ | } | |
| 153 | |||
| 154 | } // namespace Os | ||
| 155 |