Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::filesystem::Path Class Reference

Handles file & directory paths. More...

#include <path.h>

Static Public Member Functions

static bool valid (const string &path)
 Check if a path is valid.
 
static bool is_file (const string &path)
 Check if a path is a file.
 
static string file_name (const string &path)
 Get the file name component of a path if it exists or an empty string.
 
static string file_extension (const string &path)
 Try to get the file extension from a path (assumes split by ".")
 
static string file_path (const string &path)
 Finds the path to the directory the file is in.
 
static string top_directory (const string &path)
 Get the top directory of a path.
 
static string parent_directory (const string &path)
 Get the parent directory of the path.
 
static string absolute_path (const string &path)
 Calculates path with out any "../" or "./".
 
static string join_path (const string &base, const string &extended)
 Joins two paths together.
 

Detailed Description

Handles file & directory paths.

Definition at line 21 of file path.h.

Member Function Documentation

◆ absolute_path()

string Path::absolute_path ( const string path)
static

Calculates path with out any "../" or "./".

Parameters
pathThe path
Returns
The new path, direct from root

Definition at line 170 of file path.cpp.

170 {
171
172 // Split the path into components
173 auto components = path.split("/");
174 common::Vector<string> absolute_components;
175 for (auto &component: components) {
176
177 // Skip empty or self references
178 if (component == "" || component == ".")
179 continue;
180
181 // Handle parent directory references
182 if (component == "..") {
183 if (!absolute_components.empty())
184 absolute_components.pop_back();
185 } else
187 }
188
189 // Join the components back into a path
190 string absolute_path = "/";
191 for(const auto& component : absolute_components)
192 absolute_path += component + "/";
193
194 // Remove trailing slash if file
195 if(Path::is_file(absolute_path) && absolute_path.length() > 1)
196 absolute_path = absolute_path.substring(0, absolute_path.length() - 1);
197
198 return absolute_path;
199}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
static bool is_file(const string &path)
Check if a path is a file.
Definition path.cpp:42
static string absolute_path(const string &path)
Calculates path with out any "../" or "./".
Definition path.cpp:170

References absolute_path(), and is_file().

Referenced by absolute_path(), MaxOS::filesystem::VFSResourceRegistry::create_resource(), and MaxOS::filesystem::VFSResourceRegistry::get_resource().

◆ file_extension()

string Path::file_extension ( const string path)
static

Try to get the file extension from a path (assumes split by ".")

Parameters
pathThe path to get the file extension from
Returns
The file extension or the original path if it does not exist

Definition at line 77 of file path.cpp.

77 {
78
79 // Find the last .
80 int last_dot = -1;
81 for (size_t i = 0; i < path.length(); i++)
82 if (path[i] == '.')
83 last_dot = i;
84
85 // Make sure there was a dot to split
86 if (last_dot == -1)
87 return "";
88
89 // Get the file extension (what is after the ".")
90 string file_extension = path.substring(last_dot + 1, path.length() - last_dot - 1);
91 return file_extension;
92
93}
static string file_extension(const string &path)
Try to get the file extension from a path (assumes split by ".")
Definition path.cpp:77

References file_extension().

Referenced by file_extension(), and is_file().

◆ file_name()

string Path::file_name ( const string path)
static

Get the file name component of a path if it exists or an empty string.

Parameters
pathThe path to get the file name from
Returns
The file name or the original path if it does not exist

Definition at line 54 of file path.cpp.

54 {
55
56 // Find the last /
57 int last_slash = -1;
58 for (size_t i = 0; i < path.length(); i++)
59 if (path[i] == '/')
60 last_slash = i;
61
62 // Make sure there was a slash to split
63 if (last_slash == -1)
64 return path;
65
66 // Get the file name
67 string file_name = path.substring(last_slash + 1, path.length() - last_slash - 1);
68 return file_name;
69}
static string file_name(const string &path)
Get the file name component of a path if it exists or an empty string.
Definition path.cpp:54

References file_name().

Referenced by MaxOS::filesystem::VirtualFileSystem::create_directory(), MaxOS::filesystem::VirtualFileSystem::create_file(), MaxOS::filesystem::VirtualFileSystem::delete_directory(), MaxOS::filesystem::VirtualFileSystem::delete_file(), MaxOS::filesystem::FileSystem::exists(), file_name(), is_file(), and MaxOS::filesystem::VirtualFileSystem::open_file().

◆ file_path()

string Path::file_path ( const string path)
static

Finds the path to the directory the file is in.

Parameters
pathThe path to get the file path from
Returns
The file path or the original path if it does not exist

Definition at line 101 of file path.cpp.

101 {
102
103 // Try to find the last /
104 int last_slash = -1;
105 for (size_t i = 0; i < path.length(); i++)
106 if (path[i] == '/')
107 last_slash = i;
108
109 // Make sure there was a slash to split
110 if (last_slash == -1)
111 return path;
112
113 // Get the file path
114 string file_path = path.substring(0, last_slash);
115 return file_path;
116
117}
static string file_path(const string &path)
Finds the path to the directory the file is in.
Definition path.cpp:101

References file_path().

Referenced by MaxOS::filesystem::FileSystem::exists(), file_path(), and MaxOS::filesystem::VirtualFileSystem::open_directory().

◆ is_file()

bool Path::is_file ( const string path)
static

Check if a path is a file.

Parameters
pathThe path to check
Returns
True if the path is a file, false otherwise

Definition at line 42 of file path.cpp.

42 {
43
44 return file_name(path).length() > 0 && file_extension(path).length() > 0;
45}

References file_extension(), and file_name().

Referenced by absolute_path(), MaxOS::filesystem::VFSResourceRegistry::create_resource(), and MaxOS::filesystem::VFSResourceRegistry::get_resource().

◆ join_path()

string Path::join_path ( const string base,
const string extended 
)
static

Joins two paths together.

Parameters
baseThe base path
extendedWhat to add to the base path (if its from root, "/", then it will just return this)
Returns
The joint path

Definition at line 208 of file path.cpp.

208 {
209
210 // The new path is from root
211 if(extended[0] == '/')
212 return extended;
213
214 return base + extended;
215}

Referenced by MaxOS::filesystem::VFSResourceRegistry::create_resource(), and MaxOS::filesystem::VFSResourceRegistry::get_resource().

◆ parent_directory()

string Path::parent_directory ( const string path)
static

Get the parent directory of the path.

Parameters
pathThe path to either the file or the directory
Returns

Definition at line 148 of file path.cpp.

148 {
149
150 // Find the last /
151 int last_slash = -1;
152 for (size_t i = 0; i < path.length(); i++)
153 if (path[i] == '/')
154 last_slash = i;
155
156 // If no slash or only root, return empty string
157 if (last_slash <= 0)
158 return "/";
159
160 // Return substring up to last slash
161 return path.substring(0, last_slash);
162}

Referenced by MaxOS::filesystem::FileResource::write(), and MaxOS::filesystem::DirectoryResource::write().

◆ top_directory()

string Path::top_directory ( const string path)
static

Get the top directory of a path.

Parameters
pathThe path to get the top directory from
Returns
The top directory or the original path if it does not exist

Definition at line 125 of file path.cpp.

125 {
126
127 // Find the first /
128 int first_slash = -1;
129 for (size_t i = 0; i < path.length(); i++)
130 if (path[i] == '/')
131 first_slash = i;
132
133 // Make sure there was a slash to split
134 if (first_slash == -1)
135 return path;
136
137 // Get the top directory
138 string top_directory = path.substring(0, first_slash);
139 return top_directory;
140}
static string top_directory(const string &path)
Get the top directory of a path.
Definition path.cpp:125

References top_directory().

Referenced by MaxOS::filesystem::FileSystem::get_directory(), and top_directory().

◆ valid()

bool Path::valid ( const string path)
static

Check if a path is valid.

Parameters
pathThe path to check
Returns
True if the path is valid, false otherwise

Definition at line 21 of file path.cpp.

21 {
22
23 // Must not be empty
24 if (path.length() == 0)
25 return false;
26
27 // Must start with a /
28 if (path[0] != '/')
29 return false;
30
31 // Valid
32 return true;
33
34}

Referenced by MaxOS::filesystem::VirtualFileSystem::create_directory(), MaxOS::filesystem::VirtualFileSystem::create_file(), MaxOS::filesystem::VirtualFileSystem::delete_directory(), MaxOS::filesystem::VirtualFileSystem::delete_file(), MaxOS::filesystem::VirtualFileSystem::open_directory(), and MaxOS::filesystem::VirtualFileSystem::open_file().


The documentation for this class was generated from the following files: