summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-09-28 00:23:22 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-09-28 00:23:22 -0500
commit07d30629d78474ed810cad73ead71db96079aff0 (patch)
tree5f43fd8f6a818a590818e0c474f03a1f05076dba /include
parent63711877057f1f89b4d1774e24fe20907a3af656 (diff)
Add directory interface.
Diffstat (limited to 'include')
-rw-r--r--include/fud_c_file.hpp37
-rw-r--r--include/fud_directory.hpp87
-rw-r--r--include/fud_string.hpp8
3 files changed, 127 insertions, 5 deletions
diff --git a/include/fud_c_file.hpp b/include/fud_c_file.hpp
index f563a35..674130c 100644
--- a/include/fud_c_file.hpp
+++ b/include/fud_c_file.hpp
@@ -18,8 +18,8 @@
#ifndef FUD_C_FILE_HPP
#define FUD_C_FILE_HPP
-#include "fud_string.hpp"
#include "fud_result.hpp"
+#include "fud_string.hpp"
#include <cstdint>
#include <cstdio>
@@ -88,7 +88,8 @@ enum class FileStatus
Error,
};
-static inline const char* FileStatusToString(FileStatus status) {
+static inline const char* FileStatusToString(FileStatus status)
+{
switch (status) {
case FileStatus::Success:
return "Success";
@@ -114,6 +115,11 @@ struct [[nodiscard]] ReadResult {
FileStatus status{FileStatus::Success};
};
+struct [[nodiscard]] WriteResult {
+ size_t bytesWritten{0};
+ FileStatus status{FileStatus::Success};
+};
+
class CBinaryFile {
public:
CBinaryFile(const String& filename, CFileMode mode);
@@ -146,6 +152,32 @@ class CBinaryFile {
return read(&destination, sizeof(destination), length, offset);
}
+ [[nodiscard]] WriteResult write(const void* source, size_t sourceSize, size_t length);
+
+ [[nodiscard]] WriteResult write(const void* source, size_t sourceSize, size_t length, size_t offset);
+
+ template <typename T>
+ [[nodiscard]] WriteResult write(const T& source)
+ {
+ return write(source, sizeof(source), sizeof(source));
+ }
+
+ template <typename T>
+ [[nodiscard]] WriteResult write(const T& source, size_t sourceSize, size_t length)
+ {
+ auto offsetResult = size();
+ if (offsetResult.isError()) {
+ return WriteResult{0, offsetResult.getError()};
+ }
+ return write(source, sourceSize, length, offsetResult.getOkay());
+ }
+
+ template <typename T>
+ [[nodiscard]] WriteResult write(const T& source, size_t sourceSize, size_t length, size_t offset)
+ {
+ return write(static_cast<const void*>(&source), sourceSize, length, offset);
+ }
+
private:
FileStatus reset() const;
@@ -158,5 +190,4 @@ class CBinaryFile {
} // namespace fud
-
#endif
diff --git a/include/fud_directory.hpp b/include/fud_directory.hpp
new file mode 100644
index 0000000..ac48b7b
--- /dev/null
+++ b/include/fud_directory.hpp
@@ -0,0 +1,87 @@
+/*
+ * libfud
+ * Copyright 2024 Dominick Allen
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FUD_DIRECTORY_HPP
+#define FUD_DIRECTORY_HPP
+
+#include "fud_result.hpp"
+#include "fud_status.hpp"
+#include "fud_string.hpp"
+
+#include <cstdint>
+#include <cstdio>
+#include <dirent.h>
+#include <optional>
+#include <ctime>
+
+namespace fud {
+
+enum class DirectoryEntryType : uint8_t
+{
+ Block,
+ Character,
+ Directory,
+ NamedPipe,
+ SymbolicLink,
+ RegularFile,
+ UnixSocket,
+ Unknown
+};
+
+/** \brief Simplified Directory Entry */
+struct DirectoryEntry {
+ String name;
+ size_t size;
+ size_t links;
+ time_t modificationTime;
+ DirectoryEntryType entryType;
+};
+
+class Directory {
+ public:
+ explicit Directory(String name);
+ Directory(const Directory& rhs) = delete;
+ Directory(Directory&& rhs);
+ ~Directory();
+ Directory& operator=(const Directory& rhs) = delete;
+ Directory& operator=(Directory&& rhs);
+
+ constexpr FudStatus status() const
+ {
+ return m_status;
+ }
+
+ constexpr int errorCode() const
+ {
+ return m_errorCode;
+ }
+
+ Result<std::optional<DirectoryEntry>, FudStatus> getNextEntry();
+
+ FudStatus reset();
+
+ private:
+ String m_name{};
+ DIR* m_directory{nullptr};
+ FudStatus m_status{FudStatus::ObjectInvalid};
+ int m_errorCode{-1};
+ int m_dirFd{-1};
+};
+
+} // namespace fud
+
+#endif
diff --git a/include/fud_string.hpp b/include/fud_string.hpp
index 97b7036..9e423ef 100644
--- a/include/fud_string.hpp
+++ b/include/fud_string.hpp
@@ -113,9 +113,13 @@ class String {
std::optional<utf8> pop();
- [[nodiscard]] FudStatus catenate(StringView source);
+ [[nodiscard]] FudStatus append(StringView source);
- [[nodiscard]] String append(const String& rhs) const;
+ [[nodiscard]] String catenate(const String& rhs) const;
+
+ [[nodiscard]] String catenate(const char* rhs) const;
+
+ [[nodiscard]] bool compare(const String& rhs) const;
const utf8* begin() const;