diff options
| author | Marcel Braun <marcel.braun@ost.ch> | 2026-05-04 19:47:15 +0200 |
|---|---|---|
| committer | Lukas Oesch <lukasoesch20@gmail.com> | 2026-05-05 14:38:52 +0200 |
| commit | 72d686961a26789b7659f17fb090511ee28604ec (patch) | |
| tree | 04161c3b1d7a3b93951f63616893ba67de1530d0 /kernel/include | |
| parent | 1269410c4c733ed38859b3e70713728afb273443 (diff) | |
| download | kernel-72d686961a26789b7659f17fb090511ee28604ec.tar.xz kernel-72d686961a26789b7659f17fb090511ee28604ec.zip | |
Add helper functions for path validation and splitting
Diffstat (limited to 'kernel/include')
| -rw-r--r-- | kernel/include/kernel/filesystem/path.hpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/kernel/include/kernel/filesystem/path.hpp b/kernel/include/kernel/filesystem/path.hpp new file mode 100644 index 0000000..976926f --- /dev/null +++ b/kernel/include/kernel/filesystem/path.hpp @@ -0,0 +1,56 @@ +#ifndef TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP +#define TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP + +#include <ranges> +#include <string_view> + +namespace kernel::filesystem::path +{ + /** + @brief Provides utilities for handling filesystem paths, including validation and splitting into components. + */ + + /** + @brief Checks if the given path is a valid absolute path (starts with '/'). + @param path The path to check. + @return true if the path is a valid absolute path, false otherwise. + */ + auto inline is_valid_absolute_path(std::string_view path) -> bool + { + return !path.empty() && path.front() == '/'; + } + + /** + @brief Checks if the given path is a valid relative path (doesn't start with '/'). + @param path The path to check. + @return true if the path is a valid relative path, false otherwise. + */ + auto inline is_valid_relative_path(std::string_view path) -> bool + { + return !path.empty() && path.front() != '/'; + } + + /** + @brief Checks if the given path is a valid path (either absolute or relative). + @param path The path to check. + @return true if the path is a valid path, false otherwise. + */ + auto inline is_valid_path(std::string_view path) -> bool + { + return is_valid_absolute_path(path) || is_valid_relative_path(path); + } + + /** + @brief Splits the given path into its components. + @param path The path to split. + @return A range of string views 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 std::string_view(part.begin(), part.end()); }); + } + +} // namespace kernel::filesystem::path + +#endif // TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP
\ No newline at end of file |
