aboutsummaryrefslogtreecommitdiff
path: root/kernel/kernel/vfs/path.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/kernel/vfs/path.hpp')
-rw-r--r--kernel/kernel/vfs/path.hpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/kernel/kernel/vfs/path.hpp b/kernel/kernel/vfs/path.hpp
new file mode 100644
index 00000000..e3dbe8d5
--- /dev/null
+++ b/kernel/kernel/vfs/path.hpp
@@ -0,0 +1,97 @@
+#ifndef TEACHOS_KERNEL_VFS_PATH_HPP
+#define TEACHOS_KERNEL_VFS_PATH_HPP
+
+#include <kernel/vfs/constants.hpp>
+
+#include <kstd/string.hpp>
+
+#include <ranges>
+#include <string_view>
+#include <utility>
+
+namespace kernel::vfs::path
+{
+
+ //! Check if the given path is within the maximum allowed length.
+ //!
+ //! @param path The path to check.
+ //! @return @c true if the path length is valid, @c false otherwise.
+ auto inline is_valid_path_length(std::string_view path) -> bool
+ {
+ return path.length() < kernel::vfs::constants::max_path_length;
+ }
+
+ //! Check if the given path is an absolute path.
+ //!
+ //! @param path The path to check.
+ //! @return @c true if the path is an absolute path, @c false otherwise.
+ auto inline is_valid_absolute_path(std::string_view path) -> bool
+ {
+ return !path.empty() && path.front() == '/' && is_valid_path_length(path);
+ }
+
+ //! Check if the given path is a relative path.
+ //!
+ //! @param path The path to check.
+ //! @return @c true if the path is a relative path, @c false otherwise.
+ auto inline is_valid_relative_path(std::string_view path) -> bool
+ {
+ return !path.empty() && path.front() != '/' && is_valid_path_length(path);
+ }
+
+ //! Check if the given path is a valid path.
+ //!
+ //! @param path The path to check.
+ //! @return @c true if the path is a valid path, @c false otherwise.
+ auto inline is_valid_path(std::string_view path) -> bool
+ {
+ return is_valid_absolute_path(path) || is_valid_relative_path(path);
+ }
+
+ //! Split the given path into its components.
+ //!
+ //! @param path The path to split.
+ //! @return A range of strings representing the components of the path.
+ auto inline split(std::string_view path)
+ {
+ return std::views::split(path, '/') | std::views::filter([](auto const & part) { return !part.empty(); }) |
+ std::views::transform(
+ [](auto const & part) { return kstd::string(std::string_view(part.begin(), part.end())); });
+ }
+
+ //! Split the given path into its parent path and filename components.
+ //!
+ //! @param path The path to split.
+ //! @return A pair of string views representing the parent path and filename.
+ auto inline split_into_path_and_filename(std::string_view path) -> std::pair<std::string_view, std::string_view>
+ {
+ if (path.empty())
+ {
+ return {"", ""};
+ }
+
+ if (path.length() > 1 && path.back() == '/')
+ {
+ path = path.substr(0, path.length() - 1);
+ }
+
+ auto last_separator_index = path.rfind('/');
+ if (last_separator_index == std::string_view::npos)
+ {
+ return {"", path};
+ }
+ else
+ {
+ auto parent_path = path.substr(0, last_separator_index);
+ if (parent_path.empty())
+ {
+ parent_path = "/";
+ }
+ auto filename = path.substr(last_separator_index + 1);
+ return {parent_path, filename};
+ }
+ }
+
+} // namespace kernel::vfs::path
+
+#endif // TEACHOS_KERNEL_FILESYSTEM_PATH_HPP \ No newline at end of file