F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Directory.cpp
Go to the documentation of this file.
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 
11  : m_is_open(false), m_handle_storage(), m_delegate(*DirectoryInterface::getDelegate(m_handle_storage)) {
12  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
13 }
14 
16  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
17  if (this->m_is_open) {
18  this->close();
19  }
20  this->m_delegate.~DirectoryInterface();
21 }
22 
23 // ------------------------------------------------------------
24 // Directory operations delegating to implementation
25 // ------------------------------------------------------------
27  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
28  return this->m_delegate.getHandle();
29 }
30 
31 Directory::Status Directory::open(const char* path, OpenMode mode) {
32  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
33  FW_ASSERT(path != nullptr);
34  FW_ASSERT(mode >= 0 and mode < OpenMode::MAX_OPEN_MODE);
35  Status status = this->m_delegate.open(path, mode);
36  if (status == Status::OP_OK) {
37  this->m_is_open = true;
38  }
39  return status;
40 }
41 
43  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
44  return this->m_is_open;
45 }
47  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
48  if (not this->m_is_open) {
49  return Status::NOT_OPENED;
50  }
51  return this->m_delegate.rewind();
52 }
53 
54 Directory::Status Directory::read(char* fileNameBuffer, FwSizeType bufSize) {
55  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
56  if (not this->m_is_open) {
57  return Status::NOT_OPENED;
58  }
59  FW_ASSERT(fileNameBuffer != nullptr);
60  Status status = this->m_delegate.read(fileNameBuffer, bufSize);
61  fileNameBuffer[bufSize - 1] = '\0'; // Guarantee null-termination
62  return status;
63 }
64 
66  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
67  if (not this->m_is_open) {
68  return Status::NOT_OPENED;
69  }
70  return this->m_delegate.read(const_cast<char*>(filename.toChar()), filename.getCapacity());
71 }
72 
74  FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
75  this->m_is_open = false;
76  return this->m_delegate.close();
77 }
78 
79 // ------------------------------------------------------------
80 // Common functions built on top of OS-specific functions
81 // ------------------------------------------------------------
82 
84  if (not this->m_is_open) {
85  return Status::NOT_OPENED;
86  }
87  // Rewind to ensure we start from the beginning of the stream
88  if (this->rewind() != Status::OP_OK) {
89  return Status::OTHER_ERROR;
90  }
91  const FwSizeType loopLimit = std::numeric_limits<FwSizeType>::max();
92  FwSizeType count = 0;
93  char unusedBuffer[1]; // buffer must have size but is unused
94  Status readStatus = Status::OP_OK;
95  fileCount = 0;
96  // Count files by reading each file entry until there is NO_MORE_FILES
97  for (FwSizeType iter = 0; iter < loopLimit; ++iter) {
98  readStatus = this->read(unusedBuffer, sizeof(unusedBuffer));
99  if (readStatus == Status::NO_MORE_FILES) {
100  break;
101  } else if (readStatus != Status::OP_OK) {
102  return Status::OTHER_ERROR;
103  }
104  count++;
105  }
106  fileCount = count;
107  if (this->rewind() != Status::OP_OK) {
108  return Status::OTHER_ERROR;
109  }
110  return Status::OP_OK;
111 }
112 
114  const FwSizeType filenameArraySize,
115  FwSizeType& filenameCount) {
116  FW_ASSERT(filenameArray != nullptr);
117  FW_ASSERT(filenameArraySize > 0);
118  if (not this->m_is_open) {
119  return Status::NOT_OPENED;
120  }
121  // Rewind to ensure we start reading from the beginning of the stream
122  if (this->rewind() != Status::OP_OK) {
123  return Status::OTHER_ERROR;
124  }
125 
126  Status readStatus = Status::OP_OK;
127  Status returnStatus = Status::OP_OK;
128  FwSizeType index;
129  filenameCount = 0;
130  // Iterate through the directory and read the filenames into the array
131  for (index = 0; index < filenameArraySize; index++) {
132  readStatus = this->read(filenameArray[index]);
133  if (readStatus == Status::NO_MORE_FILES) {
134  break;
135  } else if (readStatus != Status::OP_OK) {
136  return Status::OTHER_ERROR;
137  }
138  }
139  filenameCount = index;
140 
141  if (this->rewind() != Status::OP_OK) {
142  return Status::OTHER_ERROR;
143  }
144 
145  return returnStatus;
146 }
147 
148 } // namespace Os
Status readDirectory(Fw::String filenameArray[], const FwSizeType arraySize, FwSizeType &filenameCount)
Read the contents of the directory and store filenames in filenameArray of size arraySize.
Definition: Directory.cpp:113
virtual DirectoryHandle * getHandle()=0
return the underlying Directory handle (implementation specific)
virtual ~DirectoryInterface()=default
default virtual destructor
Operation succeeded.
Definition: Os.hpp:26
virtual Status read(char *fileNameBuffer, FwSizeType buffSize)=0
Get next filename from directory stream.
A catch-all for other errors. Have to look in implementation-specific code.
PlatformSizeType FwSizeType
Status rewind() override
Rewind directory stream.
Definition: Directory.cpp:46
Directory()
Constructor.
Definition: Directory.cpp:10
Status getFileCount(FwSizeType &fileCount)
Get the number of files in the directory.
Definition: Directory.cpp:83
void close() override
Close directory.
Definition: Directory.cpp:73
Status read(char *fileNameBuffer, FwSizeType buffSize) override
Get next filename from directory stream.
Definition: Directory.cpp:54
bool isOpen()
Check if Directory is open or not.
Definition: Directory.cpp:42
Status open(const char *path, OpenMode mode) override
Open or create a directory.
Definition: Directory.cpp:31
virtual Status open(const char *path, OpenMode mode)=0
Open or create a directory.
DirectoryHandle * getHandle() override
return the underlying Directory handle (implementation specific)
Definition: Directory.cpp:26
virtual Status rewind()=0
Rewind directory stream.
virtual SizeType getCapacity() const =0
return size of buffer
~Directory() final
Destructor.
Definition: Directory.cpp:15
#define FW_ASSERT(...)
Definition: Assert.hpp:14
virtual void close()=0
Get next filename from directory stream and write it to a Fw::StringBase object.
virtual const CHAR * toChar() const =0