1 /*
2  * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
3  * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
4  */
5 
6 #ifndef BX_FILEPATH_H_HEADER_GUARD
7 #define BX_FILEPATH_H_HEADER_GUARD
8 
9 #include "error.h"
10 #include "string.h"
11 
12 BX_ERROR_RESULT(BX_ERROR_ACCESS,        BX_MAKEFOURCC('b', 'x', 0, 0) );
13 BX_ERROR_RESULT(BX_ERROR_NOT_DIRECTORY, BX_MAKEFOURCC('b', 'x', 0, 1) );
14 
15 namespace bx
16 {
17 	constexpr int32_t kMaxFilePath = 1024;
18 
19 	/// Special predefined OS directories.
20 	///
21 	struct Dir
22 	{
23 		/// Special OS directories:
24 		enum Enum
25 		{
26 			Current, //!< Current directory.
27 			Temp,    //!< Temporary directory.
28 			Home,    //!< User's home directory.
29 
30 			Count
31 		};
32 	};
33 
34 	/// FilePath parser and helper.
35 	///
36 	/// /abv/gd/555/333/pod.mac
37 	/// ppppppppppppppppbbbeeee
38 	/// ^               ^  ^
39 	/// +-path     base-+  +-ext
40 	///                 ^^^^^^^
41 	///                 +-filename
42 	///
43 	class FilePath
44 	{
45 	public:
46 		/// Default constructor, creates empty file path.
47 		///
48 		FilePath();
49 
50 		/// Construct file path from special OS directory.
51 		///
52 		FilePath(Dir::Enum _dir);
53 
54 		/// Construct file path from C string.
55 		///
56 		FilePath(const char* _str);
57 
58 		/// Construct file path from string.
59 		///
60 		FilePath(const StringView& _str);
61 
62 		/// Assign file path from string.
63 		///
64 		FilePath& operator=(const StringView& _rhs);
65 
66 		/// Clear file path.
67 		///
68 		void clear();
69 
70 		/// Set file path from special OS directory.
71 		///
72 		void set(Dir::Enum _dir);
73 
74 		/// Set file path.
75 		///
76 		void set(const StringView& _str);
77 
78 		/// Join directory to file path.
79 		///
80 		void join(const StringView& _str);
81 
82 		/// Implicitly converts FilePath to StringView.
83 		///
84 		operator StringView() const;
85 
86 		/// Returns zero-terminated C string pointer to file path.
87 		///
88 		const char* getCPtr() const;
89 
90 		/// If path is `/abv/gd/555/333/pod.mac` returns `/abv/gd/555/333/`.
91 		///
92 		StringView getPath() const;
93 
94 		/// If path is `/abv/gd/555/333/pod.mac` returns `pod.mac`.
95 		///
96 		StringView getFileName() const;
97 
98 		/// If path is `/abv/gd/555/333/pod.mac` returns `pod`.
99 		///
100 		StringView getBaseName() const;
101 
102 		/// If path is `/abv/gd/555/333/pod.mac` returns `.mac`.
103 		///
104 		StringView getExt() const;
105 
106 		/// Returns true if file path is absolute.
107 		///
108 		bool isAbsolute() const;
109 
110 		/// Returns true if file path is empty.
111 		///
112 		bool isEmpty() const;
113 
114 	private:
115 		char m_filePath[kMaxFilePath];
116 	};
117 
118 } // namespace bx
119 
120 #endif // BX_FILEPATH_H_HEADER_GUARD
121