aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/kernel/filesystem/path.hpp
blob: 298ac5f28b2806e74ae36f6581f5c2f87472f430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP
#define TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP

#include <ranges>
#include <string_view>
#include <kstd/string>

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 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())); });
  }

}  // namespace kernel::filesystem::path

#endif  // TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP