-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathstr.h
More file actions
238 lines (203 loc) · 7.95 KB
/
Copy pathpathstr.h
File metadata and controls
238 lines (203 loc) · 7.95 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
******************************************************************
* A small library for handling filesystem paths as strings (Qt). *
* *
* artemvlas (at) proton (dot) me *
* https://github.com/artemvlas/pathstr *
******************************************************************
*
* MIT License
*
* Copyright (c) 2021 - present Artem Vlasenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef PATHSTR_H
#define PATHSTR_H
#include <QString>
namespace pathstr {
/*** Constants ***/
static const QChar s_sep = u'/';
static const QChar s_sepWin = '\\';
static const QChar s_dot = u'.';
/* Joins two path strings with '/' separator, checking that the path separator is not duplicated:
* joinPath("/absolutePath", "addPath") -> "/absolutePath/addPath"
* joinPath("/absolutePath/", "addPath") -> "/absolutePath/addPath"
* joinPath("/absolutePath/", "/addPath") -> "/absolutePath/addPath"
* joinPath("C:\\folder\\", "\\folder2") -> "C:\\folder\\folder2"
*/
QString joinPath(const QString &absolutePath, const QString &addPath);
/* Returns the name of the file system entry (file or folder)
* regardless of the separator presence at the end of the path:
* "/folder/fooEntry/" -> "fooEntry"
* "/folder/fooEntry" -> "fooEntry"
* "/folder/archive.zip" -> "archive.zip"
* "/" -> "Root"
* "C:/" -> "Drive C"
*/
QString entryName(const QString &path);
/* Returns the file name without path and complete suffix.
* "/folder/archive.tar.gz" -> "archive"
* "/folder/archive.zip" -> "archive"
* "file.name.with.dots.tar.gz" -> "file.name.with.dots"
*/
QString baseName(const QString &fileName);
/* Returns the path to the parent folder,
* regardless of the separator presence at the end of the path:
* "/folder/file_or_folder2/" -> "/folder"
* "/folder/file_or_folder2" -> "/folder"
* "C:/" -> "C:/"
* "/" -> "/"
* "folder/" -> ""
*/
QString parentFolder(const QString &path);
// relativePath("/rootFolder", "/rootFolder/folder2/file") -> "folder2/file"
QString relativePath(const QString &rootFolder, const QString &fullPath);
/* Changes the <oldName> filename, keeping the complete suffix and path unchanged.
* <oldName> is the file name or path.
* <newName> is the new file name.
*
* "fooFile.txt" -> "newName.txt"
* "/folder/archive.tar.gz" -> "/folder/newName.tar.gz"
*/
QString renameFile(const QString &oldName, const QString &newName);
/* Appends the <end> string to the file name,
* preserving the extension and path to the parent folder.
*
* "fooFile.txt", "-01" -> "fooFile-01.txt"
* "fooFile.txt", "-01.TXT" -> "fooFile-01.txt"
* "/folder/archive.tar.gz", "-01" -> "/folder/archive-01.tar.gz"
*/
QString appendFileName(const QString &fileName, const QString &end);
/* Prepends the <prefix> string to the file name, preserving the parent folder path.
*
* "fooFile.txt", "01-" -> "01-fooFile.txt"
* "/folder/archive.tar.gz", "01-" -> "/folder/01-archive.tar.gz"
*/
QString prependFileName(const QString &fileName, const QString &prefix);
// parentFolder/baseName.ext
QString composeFilePath(const QString &parentFolder,
const QString &baseName, const QString &ext);
/* Returns the root of an absolute path.
* "/home/folder" -> "/"
* "C:/folder" -> "C:/"
*/
QString root(const QString &path);
/* Returns the suffix (extension) of the <fileName>.
* "file.txt" -> "txt"
* "archive.tar.gz" -> "gz"
* ".hidden_file" -> ""
*/
QString suffix(const QString &fileName);
/* Returns the complete suffix (extension) of the <fileName>.
* The last two dots at most are taken into account.
*
* "/folder/archive.tar.gz" -> "tar.gz"
* "file.name.with.dots.tar.gz" -> "tar.gz"
* "/folder/archive.zip" -> "zip"
* "folder.name/.archive.zip" -> "zip"
*/
QString completeSuffix(const QString &fileName);
/* Sets or changes an existing filename suffix
* setSuffix("file", "zip") -> "file.zip"
* setSuffix("file.txt", "zip") -> "file.zip"
*/
QString setSuffix(const QString &fileName, const QString &suf);
/* Returns the size of the suffix.
* If the file name begins with a dot (Linux hidden file), it matters.
* "/folder/file.txt" -> 3
* ".hidden_file" -> 0
*/
int suffixSize(const QString &fileName);
/* Returns the size of the complete suffix (extension).
* The last two dots at most are taken into account.
*
* "/folder/archive.tar.gz" -> 6
* "file.name.with.dots.tar.gz" -> 6
* "/folder/archive.zip" -> 3
*/
int completeSuffixSize(const QString &fileName);
/* True if the <fileName> string has the <ext> suffix
* hasExtension("file.txt", "txt") -> true
* hasExtension("file.txt", ".TXT") -> true
* hasExtension("file.txt", "cpp") -> false
* hasExtension("folder/archive.tar.gz", ".tar.GZ") -> true
*/
bool hasExtension(const QString &fileName, const QString &ext);
/* True if the <fileName> has any extension from the list.
* hasExtension("file.txt", {"jpg", "txt", "json"}) -> true
* hasExtension("file.txt", {"zip", "cpp", "epub"}) -> false
*/
bool hasExtension(const QString &fileName, const QStringList &extensions);
// true if starts with "X:"
bool hasWindowsRoot(const QString &path);
// true: "/" or "X:[/]"; else false
bool isRoot(const QString &path);
// true if the <path> starts with '/' or 'X:'
bool isAbsolute(const QString &path);
// true if the path is not Absolute
bool isRelative(const QString &path);
// true if '/' or '\\'
inline bool isSeparator(const QChar sep)
{
return (sep == s_sep) || (sep == s_sepWin);
}
// true if the <path> string ends with a slash or backslash (path separator '/' or '\\')
inline bool endsWithSep(const QString &path)
{
return !path.isEmpty() && isSeparator(path.back());
}
// ...starts with
inline bool startsWithSep(const QString &path)
{
return !path.isEmpty() && isSeparator(path.front());
}
// Appends '/' if not any
inline QString appendSep(const QString &path)
{
return path.endsWith(s_sep) ? path : path + s_sep;
}
// If the <path> ends with a separator, remove it
inline QString chopSep(const QString &path)
{
return endsWithSep(path) ? path.chopped(1) : path;
}
/* win ('\\') -> posix ('/') path separator
* C:\Windows\System -> C:/Windows/System
*/
inline QString& toPosixPath(QString &path)
{
return path.replace(s_sepWin, s_sep);
}
// const value
inline QString toPosixPath(const QString &path)
{
QString pathCopy = path;
return toPosixPath(pathCopy);
}
/*** Additional tools ***/
/* Join strings with the specified separator ('sep'),
* checks for the absence of 'sep' duplication:
*
* joinStrings("string1", "string2", '/') -> "string1/string2"
* joinStrings("string1/", "/string2", '/') -> "string1/string2"
*/
QString joinStrings(const QString &str1, const QString &str2, QChar sep);
} // namespace pathstr
#endif // PATHSTR_H