-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.cpp
77 lines (61 loc) · 1.8 KB
/
Path.cpp
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
#pragma once
#include <WString.h>
#include "Path.h"
const String empty = "";
/// <summary>
/// Appends path2 to path1, ensuring a valid path format
/// </summary>
String Path::combinePaths(String path1, String path2) {
path1 = normalisePath(path1);
path2 = normalisePath(path2);
return path1 + path2;
}
/// <summary>
/// Appends a / to a folder path to ensure a format.
/// Removed prefixed / and appends / to folders
/// </summary>
/// <reutrns>A new normalised version of the given path</returns>
String Path::normalisePath(String path) {
String newpath = path; // Make a copy in case string is readonly
newpath.replace('\\', '/');
if (newpath.startsWith("/")) {
newpath = newpath.substring(1);
}
if (Path::hasFile(newpath))
return newpath;
if (!newpath.endsWith("/"))
newpath += '/';
return newpath;
}
String Path::makeAbsolute(String path) {
if (path.startsWith("/"))
return path;
return '/' + path;
}
/// <summary>
/// Checks whether path mentions a file
/// </summary>
/// <returns>True if a file exists in the given path</returns>
bool Path::hasFile(String path) {
unsigned int extensionindex = path.indexOf('.');
return (extensionindex != -1) && (extensionindex != path.length()-1);
}
/// <summary>
/// Extracts file name from a path
/// </summary>
/// <return>File and extension from given path or empoty string if no file exists</returns>
String Path::getFileName(String path) {
path = normalisePath(path);
if (!hasFile(path))
return empty;
return path.substring(path.lastIndexOf('\\') + 1);
}
/// <summary>
/// Extracts a file's extension
/// </summary>
/// <returns>The file's extension or an empty string if no file is found in string</return>
String Path::getFileExtension(String path) {
if (!Path::hasFile(path))
return empty;
return path.substring(path.lastIndexOf('.') + 1);
}