MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

Traits.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_FS_TRAITS_HXX
21 #define MPD_FS_TRAITS_HXX
22
23 #include "check.h"
24 #include "Compiler.h"
25 #include "util/StringPointer.hxx"
26 #include "util/StringAPI.hxx"
27
28 #ifdef WIN32
29 #include "util/CharUtil.hxx"
30 #include <tchar.h>
31 #endif
32
33 #include <string>
34
35 #include <assert.h>
36
37 #ifdef WIN32
38 #define PATH_LITERAL(s) _T(s)
39 #else
40 #define PATH_LITERAL(s) (s)
41 #endif
42
46 struct PathTraitsFS {
47 #ifdef WIN32
48 typedef std::wstring string;
49 #else
50 typedef std::string string;
51 #endif
52 typedef string::traits_type char_traits;
53 typedef char_traits::char_type value_type;
54 typedef StringPointer<value_type> Pointer;
55 typedef Pointer::pointer_type pointer_type;
56 typedef Pointer::const_pointer_type const_pointer_type;
57
58 #ifdef WIN32
59 static constexpr value_type SEPARATOR = '\\';
60 #else
61 static constexpr value_type SEPARATOR = '/';
62 #endif
63
64 static constexpr const_pointer_type CURRENT_DIRECTORY = PATH_LITERAL(".");
65
66 static constexpr bool IsSeparator(value_type ch) {
67 return
68 #ifdef WIN32
69 ch == '/' ||
70 #endif
71 ch == SEPARATOR;
72 }
73
74 gcc_pure gcc_nonnull_all
75 static const_pointer_type FindLastSeparator(const_pointer_type p) {
76 #if !CLANG_CHECK_VERSION(3,6)
77 /* disabled on clang due to -Wtautological-pointer-compare */
78 assert(p != nullptr);
79 #endif
80
81 #ifdef WIN32
82 const_pointer_type pos = p + GetLength(p);
83 while (p != pos && !IsSeparator(*pos))
84 --pos;
85 return IsSeparator(*pos) ? pos : nullptr;
86 #else
87 return StringFindLast(p, SEPARATOR);
88 #endif
89 }
90
91 #ifdef WIN32
92 gcc_pure gcc_nonnull_all
93 static constexpr bool IsDrive(const_pointer_type p) {
94 return IsAlphaASCII(p[0]) && p[1] == ':';
95 }
96 #endif
97
98 gcc_pure gcc_nonnull_all
99 static bool IsAbsolute(const_pointer_type p) {
100 #if !CLANG_CHECK_VERSION(3,6)
101 /* disabled on clang due to -Wtautological-pointer-compare */
102 assert(p != nullptr);
103 #endif
104
105 #ifdef WIN32
106 if (IsDrive(p) && IsSeparator(p[2]))
107 return true;
108 #endif
109 return IsSeparator(*p);
110 }
111
112 gcc_pure gcc_nonnull_all
113 static size_t GetLength(const_pointer_type p) {
114 return StringLength(p);
115 }
116
117 gcc_pure gcc_nonnull_all
118 static const_pointer_type Find(const_pointer_type p, value_type ch) {
119 return StringFind(p, ch);
120 }
121
126 gcc_pure gcc_nonnull_all
127 static const_pointer_type GetBase(const_pointer_type p);
128
134 gcc_pure gcc_nonnull_all
135 static string GetParent(const_pointer_type p);
136
143 gcc_pure gcc_nonnull_all
144 static const_pointer_type Relative(const_pointer_type base,
145 const_pointer_type other);
146
153 gcc_pure gcc_nonnull_all
154 static string Build(const_pointer_type a, size_t a_size,
155 const_pointer_type b, size_t b_size);
156
157 gcc_pure gcc_nonnull_all
158 static string Build(const_pointer_type a, const_pointer_type b) {
159 return Build(a, GetLength(a), b, GetLength(b));
160 }
161 };
162
166 struct PathTraitsUTF8 {
167 typedef std::string string;
168 typedef string::traits_type char_traits;
169 typedef char_traits::char_type value_type;
170 typedef value_type *pointer_type;
171 typedef const value_type *const_pointer_type;
172
173 static constexpr value_type SEPARATOR = '/';
174
175 static constexpr const_pointer_type CURRENT_DIRECTORY = ".";
176
177 static constexpr bool IsSeparator(value_type ch) {
178 return ch == SEPARATOR;
179 }
180
181 gcc_pure gcc_nonnull_all
182 static const_pointer_type FindLastSeparator(const_pointer_type p) {
183 #if !CLANG_CHECK_VERSION(3,6)
184 /* disabled on clang due to -Wtautological-pointer-compare */
185 assert(p != nullptr);
186 #endif
187
188 return strrchr(p, SEPARATOR);
189 }
190
191 #ifdef WIN32
192 gcc_pure gcc_nonnull_all
193 static constexpr bool IsDrive(const_pointer_type p) {
194 return IsAlphaASCII(p[0]) && p[1] == ':';
195 }
196 #endif
197
198 gcc_pure gcc_nonnull_all
199 static bool IsAbsolute(const_pointer_type p) {
200 #if !CLANG_CHECK_VERSION(3,6)
201 /* disabled on clang due to -Wtautological-pointer-compare */
202 assert(p != nullptr);
203 #endif
204
205 #ifdef WIN32
206 if (IsDrive(p) && IsSeparator(p[2]))
207 return true;
208 #endif
209 return IsSeparator(*p);
210 }
211
212 gcc_pure gcc_nonnull_all
213 static size_t GetLength(const_pointer_type p) {
214 return StringLength(p);
215 }
216
217 gcc_pure gcc_nonnull_all
218 static const_pointer_type Find(const_pointer_type p, value_type ch) {
219 return StringFind(p, ch);
220 }
221
226 gcc_pure gcc_nonnull_all
227 static const_pointer_type GetBase(const_pointer_type p);
228
234 gcc_pure gcc_nonnull_all
235 static string GetParent(const_pointer_type p);
236
243 gcc_pure gcc_nonnull_all
244 static const_pointer_type Relative(const_pointer_type base,
245 const_pointer_type other);
246
253 gcc_pure gcc_nonnull_all
254 static string Build(const_pointer_type a, size_t a_size,
255 const_pointer_type b, size_t b_size);
256
257 gcc_pure gcc_nonnull_all
258 static string Build(const_pointer_type a, const_pointer_type b) {
259 return Build(a, GetLength(a), b, GetLength(b));
260 }
261 };
262
263 #endif
PathTraitsFS::Build
gcc_pure static gcc_nonnull_all string Build(const_pointer_type a, const_pointer_type b)
Definition: Traits.hxx:158
check.h
StringPointer
Simple OO wrapper for a const string pointer.
Definition: StringPointer.hxx:37
PathTraitsFS::IsSeparator
static constexpr bool IsSeparator(value_type ch)
Definition: Traits.hxx:66
gcc_nonnull_all
#define gcc_nonnull_all
Definition: Compiler.h:122
PathTraitsUTF8::GetLength
gcc_pure static gcc_nonnull_all size_t GetLength(const_pointer_type p)
Definition: Traits.hxx:213
PathTraitsUTF8::GetBase
gcc_pure static gcc_nonnull_all const_pointer_type GetBase(const_pointer_type p)
Determine the "base" file name of the given UTF-8 path.
PathTraitsUTF8
This class describes the nature of a MPD internal filesystem path.
Definition: Traits.hxx:166
PathTraitsFS::FindLastSeparator
gcc_pure static gcc_nonnull_all const_pointer_type FindLastSeparator(const_pointer_type p)
Definition: Traits.hxx:75
CharUtil.hxx
PathTraitsFS
This class describes the nature of a native filesystem path.
Definition: Traits.hxx:46
PathTraitsUTF8::char_traits
string::traits_type char_traits
Definition: Traits.hxx:168
IsAlphaASCII
static constexpr bool IsAlphaASCII(char ch)
Definition: CharUtil.hxx:104
PathTraitsFS::char_traits
string::traits_type char_traits
Definition: Traits.hxx:52
PathTraitsUTF8::CURRENT_DIRECTORY
static constexpr const_pointer_type CURRENT_DIRECTORY
Definition: Traits.hxx:175
PathTraitsUTF8::SEPARATOR
static constexpr value_type SEPARATOR
Definition: Traits.hxx:173
PathTraitsUTF8::Relative
gcc_pure static gcc_nonnull_all const_pointer_type Relative(const_pointer_type base, const_pointer_type other)
Determine the relative part of the given path to this object, not including the directory separator...
StringPointer::pointer_type
T * pointer_type
Definition: StringPointer.hxx:42
PathTraitsUTF8::pointer_type
value_type * pointer_type
Definition: Traits.hxx:170
StringFindLast
gcc_pure static gcc_nonnull_all const char * StringFindLast(const char *haystack, char needle)
Definition: StringAPI.hxx:85
PathTraitsFS::Relative
gcc_pure static gcc_nonnull_all const_pointer_type Relative(const_pointer_type base, const_pointer_type other)
Determine the relative part of the given path to this object, not including the directory separator...
PathTraitsFS::pointer_type
Pointer::pointer_type pointer_type
Definition: Traits.hxx:55
PathTraitsFS::GetBase
gcc_pure static gcc_nonnull_all const_pointer_type GetBase(const_pointer_type p)
Determine the "base" file name of the given native path.
PATH_LITERAL
#define PATH_LITERAL(s)
Definition: Traits.hxx:40
PathTraitsFS::const_pointer_type
Pointer::const_pointer_type const_pointer_type
Definition: Traits.hxx:56
PathTraitsUTF8::Find
gcc_pure static gcc_nonnull_all const_pointer_type Find(const_pointer_type p, value_type ch)
Definition: Traits.hxx:218
Compiler.h
StringAPI.hxx
PathTraitsUTF8::const_pointer_type
const value_type * const_pointer_type
Definition: Traits.hxx:171
PathTraitsFS::Find
gcc_pure static gcc_nonnull_all const_pointer_type Find(const_pointer_type p, value_type ch)
Definition: Traits.hxx:118
StringPointer::const_pointer_type
const T * const_pointer_type
Definition: StringPointer.hxx:43
PathTraitsUTF8::IsSeparator
static constexpr bool IsSeparator(value_type ch)
Definition: Traits.hxx:177
StringPointer.hxx
PathTraitsFS::CURRENT_DIRECTORY
static constexpr const_pointer_type CURRENT_DIRECTORY
Definition: Traits.hxx:64
PathTraitsFS::GetLength
gcc_pure static gcc_nonnull_all size_t GetLength(const_pointer_type p)
Definition: Traits.hxx:113
PathTraitsUTF8::Build
gcc_pure static gcc_nonnull_all string Build(const_pointer_type a, const_pointer_type b)
Definition: Traits.hxx:258
PathTraitsUTF8::Build
gcc_pure static gcc_nonnull_all string Build(const_pointer_type a, size_t a_size, const_pointer_type b, size_t b_size)
Constructs the path from the given components.
PathTraitsUTF8::GetParent
gcc_pure static gcc_nonnull_all string GetParent(const_pointer_type p)
Determine the "parent" file name of the given UTF-8 path.
PathTraitsFS::GetParent
gcc_pure static gcc_nonnull_all string GetParent(const_pointer_type p)
Determine the "parent" file name of the given native path.
PathTraitsFS::SEPARATOR
static constexpr value_type SEPARATOR
Definition: Traits.hxx:61
PathTraitsUTF8::FindLastSeparator
gcc_pure static gcc_nonnull_all const_pointer_type FindLastSeparator(const_pointer_type p)
Definition: Traits.hxx:182
PathTraitsFS::Pointer
StringPointer< value_type > Pointer
Definition: Traits.hxx:54
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
PathTraitsFS::value_type
char_traits::char_type value_type
Definition: Traits.hxx:53
StringFind
gcc_pure static gcc_nonnull_all const char * StringFind(const char *haystack, const char *needle)
Definition: StringAPI.hxx:50
PathTraitsFS::string
std::string string
Definition: Traits.hxx:50
PathTraitsFS::IsAbsolute
gcc_pure static gcc_nonnull_all bool IsAbsolute(const_pointer_type p)
Definition: Traits.hxx:99
PathTraitsUTF8::IsAbsolute
gcc_pure static gcc_nonnull_all bool IsAbsolute(const_pointer_type p)
Definition: Traits.hxx:199
StringLength
gcc_pure static gcc_nonnull_all size_t StringLength(const char *p)
Definition: StringAPI.hxx:43
PathTraitsUTF8::string
std::string string
Definition: Traits.hxx:167
PathTraitsUTF8::value_type
char_traits::char_type value_type
Definition: Traits.hxx:169
PathTraitsFS::Build
gcc_pure static gcc_nonnull_all string Build(const_pointer_type a, size_t a_size, const_pointer_type b, size_t b_size)
Constructs the path from the given components.