diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-09-28 11:14:31 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-09-28 11:14:31 -0500 |
commit | b6967c8a9190efa4e9128850fa723fe3ea3140f7 (patch) | |
tree | 101be12055abd27968fce2666d573c80f8afe22d /include | |
parent | 987466c74332025fae74d196ff680a57ccb247a2 (diff) |
Expand directory interface, remove FileStatus for FudStatus.
Diffstat (limited to 'include')
-rw-r--r-- | include/fud_c_file.hpp | 53 | ||||
-rw-r--r-- | include/fud_directory.hpp | 43 |
2 files changed, 57 insertions, 39 deletions
diff --git a/include/fud_c_file.hpp b/include/fud_c_file.hpp index 674130c..9605ff0 100644 --- a/include/fud_c_file.hpp +++ b/include/fud_c_file.hpp @@ -18,6 +18,7 @@ #ifndef FUD_C_FILE_HPP #define FUD_C_FILE_HPP +#include "fud_status.hpp" #include "fud_result.hpp" #include "fud_string.hpp" @@ -77,64 +78,40 @@ constexpr const char* CTextFileModeFromFlags(CFileMode mode) } } -enum class FileStatus -{ - Success, - PartialSuccess, - InvalidName, - InvalidArgument, - InvalidState, - NullPointer, - Error, -}; - -static inline const char* FileStatusToString(FileStatus status) -{ - switch (status) { - case FileStatus::Success: - return "Success"; - case FileStatus::PartialSuccess: - return "PartialSuccess"; - case FileStatus::InvalidName: - return "InvalidName"; - case FileStatus::InvalidArgument: - return "InvalidArgument"; - case FileStatus::InvalidState: - return "InvalidState"; - case FileStatus::NullPointer: - return "NullPointer"; - case FileStatus::Error: - return "Error"; - default: - return "Unknown"; - } -} - struct [[nodiscard]] ReadResult { size_t bytesRead{0}; - FileStatus status{FileStatus::Success}; + FudStatus status{FudStatus::Success}; }; struct [[nodiscard]] WriteResult { size_t bytesWritten{0}; - FileStatus status{FileStatus::Success}; + FudStatus status{FudStatus::Success}; }; class CBinaryFile { public: CBinaryFile(const String& filename, CFileMode mode); + CBinaryFile(const String& filename, CFileMode mode, const String& extraFlags); + CBinaryFile(const CBinaryFile& rhs) = delete; + ~CBinaryFile(); + CBinaryFile& operator=(const CBinaryFile& rhs) = delete; - FileStatus open(); + + CBinaryFile& operator=(CBinaryFile&& rhs); + + FudStatus open(); + void close(); + const FILE* file() const; [[nodiscard]] FILE* file(); [[nodiscard]] bool isOpen() const; - [[nodiscard]] Result<size_t, FileStatus> size() const; + [[nodiscard]] Result<size_t, FudStatus> size() const; [[nodiscard]] ReadResult read(void* destination, size_t destinationSize, size_t length); @@ -179,7 +156,7 @@ class CBinaryFile { } private: - FileStatus reset() const; + FudStatus reset() const; const String m_filename; String m_extraFlags{}; diff --git a/include/fud_directory.hpp b/include/fud_directory.hpp index ac48b7b..5a988dc 100644 --- a/include/fud_directory.hpp +++ b/include/fud_directory.hpp @@ -24,9 +24,9 @@ #include <cstdint> #include <cstdio> +#include <ctime> #include <dirent.h> #include <optional> -#include <ctime> namespace fud { @@ -42,6 +42,31 @@ enum class DirectoryEntryType : uint8_t Unknown }; +constexpr const char* DirectoryEntryTypeToString(DirectoryEntryType entryType) +{ + switch (entryType) { + case DirectoryEntryType::Block: + return "Block"; + case DirectoryEntryType::Character: + return "Character"; + case DirectoryEntryType::Directory: + return "Directory"; + case DirectoryEntryType::NamedPipe: + return "Named Pipe"; + case DirectoryEntryType::SymbolicLink: + return "Symbolic Link"; + case DirectoryEntryType::RegularFile: + return "Regular File"; + case DirectoryEntryType::UnixSocket: + return "Unix Socket"; + case DirectoryEntryType::Unknown: + default: + return "Unknown"; + } +} + +struct Stat; + /** \brief Simplified Directory Entry */ struct DirectoryEntry { String name; @@ -49,6 +74,18 @@ struct DirectoryEntry { size_t links; time_t modificationTime; DirectoryEntryType entryType; + + static Result<DirectoryEntry, FudStatus> fromStat(const String& name, const Stat& statBuffer); + + [[nodiscard]] constexpr bool isDirectory() const + { + return entryType == DirectoryEntryType::Directory; + } + + [[nodiscard]] constexpr bool isRegularFile() const + { + return entryType == DirectoryEntryType::RegularFile; + } }; class Directory { @@ -70,11 +107,15 @@ class Directory { return m_errorCode; } + Result<DirectoryEntry, FudStatus> info(); + Result<std::optional<DirectoryEntry>, FudStatus> getNextEntry(); FudStatus reset(); private: + bool valid() const; + String m_name{}; DIR* m_directory{nullptr}; FudStatus m_status{FudStatus::ObjectInvalid}; |