File: Path.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (121 lines) | stat: -rw-r--r-- 3,092 bytes parent folder | download | duplicates (2)
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
#pragma once

#include "Timestamp.h"

/**
 * A class for managing path names.
 */
class Path {
	friend std::wostream &operator <<(std::wostream &to, const Path &path);
public:
	// Get the path of the executable file we're currently running from.
	static Path executableFile();

	// Get the path of the executable file we're currently running from.
	static Path executable();

	// Get a path representing a file relative to the executable path.
	static Path executable(const Path &path);

	// Get a path to the root of the debug environment (only valid during debug).
	static Path dbgRoot();

	// Current working directory.
	static Path cwd();

	// Create an empty path.
	Path();

	// Create a path object from a string.
	explicit Path(const String &path);

	// Comparison.
	bool operator ==(const Path &o) const;
	inline bool operator !=(const Path &o) const { return !(*this == o); }
	bool operator <(const Path &o) const;
	bool operator >(const Path &o) const;

	// Concat this path with another path, the other path must be relative.
	Path operator +(const Path &other) const;
	Path &operator +=(const Path &other);

	// Add a string to go deeper into the hierarchy. If this object is not
	// a directory already, it will be made into one.
	Path operator +(const String &file) const;
	Path &operator +=(const String &file);

	// To string.
	String toS() const;

	// Status about this path.
	bool isDir() const;
	bool isAbsolute() const;
	bool isEmpty() const;

	// Make this obj a directory.
	inline void makeDir() { isDirectory = true; }

	// Get parent directory.
	Path parent() const;

	// Get the title of this file or directory.
	String title() const;
	String titleNoExt() const;

	// Has file extension? (ext shall not contain .)
	bool hasExt(const String &ext) const;

	// Get file extension (always the last one).
	String ext() const;

	// Does the file exist?
	bool exists() const;

	// Delete the file.
	void deleteFile() const;

	// Make this path relative to another path. Absolute-making is accomplished by
	// using the + operator above.
	Path makeRelative(const Path &to) const;

	// Make this path absolute (if it is not already).
	Path makeAbsolute(const Path &to) const;

	// Is this path a sub-path of "other"?
	bool isSubPath(const Path &other) const;

	// Modify this path to include the common path with 'other'.
	void common(const Path &with);

	// Find the children of this path.
	vector<Path> children() const;

	// Get a sorted version of 'children'.
	vector<Path> sortedChildren() const;

	// Modified time.
	Timestamp mTime() const;

	// Created time.
	Timestamp cTime() const;

	// Create this path as directory if it does not already exist.
	void createDir() const;

	// Output with unix-style separators.
	void outputUnix(std::wostream &to) const;

private:

	// Internal representation is a list of strings, one for each part of the pathname.
	vector<String> parts;

	// Is this a directory?
	bool isDirectory;

	// Parse a path string.
	void parseStr(const String &str);

	// Simplify a path string, which means to remove any . and ..
	void simplify();
};