File: path.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (581 lines) | stat: -rw-r--r-- 16,383 bytes parent folder | download
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef COMMON_PATH_H
#define COMMON_PATH_H

#include "common/scummsys.h"
#include "common/str.h"
#include "common/str-array.h"

#ifdef CXXTEST_RUNNING
class PathTestSuite;
#endif

namespace Common {

/**
 * @defgroup common_path Path
 * @ingroup common
 *
 * @brief API for working with paths.
 *
 * @{
 */

/**
 * Simple path class. Allows simple conversion to/from path strings with
 * arbitrary directory separators, providing a common representation.
 *
 * Internally, this is just a simple wrapper around a String, using
 * "/" as a directory separator.
 * It escapes it using "|" if / is used inside a path component.
 */
class Path {
#ifdef CXXTEST_RUNNING
	friend class ::PathTestSuite;
#endif

private:
#ifndef RELEASE_BUILD
	static bool _shownSeparatorCollisionWarning;
#endif

	// The directory separator
	static const char SEPARATOR = '/';
	// The escape prefix character
	static const char ESCAPE = '|';
	// How the prefix character is escaped (doubling)
	static const char ESCAPED_ESCAPE = '|';
	// How the separator is escaped
	static const char ESCAPED_SEPARATOR  = '\\';

	String _str;

	/**
	 * Escapes a path:
	 * - all ESCAPE are encoded to ESCAPE ESCAPED_ESCAPE
	 * - all SEPARATOR are encoded to ESCAPE ESCAPED_SEPARATOR if srcSeparator is not SEPARATOR
	 * - replaces all srcSeparator to SEPARATOR
	 *
	 * @param dst The String object to append result to.
	 * @param srcSeparator The directory separator used in source string.
	 * @param begin Begin of range
	 * @param end End of the range excluded.
	 */
	static void escape(String &dst, char srcSeparator, const char *src, const char *end = nullptr);

	/**
	 * Invert of escape
	 * - all ESCAPE ESCAPED_ESCAPE sequences are restored to ESCAPE
	 * - all ESCAPE ESCAPED_SEPARATOR sequences are restored to SEPARATOR
	 * - all SEPARATOR are replaced by dstSeparator
	 * Collisions in resulting path are checked and warned once.
	 *
	 * @param dstSeparator The directory separator to use in resulting string.
	 * @param begin Start of the range.
	 * @param end End of the range excluded.
	 */
	static String unescape(char dstSeparator, const char *src, const char *end = nullptr);

	/**
	 * Checks if the provided range can be unescaped
	 *
	 * @param atBegin Whether this range will end up at the begin of path or not.
	 * @param begin Start of the range.
	 * @param end End of the range excluded.
	 *
	 */
	static bool canUnescape(bool willBeAtBegin, bool wasAtBegin,
	                        const char *begin, const char *end = nullptr);

	/**
	 * Determines if the provided string will need to be encode and can't be copied as is
	 *
	 * @param str The string to check
	 * @param separator The directory separator used in str
	 */
	static inline bool needsEncoding(const char *str, char separator) {
		return str && *str && // String is not empty and
		       (separator != SEPARATOR || // separator is not the one we use
		        *str == ESCAPE); // or string begins with ESCAPE
	}

	/**
	 * Encodes a path:
	 * - escapes when needed
	 * - all separators are changed to SEPARATOR
	 * - a ESCAPE is prepended to string if escaping took place
	 * - if separator is \x00 str is assumed to be only one component
	 * - using ESCAPE as separator is not supported
	 *
	 * @param str The string to encode
	 * @param separator The directory separator used in str
	 */
	static String encode(const char *str, char separator);

	/**
	 * Creates a path using [begin, end) path data from ourselves.
	 * begin and end are assumed to lie inside _str
	 *
	 * @param begin Start of the range.
	 * @param end End of the range excluded.
	 *
	 */
	Path extract(const char *begin, const char *end = nullptr) const;

	/** Finds the last separator before @p last. */
	size_t findLastSeparator(size_t last = String::npos) const;

	/**
	 * Apply a reduction function on every path components
	 * and returns the reducer result
	 *
	 * @param reducer The function which reduces the path components
	 * @param value The initial value passed to reducer
	 */
	template<typename T>
	T reduceComponents(T (*reducer)(T value, const String &element, bool last), T value) const;

	/**
	 * Compares a path component by component by calling a comparator function
	 * The function bails out as soon as the comparison fails.
	 *
	 * @param comparator The function which compares the path components
	 * @param other The other path to compare with
	 */
	bool compareComponents(bool (*comparator)(const String &x, const String &y), const Path &other) const;

	/**
	 * Determines if the path is escaped
	 */
	inline bool isEscaped() const {
		// c_str() returns a null terminated string, if _str is empty we get \0
		return *_str.c_str() == ESCAPE;
	}

	/**
	 * Returns the suffix in this path after @p other path
	 * Returns nullptr if @p other isn't a prefix
	 */
	const char *getSuffix(const Common::Path &other) const;

public:
	/**
	 * A separator to use when building path conataining only base names
	 * This must not be used with toString(), use baseName() instead.
	 */
	static const char kNoSeparator = '\x00';

	/**
	 * The platform native separator.
	 * This is used when accessing files on disk
	 */
#if defined(WIN32)
	static const char kNativeSeparator = '\\';
#else
	static const char kNativeSeparator = '/';
#endif

	/**
	 * Hash and comparator for Path with following changes:
	 * * case-insensitive
	 * * decoding of punycode
	 * * Matching ':' and '/' inside path components to
	 * This allows a path "Sound Manager 3.1 / SoundLib<separator>Sound"
	 * to match both "xn--Sound Manager 3.1  SoundLib-lba84k/Sound"
	 * and "Sound Manager 3.1 : SoundLib/Sound"
	 */
	struct IgnoreCaseAndMac_EqualTo {
		bool operator()(const Path &x, const Path &y) const { return x.equalsIgnoreCaseAndMac(y); }
	};

	struct IgnoreCaseAndMac_Hash {
		uint operator()(const Path &x) const { return x.hashIgnoreCaseAndMac(); }
	};

	/**
	 * Hash and comparator for Path with case-insensitivity
	 * This may not be used when punycoded and Mac files could be found
	 * but this should be safe in other places and costs less
	 */
	struct IgnoreCase_EqualTo {
		bool operator()(const Path &x, const Path &y) const { return x.equalsIgnoreCase(y); }
	};

	struct IgnoreCase_Hash {
		uint operator()(const Path &x) const { return x.hashIgnoreCase(); }
	};

	struct EqualTo {
		bool operator()(const Path &x, const Path &y) const { return x.equals(y); }
	};

	struct Hash {
		uint operator()(const Path &x) const { return x.hash(); }
	};

	/** Construct a new empty path. */
	Path() {}

	/** Construct a copy of the given path. */
	Path(const Path &path) : _str(path._str) { }

	/**
	 * Construct a new path from the given NULL-terminated C string.
	 *
	 * @param str       A NULL-terminated C string representing a path,
	 *                  e.g. "foo/bar/baz"
	 * @param separator The directory separator used in the path string.
	 *                  Use kNoSeparator if there is no separator.
	 *                  Defaults to '/'.
	 */
	Path(const char *str, char separator = '/') :
		_str(needsEncoding(str, separator) ? encode(str, separator) : str) { }

	/**
	 * Construct a new path from the given String.
	 * This is explicit to limit mixing strings and paths in the codebase.
	 *
	 * @param str       A String representing a path, e.g. "foo/bar/baz"
	 * @param separator The directory separator used in the path string.
	 *                  Use kNoSeparator if there is no separator.
	 *                  Defaults to '/'.
	 */
	explicit Path(const String &str, char separator = '/') :
		_str(needsEncoding(str.c_str(), separator) ? encode(str.c_str(), separator) : str) { }

	/**
	 * Converts a path to a string using the given directory separator.
	 * Collisions in resulting path are checked and warned once.
	 *
	 * @param separator The character used to separate directory names.
	 *                  kNoSeparator must not be used here.
	 *                  Defaults to '/'.
	 */
	String toString(char separator = '/') const;

	/**
	 * Clears the path object
	 */
	void clear() { _str.clear(); }

	/**
	 * Returns the Path for the parent directory of this path.
	 *
	 * Appending the getLastComponent() of a path to getParent() returns a path
	 * identical to the original path.
	 */
	Path getParent() const;

	/**
	 * Returns the last component of this path.
	 *
	 * Appending the getLastComponent() of a path to getParent() returns a path
	 * identical to the original path.
	 */
	Path getLastComponent() const;

	/**
	 * Returns the last non-empty component of this path.
	 * Compared to getLastComponent(), baseName() doesn't
	 * return the trailing / if any.
	 */
	String baseName() const;

	/**
	 * Returns number of components in this path,
	 */
	int numComponents() const;

	/** Check whether this path is identical to path @p x. */
	bool operator==(const Path &x) const {
		return _str == x._str;
	}

	/** Check whether this path is different than path @p x. */
	bool operator!=(const Path &x) const {
		return _str != x._str;
	}

	/**
	 * Check whether this path is identical to path @p x.
	 */
	bool equals(const Path &x) const {
		return _str.equals(x._str);
	}
	/**
	 * Check whether this path is identical to path @p x.
	 * Ignores case
	 */
	bool equalsIgnoreCase(const Path &x) const;
	/**
	 * Check whether this path is identical to path @p x.
	 * Ignores case, punycode and Mac path separator.
	 */
	bool equalsIgnoreCaseAndMac(const Path &x) const;

	/**
	 * Calculate a case sensitive hash of path
	 */
	uint hash() const;
	/**
	 * Calculate a case insensitive hash of path
	 */
	uint hashIgnoreCase() const;
	/**
	 * Calculate a hash of path which is case insensitive.
	 * Ignores case, punycode and Mac path separator.
	 */
	uint hashIgnoreCaseAndMac() const;

	bool operator<(const Path &x) const;

	/** Return if this path is empty */
	bool empty() const {
		return _str.empty();
	}

	/** Assign a given path to this path. */
	Path &operator=(const Path &path) {
		_str = path._str;
		return *this;
	}

	/** @overload */
	Path &operator=(const char *str) {
		set(str);
		return *this;
	}

	/** @overload */
	Path &operator=(const String &str) {
		set(str.c_str());
		return *this;
	}

	void set(const char *str, char separator = '/') {
		if (needsEncoding(str, separator)) {
			_str = encode(str, separator);
		} else {
			_str = str;
		}
	}

	/**
	 * Appends the given path to this path (in-place).
	 * Does not automatically add a directory separator.
	 */
	Path &appendInPlace(const Path &x);

	/** @overload */
	Path &appendInPlace(const String &str, char separator = '/') {
		appendInPlace(str.c_str(), separator);
		return *this;
	}

	/** @overload */
	Path &appendInPlace(const char *str, char separator = '/');

	/**
	 * Returns this path with the given path appended (out-of-place).
	 * Does not automatically add a directory separator.
	 */
	WARN_UNUSED_RESULT Path append(const Path &x) const {
		Path temp(*this);
		temp.appendInPlace(x);
		return temp;
	}

	/** @overload */
	WARN_UNUSED_RESULT Path append(const String &str, char separator = '/') const {
		return append(str.c_str(), separator);
	}

	/** @overload */
	WARN_UNUSED_RESULT Path append(const char *str, char separator = '/') const {
		Path temp(*this);
		temp.appendInPlace(str, separator);
		return temp;
	}

	/**
	 * Appends exactly one component, without any separators
	 * and prepends a separator if necessarry
	 */
	WARN_UNUSED_RESULT Path appendComponent(const char *str) const;

	/** @overload */
	WARN_UNUSED_RESULT Path appendComponent(const String &x) const {
		return appendComponent(x.c_str());
	}

	/**
	 * Joins the given path to this path (in-place).
	 * Automatically adds a directory separator.
	 */
	Path &joinInPlace(const Path &x);

	/** @overload */
	Path &joinInPlace(const String &str, char separator = '/') {
		return joinInPlace(str.c_str(), separator);
	}

	/** @overload */
	Path &joinInPlace(const char *str, char separator = '/');

	/**
	 * Returns this path joined with the given path (out-of-place).
	 * Automatically adds a directory separator.
	 */
	WARN_UNUSED_RESULT Path join(const Path &x) const {
		Path temp(*this);
		temp.joinInPlace(x);
		return temp;
	}

	/** @overload */
	WARN_UNUSED_RESULT Path join(const String &str, char separator = '/') const {
		return join(str.c_str(), separator);
	}

	/** @overload */
	WARN_UNUSED_RESULT Path join(const char *str, char separator = '/') const {
		Path temp(*this);
		temp.joinInPlace(str, separator);
		return temp;
	}

	/**
	 * Removes the trainling separators if any in this path (in-place).
	 */
	Path &removeTrailingSeparators();

	/**
	 * Returns whether this path ends with a separator
	 */
	bool isSeparatorTerminated() const { return _str.lastChar() == SEPARATOR; }

	/**
	 * Returns whether this path begins with @p other path
	 */
	bool isRelativeTo(const Common::Path &other) const { return getSuffix(other) != nullptr; }

	/**
	 * Returns a new path relative to the @p other one.
	 * Returns a copy this if it wasn't relative to.
	 */
	Path relativeTo(const Common::Path &other) const;

	/**
	 * Convert path from Punycode
	 */
	Path punycodeDecode() const;

	/**
	* Convert path to Punycode
	 */
	Path punycodeEncode() const;

	/**
	 * Returns whether the path will need to be Punycoded
	 */
	bool punycodeNeedsEncode() const;

	/**
	 * Convert all characters in the path to lowercase.
	 *
	 * Be aware that this only affects the case of ASCII characters. All
	 * other characters will not be touched at all.
	 */
	void toLowercase() {
		// Escapism is not changed by changing case
		_str.toLowercase();
	}

	/**
	 * Convert all characters in the path to uppercase.
	 *
	 * Be aware that this only affects the case of ASCII characters. All
	 * other characters will not be touched at all.
	 */
	void toUppercase() {
		// Escapism is not changed by changing case
		_str.toUppercase();
	}

	/**
	* Check pattern match similar matchString
	 */
	bool matchPattern(const Path &pattern) const;

	/**
	 * Normalize path to a canonical form. In particular:
	 * - trailing separators are removed:  /foo/bar/ -> /foo/bar
	 * - double separators (= empty components) are removed:   /foo//bar -> /foo/bar
	 * - dot components are removed:  /foo/./bar -> /foo/bar
	 * - double dot components are removed:  /foo/baz/../bar -> /foo/bar
	 *
	 * @return      the normalized path
	 */
	Path normalize() const;

	/**
	 * Splits into path components. After every component except
	 * last there is an implied separator. First component is empty
	 * if path starts with a separator. Last component is empty if
	 * the path ends with a separator. Other components may be empty if
	 * 2 separots follow each other
	 */
	StringArray splitComponents() const;

	/**
	 * Opposite of splitComponents
	 */
	static Path joinComponents(StringArray::const_iterator begin, StringArray::const_iterator end);
	static Path joinComponents(const StringArray &c) {
		return joinComponents(c.begin(), c.end());
	}

	/**
	 * Use by ConfigManager to store a path in a protected fashion
	 * All components are punyencoded and / is used as a delimiter for all platforms
	 * Under Windows don't encode when it's not needed and make use of \ separator
	 * in this case
	 */
	String toConfig() const;

	/**
	 * Used by ConfigManager to parse a configuration value in a backwards compatible way
	 */
	static Path fromConfig(const String &value);

	/**
	 * Creates a path from a string given by the user
	 */
	static Path fromCommandLine(const String &value);
};

/** @} */

} // End of namespace Common

#endif