summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bx/include/bx/filepath.h
blob: 464799e2166fbaf2742fc7f3299decc8bad6472a (plain) (blame)
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
/*
 * Copyright 2010-2022 Branimir Karadzic. All rights reserved.
 * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
 */

#ifndef BX_FILEPATH_H_HEADER_GUARD
#define BX_FILEPATH_H_HEADER_GUARD

#include "error.h"
#include "string.h"

namespace bx
{
	BX_ERROR_RESULT(kErrorAccess,       BX_MAKEFOURCC('b', 'x', 1, 1) );
	BX_ERROR_RESULT(kErrorNotDirectory, BX_MAKEFOURCC('b', 'x', 1, 2) );

	constexpr int32_t kMaxFilePath = 1024;

	/// Special predefined OS directories.
	///
	struct Dir
	{
		/// Special OS directories:
		enum Enum
		{
			Current, //!< Current directory.
			Temp,    //!< Temporary directory.
			Home,    //!< User's home directory.

			Count
		};
	};

	/// FilePath parser and helper.
	///
	/// /abv/gd/555/333/pod.mac
	/// ppppppppppppppppbbbeeee
	/// ^               ^  ^
	/// +-path     base-+  +-ext
	///                 ^^^^^^^
	///                 +-filename
	///
	class FilePath
	{
	public:
		/// Default constructor, creates empty file path.
		///
		FilePath();

		/// Construct file path from special OS directory.
		///
		FilePath(Dir::Enum _dir);

		/// Construct file path from C string.
		///
		FilePath(const char* _str);

		/// Construct file path from string.
		///
		FilePath(const StringView& _str);

		/// Assign file path from string.
		///
		FilePath& operator=(const StringView& _rhs);

		/// Clear file path.
		///
		void clear();

		/// Set file path from special OS directory.
		///
		void set(Dir::Enum _dir);

		/// Set file path.
		///
		void set(const StringView& _str);

		/// Join directory to file path.
		///
		void join(const StringView& _str);

		/// Implicitly converts FilePath to StringView.
		///
		operator StringView() const;

		/// Returns zero-terminated C string pointer to file path.
		///
		const char* getCPtr() const;

		/// If path is `/abv/gd/555/333/pod.mac` returns `/abv/gd/555/333/`.
		///
		StringView getPath() const;

		/// If path is `/abv/gd/555/333/pod.mac` returns `pod.mac`.
		///
		StringView getFileName() const;

		/// If path is `/abv/gd/555/333/pod.mac` returns `pod`.
		///
		StringView getBaseName() const;

		/// If path is `/abv/gd/555/333/pod.mac` returns `.mac`.
		///
		StringView getExt() const;

		/// Returns true if file path is absolute.
		///
		bool isAbsolute() const;

		/// Returns true if file path is empty.
		///
		bool isEmpty() const;

	private:
		char m_filePath[kMaxFilePath];
	};

} // namespace bx

#endif // BX_FILEPATH_H_HEADER_GUARD