aboutsummaryrefslogtreecommitdiff
path: root/kernel/kernel/filesystem/path.hpp
blob: a0dbd1f13b1056cf00692644120d893e45e0c0a4 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP
#define TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP

#include <kernel/filesystem/constants.hpp>

#include <kstd/string.hpp>

#include <ranges>
#include <string_view>
#include <utility>

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

  /**
  @brief Splits 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::filesystem::path

#endif  // TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP