F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Directory.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Directory.hpp
3 // \brief Os::Directory interface definition
4 // ======================================================================
5 
6 #ifndef _OS_DIRECTORY_HPP_
7 #define _OS_DIRECTORY_HPP_
8 
9 #include <FpConfig.hpp>
10 #include <Os/Os.hpp>
11 #include <Fw/Types/String.hpp>
12 
13 namespace Os {
14 
15 struct DirectoryHandle {};
16 
18  public:
19  enum Status {
31  };
32 
33  enum OpenMode {
34  READ,
38  };
39 
41  DirectoryInterface() = default;
42 
44  virtual ~DirectoryInterface() = default;
45 
47  DirectoryInterface(const DirectoryInterface& other) = delete;
48 
50  DirectoryInterface& operator=(const DirectoryInterface& other) = delete;
51 
54  virtual DirectoryHandle* getHandle() = 0;
55 
57  static DirectoryInterface* getDelegate(DirectoryHandleStorage& aligned_new_memory);
58 
59 
60  // -----------------------------------------------------------------
61  // Directory operations to be implemented by an OSAL implementation
62  // -----------------------------------------------------------------
63  // These functions are to be overridden in each OS implementation
64  // See an example in in Os/Posix/Directory.hpp
65 
79  virtual Status open(const char* path, OpenMode mode) = 0;
80 
86  virtual Status rewind() = 0;
87 
99  virtual Status read(char * fileNameBuffer, FwSizeType buffSize) = 0;
100 
105  // virtual Status read(Fw::StringBase& filename) = 0;
106 
108  virtual void close() = 0;
109 
110 
111 };
112 
117 class Directory final : public DirectoryInterface {
118  public:
120  Directory();
121 
125  ~Directory() final;
126 
129  DirectoryHandle* getHandle() override;
130 
131 
132  // ------------------------------------------------------------
133  // Implementation-specific Directory member functions
134  // ------------------------------------------------------------
135  // These functions are overridden in each OS implementation (e.g. in Os/Posix/Directory.hpp)
136 
150  Status open(const char* path, OpenMode mode) override;
151 
154  bool isOpen();
155 
161  Status rewind() override;
162 
174  Status read(char * fileNameBuffer, FwSizeType buffSize) override;
175 
176 
178  void close() override;
179 
180 
181  // ------------------------------------------------------------
182  // Common functions built on top of OS-specific functions
183  // ------------------------------------------------------------
184 
189  Status read(Fw::StringBase& filename);
190 
200  Status readDirectory(Fw::String filenameArray[], const FwSizeType arraySize, FwSizeType& filenameCount);
201 
211  Status getFileCount(FwSizeType& fileCount);
212 
213  private:
214  bool m_is_open;
215 
216  private:
217  // This section is used to store the implementation-defined Directory handle. To Os::Directory and fprime, this type is
218  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
219  // the byte-array here and set `handle` to that address for storage.
220  alignas(FW_HANDLE_ALIGNMENT) DirectoryHandleStorage m_handle_storage;
221  DirectoryInterface& m_delegate;
222 };
223 
224 }
225 
226 #endif
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
Directory has more files than can be read.
Definition: Directory.hpp:26
virtual DirectoryHandle * getHandle()=0
return the underlying Directory handle (implementation specific)
virtual ~DirectoryInterface()=default
default virtual destructor
U8 DirectoryHandleStorage[FW_DIRECTORY_HANDLE_MAX_SIZE]
Definition: Os.hpp:15
virtual Status read(char *fileNameBuffer, FwSizeType buffSize)=0
Get next filename from directory stream.
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
Directory doesn&#39;t exist.
Definition: Directory.hpp:21
DirectoryInterface()=default
default constructor
Status rewind() override
Rewind directory stream.
Definition: Directory.cpp:45
Directory hasn&#39;t been opened yet.
Definition: Directory.hpp:23
Directory()
Constructor.
Definition: Directory.cpp:10
A catch-all for other errors. Have to look in implementation-specific code.
Definition: Directory.hpp:30
No permission to read directory.
Definition: Directory.hpp:22
Status getFileCount(FwSizeType &fileCount)
Get the number of files in the directory.
Definition: Directory.cpp:82
void close() override
Close directory.
Definition: Directory.cpp:72
Status read(char *fileNameBuffer, FwSizeType buffSize) override
Get next filename from directory stream.
Definition: Directory.cpp:53
Create directory if it doesn&#39;t exist.
Definition: Directory.hpp:35
Directory stream has no more files.
Definition: Directory.hpp:25
C++-compatible configuration header for fprime configuration.
DirectoryInterface & operator=(const DirectoryInterface &other)=delete
assignment operator is forbidden
Directory class.
Definition: Directory.hpp:117
Create directory and error if it already exists.
Definition: Directory.hpp:36
Maximum value of OpenMode.
Definition: Directory.hpp:37
Directory stream descriptor is invalid.
Definition: Directory.hpp:27
Operation was successful.
Definition: Directory.hpp:20
bool isOpen()
Check if Directory is open or not.
Definition: Directory.cpp:41
Status open(const char *path, OpenMode mode) override
Open or create a directory.
Definition: Directory.cpp:30
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:25
static DirectoryInterface * getDelegate(DirectoryHandleStorage &aligned_new_memory)
provide a pointer to a Directory delegate object
Definition: DefaultFile.cpp:20
Directory already exists.
Definition: Directory.hpp:28
virtual Status rewind()=0
Rewind directory stream.
Operation is not supported by the current implementation.
Definition: Directory.hpp:29
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:440
~Directory() final
Destructor.
Definition: Directory.cpp:14
virtual void close()=0
Get next filename from directory stream and write it to a Fw::StringBase object.
Error if directory doesn&#39;t exist.
Definition: Directory.hpp:34
Path is not a directory.
Definition: Directory.hpp:24