MPD  0.20.6
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;
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 
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
93  static constexpr bool IsDrive(const_pointer_type p) {
94  return IsAlphaASCII(p[0]) && p[1] == ':';
95  }
96 #endif
97 
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 
113  static size_t GetLength(const_pointer_type p) {
114  return StringLength(p);
115  }
116 
118  static const_pointer_type Find(const_pointer_type p, value_type ch) {
119  return StringFind(p, ch);
120  }
121 
127  static const_pointer_type GetBase(const_pointer_type p);
128 
135  static string GetParent(const_pointer_type p);
136 
144  static const_pointer_type Relative(const_pointer_type base,
145  const_pointer_type other);
146 
154  static string Build(const_pointer_type a, size_t a_size,
155  const_pointer_type b, size_t b_size);
156 
158  static string Build(const_pointer_type a, const_pointer_type b) {
159  return Build(a, GetLength(a), b, GetLength(b));
160  }
161 };
162 
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 
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
193  static constexpr bool IsDrive(const_pointer_type p) {
194  return IsAlphaASCII(p[0]) && p[1] == ':';
195  }
196 #endif
197 
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 
213  static size_t GetLength(const_pointer_type p) {
214  return StringLength(p);
215  }
216 
218  static const_pointer_type Find(const_pointer_type p, value_type ch) {
219  return StringFind(p, ch);
220  }
221 
227  static const_pointer_type GetBase(const_pointer_type p);
228 
235  static string GetParent(const_pointer_type p);
236 
244  static const_pointer_type Relative(const_pointer_type base,
245  const_pointer_type other);
246 
254  static string Build(const_pointer_type a, size_t a_size,
255  const_pointer_type b, size_t b_size);
256 
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
gcc_pure static gcc_nonnull_all string Build(const_pointer_type a, const_pointer_type b)
Definition: Traits.hxx:158
Simple OO wrapper for a const string pointer.
static constexpr bool IsSeparator(value_type ch)
Definition: Traits.hxx:66
#define gcc_nonnull_all
Definition: Compiler.h:122
gcc_pure static gcc_nonnull_all size_t GetLength(const_pointer_type p)
Definition: Traits.hxx:213
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.
This class describes the nature of a MPD internal filesystem path.
Definition: Traits.hxx:166
gcc_pure static gcc_nonnull_all const_pointer_type FindLastSeparator(const_pointer_type p)
Definition: Traits.hxx:75
This class describes the nature of a native filesystem path.
Definition: Traits.hxx:46
string::traits_type char_traits
Definition: Traits.hxx:168
static constexpr bool IsAlphaASCII(char ch)
Definition: CharUtil.hxx:104
string::traits_type char_traits
Definition: Traits.hxx:52
static constexpr const_pointer_type CURRENT_DIRECTORY
Definition: Traits.hxx:175
static constexpr value_type SEPARATOR
Definition: Traits.hxx:173
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...
value_type * pointer_type
Definition: Traits.hxx:170
gcc_pure static gcc_nonnull_all const char * StringFindLast(const char *haystack, char needle)
Definition: StringAPI.hxx:85
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...
Pointer::pointer_type pointer_type
Definition: Traits.hxx:55
gcc_pure static gcc_nonnull_all const_pointer_type GetBase(const_pointer_type p)
Determine the "base" file name of the given native path.
#define PATH_LITERAL(s)
Definition: Traits.hxx:40
Pointer::const_pointer_type const_pointer_type
Definition: Traits.hxx:56
gcc_pure static gcc_nonnull_all const_pointer_type Find(const_pointer_type p, value_type ch)
Definition: Traits.hxx:218
const value_type * const_pointer_type
Definition: Traits.hxx:171
gcc_pure static gcc_nonnull_all const_pointer_type Find(const_pointer_type p, value_type ch)
Definition: Traits.hxx:118
const T * const_pointer_type
static constexpr bool IsSeparator(value_type ch)
Definition: Traits.hxx:177
static constexpr const_pointer_type CURRENT_DIRECTORY
Definition: Traits.hxx:64
gcc_pure static gcc_nonnull_all size_t GetLength(const_pointer_type p)
Definition: Traits.hxx:113
gcc_pure static gcc_nonnull_all string Build(const_pointer_type a, const_pointer_type b)
Definition: Traits.hxx:258
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.
gcc_pure static gcc_nonnull_all string GetParent(const_pointer_type p)
Determine the "parent" file name of the given UTF-8 path.
gcc_pure static gcc_nonnull_all string GetParent(const_pointer_type p)
Determine the "parent" file name of the given native path.
static constexpr value_type SEPARATOR
Definition: Traits.hxx:61
gcc_pure static gcc_nonnull_all const_pointer_type FindLastSeparator(const_pointer_type p)
Definition: Traits.hxx:182
StringPointer< value_type > Pointer
Definition: Traits.hxx:54
#define gcc_pure
Definition: Compiler.h:116
char_traits::char_type value_type
Definition: Traits.hxx:53
gcc_pure static gcc_nonnull_all const char * StringFind(const char *haystack, const char *needle)
Definition: StringAPI.hxx:50
std::string string
Definition: Traits.hxx:50
gcc_pure static gcc_nonnull_all bool IsAbsolute(const_pointer_type p)
Definition: Traits.hxx:99
gcc_pure static gcc_nonnull_all bool IsAbsolute(const_pointer_type p)
Definition: Traits.hxx:199
gcc_pure static gcc_nonnull_all size_t StringLength(const char *p)
Definition: StringAPI.hxx:43
std::string string
Definition: Traits.hxx:167
char_traits::char_type value_type
Definition: Traits.hxx:169
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.