Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
PathViews.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/StringFwd.h"
6#include "CoreTypes.h"
7
8class FName;
9class FString;
10template <typename CharType> class TStringBuilderBase;
11template <typename FuncType> class TFunctionRef;
12
14{
15public:
16 /**
17 * Returns the portion of the path after the last separator.
18 *
19 * Examples: (Using '/' but '\' is valid too.)
20 * "A/B/C.D" -> "C.D"
21 * "A/B/C" -> "C"
22 * "A/B/" -> ""
23 * "A" -> "A"
24 *
25 * @return The portion of the path after the last separator.
26 */
27 static FStringView GetCleanFilename(const FStringView& InPath);
28
29 /**
30 * Returns the portion of the path after the last separator and before the last dot.
31 *
32 * Examples: (Using '/' but '\' is valid too.)
33 * "A/B/C.D" -> "C"
34 * "A/B/C" -> "C"
35 * "A/B/" -> ""
36 * "A" -> "A"
37 *
38 * @return The portion of the path after the last separator and before the last dot.
39 */
40 static FStringView GetBaseFilename(const FStringView& InPath);
41
42 /**
43 * Returns the portion of the path before the last dot.
44 *
45 * Examples: (Using '/' but '\' is valid too.)
46 * "A/B/C.D" -> "A/B/C"
47 * "A/B/C" -> "A/B/C"
48 * "A/B/" -> "A/B/"
49 * "A" -> "A"
50 *
51 * @return The portion of the path before the last dot.
52 */
53 static FStringView GetBaseFilenameWithPath(const FStringView& InPath);
54
55 /** Returns the portion of the path before the last dot and optionally after the last separator. */
56 UE_DEPRECATED(4.25, "FPathViews::GetBaseFilename(InPath, bRemovePath) has been superseded by "
57 "FPathViews::GetBaseFilename(InPath) and FPathViews::GetBaseFilenameWithPath(InPath).")
58 static FStringView GetBaseFilename(const FStringView& InPath, bool bRemovePath);
59
60 /**
61 * Returns the portion of the path before the last separator.
62 *
63 * Examples: (Using '/' but '\' is valid too.)
64 * "A/B/C.D" -> "A/B"
65 * "A/B/C" -> "A/B"
66 * "A/B/" -> "A/B"
67 * "A" -> ""
68 *
69 * @return The portion of the path before the last separator.
70 */
71 static FStringView GetPath(const FStringView& InPath);
72
73 /**
74 * Returns the portion of the path after the last dot following the last separator, optionally including the dot.
75 *
76 * Examples: (Using '/' but '\' is valid too.)
77 * "A/B.C.D" -> "D" (bIncludeDot=false) or ".D" (bIncludeDot=true)
78 * "A/B/C.D" -> "D" (bIncludeDot=false) or ".D" (bIncludeDot=true)
79 * "A/B/.D" -> "D" (bIncludeDot=false) or ".D" (bIncludeDot=true)
80 * ".D" -> "D" (bIncludeDot=false) or ".D" (bIncludeDot=true)
81 * "A/B/C" -> ""
82 * "A.B/C" -> ""
83 * "A.B/" -> ""
84 * "A" -> ""
85 *
86 * @param bIncludeDot Whether to include the leading dot in the returned view.
87 *
88 * @return The portion of the path after the last dot following the last separator, optionally including the dot.
89 */
90 static FStringView GetExtension(const FStringView& InPath, bool bIncludeDot=false);
91
92 /**
93 * Returns the last non-empty path component.
94 *
95 * Examples: (Using '/' but '\' is valid too.)
96 * "A/B/C.D" -> "C.D"
97 * "A/B/C" -> "C"
98 * "A/B/" -> "B"
99 * "A" -> "A"
100 *
101 * @return The last non-empty path component.
102 */
103 static FStringView GetPathLeaf(const FStringView& InPath);
104
105 /**
106 * Return whether the given relative or absolute path is a leaf path - has no separators.
107 *
108 * Examples: (Using '/' but '\' functions the same way)
109 * A -> true
110 * A/ -> true
111 * D:/ -> true
112 * / -> true
113 * // -> true
114 * A/B -> false
115 * D:/A -> false
116 * //A -> false
117 */
118 static bool IsPathLeaf(FStringView InPath);
119
120 /**
121 * Splits InPath into individual directory components, and calls ComponentVisitor on each.
122 *
123 * Examples:
124 * "A/B.C" -> {"A", "B.C"}
125 * "A/B/C" -> {"A", "B", "C"}
126 * "../../A/B/C.D" -> {"..", "..", "A", "B", "C.D" }
127 */
128 static void IterateComponents(FStringView InPath, TFunctionRef<void(FStringView)> ComponentVisitor);
129
130 /**
131 * Splits a path into three parts, any of which may be empty: the path, the clean name, and the extension.
132 *
133 * Examples: (Using '/' but '\' is valid too.)
134 * "A/B/C.D" -> ("A/B", "C", "D")
135 * "A/B/C" -> ("A/B", "C", "")
136 * "A/B/" -> ("A/B", "", "")
137 * "A/B/.D" -> ("A/B", "", "D")
138 * "A/B.C.D" -> ("A", "B.C", "D")
139 * "A" -> ("", "A", "")
140 * "A.D" -> ("", "A", "D")
141 * ".D" -> ("", "", "D")
142 *
143 * @param OutPath [out] Receives the path portion of the input string, excluding the trailing separator.
144 * @param OutName [out] Receives the name portion of the input string.
145 * @param OutExt [out] Receives the extension portion of the input string, excluding the dot.
146 */
147 static void Split(const FStringView& InPath, FStringView& OutPath, FStringView& OutName, FStringView& OutExt);
148
149 /**
150 * Appends each suffix argument to the path in the builder and ensures that there is a separator between them.
151 *
152 * Examples:
153 * ("", "") -> ""
154 * ("A", "") -> "A/"
155 * ("", "B") -> "B"
156 * ("/", "B") -> "/B"
157 * ("A", "B") -> "A/B"
158 * ("A/", "B") -> "A/B"
159 * ("A\\", "B") -> "A\\B"
160 * ("A/B", "C/D") -> "A/B/C/D"
161 * ("A/", "B", "C/", "D") -> "A/B/C/D"
162 *
163 * @param Builder A possibly-empty path that may end in a separator.
164 * @param Args Arguments that can write to a string builder and do not start with a separator.
165 */
166 template <typename CharType, typename... ArgTypes>
167 static void Append(TStringBuilderBase<CharType>& Builder, ArgTypes&&... Args)
168 {
169 const auto AddSeparator = [&Builder]() -> TStringBuilderBase<CharType>&
170 {
171 if (!(Builder.Len() == 0 || Builder.LastChar() == '/' || Builder.LastChar() == '\\'))
172 {
173 Builder.AppendChar('/');
174 }
175 return Builder;
176 };
177 ((AddSeparator() << Args), ...);
178 }
179
180 /**
181 * Replaces the pre-existing file extension of a filename.
182 *
183 * @param InPath A valid file path with a pre-existing extension.
184 * @param InNewExtension The new extension to use (prefixing with a '.' is optional)
185 *
186 * @return The new file path complete with the new extension unless InPath is not valid in which
187 * case a copy of InPath will be returned instead.
188 */
189 static FString ChangeExtension(const FStringView& InPath, const FStringView& InNewExtension);
190
191 /** Return whether the given character is a path-separator character (/ or \‍) */
192 static bool IsSeparator(TCHAR c);
193
194 /**
195 * Return true if the given paths are the same path (with exceptions noted below).
196 * Case-insensitive
197 * / is treated as equal to \
198 * Presence or absence of terminating separator (/) is ignored in the comparison.
199 * Directory elements of . and .. are currently not interpreted and are treated as literal characters.
200 * Callers should not rely on this behavior as it may be corrected in the future.
201 * callers should instead conform the paths before calling.
202 * Relative paths and absolute paths are not resolved, and relative paths will never equal absolute paths.
203 * Callers should not rely on this behavior as it may be corrected in the future;
204 * callers should instead conform the paths before calling.
205 * Examples:
206 * ("../A/B.C", "../A/B.C") -> true
207 * ("../A/B", "../A/B.C") -> false
208 * ("../A/", "../A/") -> true
209 * ("../A/", "../A") -> true
210 * ("d:/root/Engine/", "d:\root\Engine") -> true
211 * (../../../Engine/Content", "d:/root/Engine/Content") -> false
212 * (d:/root/Engine/..", "d:/root") -> false
213 * (d:/root/Engine/./Content", "d:/root/Engine/Content") -> false
214 */
215 static bool Equals(FStringView A, FStringView B);
216
217 /**
218 * Return true if the the first path is lexicographically less than the second path (with caveats noted below).
219 * Case-insensitive
220 * / is treated as equal to \
221 * Presence or absence of terminating separator (/) is ignored in the comparison.
222 * Directory elements of . and .. are currently not interpreted and are treated as literal characters.
223 * Callers should not rely on this behavior as it may be corrected in the future.
224 * callers should instead conform the paths before calling.
225 * Relative paths and absolute paths are not resolved, and relative paths will never equal absolute paths.
226 * Callers should not rely on this behavior as it may be corrected in the future;
227 * callers should instead conform the paths before calling.
228 * Examples:
229 * ("../A/B.C", "../A/B.C") -> false (they are equal)
230 * ("../A/B", "../A/B.C") -> true (a string is greater than any prefix of itself)
231 * ("../A/", "../A/") -> false (they are equal)
232 * ("../A/", "../A") -> false (they are equal)
233 * ("../A", "../A/") -> false (they are equal)
234 * ("d:/root/Engine/", "d:\root\Engine") -> false (they are equal)
235 * (../../../Engine/Content", "d:/root/Engine/Content") -> true ('.' is less than 'd')
236 * (d:/root/Engine/..", "d:/root") -> false (A string is greater than any prefix of itself)
237 * (d:/root/Engine/./Content", "d:/root/Engine/Content") -> false
238 */
239 static bool Less(FStringView A, FStringView B);
240
241 /**
242 * Check whether Parent is a parent path of Child and report the relative path if so.
243 * Case-insensitive
244 * / is treated as equal to \
245 * Presence or absence of terminating separator (/) is ignored in the comparison.
246 * Directory elements of . and .. are currently not interpreted and are treated as literal characters.
247 * Callers should not rely on this behavior as it may be corrected in the future.
248 * callers should instead conform the paths before calling.
249 * Relative paths and absolute paths are not resolved, and relative paths will never equal absolute paths.
250 * Callers should not rely on this behavior as it may be corrected in the future;
251 * callers should instead conform the paths before calling.
252 * Examples:
253 * ("../A/B", "../A") -> (true, "B")
254 * ("../A\B", "../A/") -> (true, "B")
255 * ("../A/", "../A") -> (true, "")
256 * (".././A/", "../A") -> (false, "")
257 * ("../../../Engine", "d:/root/Engine") -> (false, "")
258 *
259 * @param Child An absolute path that may be a child path of Parent.
260 * @param Parent An absolute path that may be a parent path of Child.
261 * @param OutRelPath Receives the relative path from Parent to Child, or empty if Parent is not a parent of Child.
262 *
263 * @return True if and only if Child is a child path of Parent (or is equal to it).
264 */
265 static bool TryMakeChildPathRelativeTo(FStringView Child, FStringView Parent, FStringView& OutRelPath);
266
267 /**
268 * Return whether Parent is a parent path of (or is equal to) Child.
269 * Case-insensitive
270 * / is treated as equal to \
271 * Presence or absence of terminating separator (/) is ignored in the comparison.
272 * Directory elements of . and .. are currently not interpreted and are treated as literal characters.
273 * Callers should not rely on this behavior as it may be corrected in the future.
274 * callers should instead conform the paths before calling.
275 * Relative paths and absolute paths are not resolved, and relative paths will never equal absolute paths.
276 * Callers should not rely on this behavior as it may be corrected in the future;
277 * callers should instead conform the paths before calling.
278 * Examples:
279 * ("../A", "../A/B") -> true
280 * ("../A/", "../A\B") -> true
281 * ("../A", "../A/") -> true
282 * ("../A", ".././A/") -> false
283 * ("d:/root/Engine", "../../../Engine") -> false
284 *
285 * @param Parent An absolute path that may be a parent path of Child.
286 * @param Child An absolute path that may be a child path of Parent.
287 *
288 * @return True if and only if Child is a child path of Parent (or is equal to it).
289 */
290 static bool IsParentPathOf(FStringView Parent, FStringView Child);
291
292 /**
293 * Return whether the given path is a relativepath - does not start with a separator or volume:.
294 * Returns true for empty paths.
295 */
296 static bool IsRelativePath(FStringView InPath);
297
298 /** Convert to absolute using process BaseDir(), normalize and append. FPaths::ConvertRelativePathToFull() equivalent. */
299 static void ToAbsolutePath(FStringView InPath, FStringBuilderBase& OutPath);
300
301 /** Convert to absolute using explicit BasePath, normalize and append. FPaths::ConvertRelativePathToFull() equivalent. */
302 static void ToAbsolutePath(FStringView BasePath, FStringView InPath, FStringBuilderBase& OutPath);
303
304 /** Convert to absolute using process BaseDir() and normalize inlined. FPaths::ConvertRelativePathToFull() equivalent. */
305 static void ToAbsolutePathInline(FStringBuilderBase& InOutPath);
306
307 /** Convert to absolute using explicit BasePath and normalize inlined. FPaths::ConvertRelativePathToFull() equivalent. */
308 static void ToAbsolutePathInline(FStringView BasePath, FStringBuilderBase& InOutPath);
309
310 /** Convert \\ to / and do platform-specific normalization */
311 static void NormalizeFilename(FStringBuilderBase& InOutPath);
312
313 /** Normalize and remove trailing slash unless the preceding character is '/' or ':' */
314 static void NormalizeDirectoryName(FStringBuilderBase& InOutPath);
315
316 /** Collapses redundant paths like "/./" and "SkipDir/..". FPaths::CollapseRelativeDirectories() equivalent. */
317 static bool CollapseRelativeDirectories(FStringBuilderBase& InOutPath);
318
319 /** Removes duplicate forward slashes, e.g. "a/b//c////f.e" -> "a/b/c/f.e" */
320 static void RemoveDuplicateSlashes(FStringBuilderBase& InOutPath);
321
322 /**
323 * Split the given absolute or relative path into its topmost directory and the relative path from that directory.
324 * Directory elements of . and .. are currently not interpreted and are treated as literal characters.
325 * Callers should not rely on this behavior as it may be corrected in the future.
326 * callers should instead conform the paths before calling.
327 *
328 * @param InPath The path to split.
329 * @param OutFirstComponent Receives the first directory element in the path, or InPath if it is a leaf path.
330 * @param OutRemainder Receives the relative path from OutFirstComponent to InPath, or empty if InPath is a leaf path.
331 */
332 static void SplitFirstComponent(FStringView InPath, FStringView& OutFirstComponent, FStringView& OutRemainder);
333
334 /**
335 * If AppendPath is a relative path, append it as a relative path onto InOutPath.
336 * If AppendPath is absolute, reset InOutPath and replace it with RelPath.
337 * Handles presence or absence of terminating separator in BasePath.
338 * Does not interpret . or ..; each occurrence of these in either path will remain in the combined InOutPath.
339 */
340 static void AppendPath(FStringBuilderBase& InOutPath, FStringView AppendPath);
341
342 /**
343 * Returns the name of the mount point in a path
344 * Removes starting forward slash and Classes_ prefix if bInWithoutSlashes is true
345 * Example: "/Classes_A/Textures" returns "A" and sets bOutHadClassesPrefix=true if bInWithoutSlashes is true
346 * returns "/Classes_A" otherwise and sets bOutHadClassesPrefix=false
347 */
348 static FStringView GetMountPointNameFromPath(const FStringView InPath, bool* bOutHadClassesPrefix = nullptr, bool bInWithoutSlashes = true);
349};
#define UE_DEPRECATED(Version, Message)
static void AppendPath(FStringBuilderBase &InOutPath, FStringView AppendPath)
static void ToAbsolutePathInline(FStringBuilderBase &InOutPath)
static FStringView GetPath(const FStringView &InPath)
static FStringView GetExtension(const FStringView &InPath, bool bIncludeDot=false)
static bool IsRelativePath(FStringView InPath)
static bool Less(FStringView A, FStringView B)
static void IterateComponents(FStringView InPath, TFunctionRef< void(FStringView)> ComponentVisitor)
static bool TryMakeChildPathRelativeTo(FStringView Child, FStringView Parent, FStringView &OutRelPath)
static FString ChangeExtension(const FStringView &InPath, const FStringView &InNewExtension)
static bool IsSeparator(TCHAR c)
static bool IsParentPathOf(FStringView Parent, FStringView Child)
static FStringView GetPathLeaf(const FStringView &InPath)
static void ToAbsolutePath(FStringView BasePath, FStringView InPath, FStringBuilderBase &OutPath)
static void Split(const FStringView &InPath, FStringView &OutPath, FStringView &OutName, FStringView &OutExt)
static void ToAbsolutePathInline(FStringView BasePath, FStringBuilderBase &InOutPath)
static void NormalizeDirectoryName(FStringBuilderBase &InOutPath)
static FStringView GetCleanFilename(const FStringView &InPath)
static void RemoveDuplicateSlashes(FStringBuilderBase &InOutPath)
static FStringView GetBaseFilenameWithPath(const FStringView &InPath)
static bool IsPathLeaf(FStringView InPath)
static FStringView GetBaseFilename(const FStringView &InPath)
static void ToAbsolutePath(FStringView InPath, FStringBuilderBase &OutPath)
static FStringView GetMountPointNameFromPath(const FStringView InPath, bool *bOutHadClassesPrefix=nullptr, bool bInWithoutSlashes=true)
static void Append(TStringBuilderBase< CharType > &Builder, ArgTypes &&... Args)
Definition PathViews.h:167
bool bRemovePath
Definition PathViews.h:58
static bool CollapseRelativeDirectories(FStringBuilderBase &InOutPath)
static void SplitFirstComponent(FStringView InPath, FStringView &OutFirstComponent, FStringView &OutRemainder)
static void NormalizeFilename(FStringBuilderBase &InOutPath)
static bool Equals(FStringView A, FStringView B)