aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/kernel/filesystem/path.hpp
blob: 4845bf1a8dc1b007288c315856f4788e7529d8f7 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP
#define TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP

#include <kernel/filesystem/constants.hpp>

#include <kstd/string>

#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 within the maximum allowed length.
  @param path The path to check.
  @return true if the path length is valid, false otherwise.
  */
  auto inline is_valid_path_length(std::string_view path) -> bool
  {
    return path.length() < kernel::filesystem::constants::max_path_length;
  }

  /**
  @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() == '/' && is_valid_path_length(path);
  }

  /**
  @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() != '/' && is_valid_path_length(path);
  }

  /**
  @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