Max OS 0.3
Loading...
Searching...
No Matches
path.cpp
Go to the documentation of this file.
1
9#include <filesystem/path.h>
10
11using namespace MaxOS;
12using namespace MaxOS::common;
13using namespace MaxOS::filesystem;
14
21bool Path::valid(const string& path) {
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}
35
42bool Path::is_file(const string& path) {
43
44 return file_name(path).length() > 0 && file_extension(path).length() > 0;
45}
46
47
54string Path::file_name(const string& path) {
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}
70
77string Path::file_extension(const string& path) {
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}
94
101string Path::file_path(const string& path) {
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}
118
125string Path::top_directory(const string& path) {
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}
141
148string Path::parent_directory(const string& path) {
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}
163
170string Path::absolute_path(const string& path) {
171
172 // Split the path into components
173 auto components = path.split("/");
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}
200
208string Path::join_path(const string& base, const string& extended) {
209
210 // The new path is from root
211 if(extended[0] == '/')
212 return extended;
213
214 return base + extended;
215}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
static string file_extension(const string &path)
Try to get the file extension from a path (assumes split by ".")
Definition path.cpp:77
static string parent_directory(const string &path)
Get the parent directory of the path.
Definition path.cpp:148
static bool valid(const string &path)
Check if a path is valid.
Definition path.cpp:21
static bool is_file(const string &path)
Check if a path is a file.
Definition path.cpp:42
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
static string top_directory(const string &path)
Get the top directory of a path.
Definition path.cpp:125
static string file_path(const string &path)
Finds the path to the directory the file is in.
Definition path.cpp:101
static string join_path(const string &base, const string &extended)
Joins two paths together.
Definition path.cpp:208
static string absolute_path(const string &path)
Calculates path with out any "../" or "./".
Definition path.cpp:170
Provides utilities for handling file and directory paths.