MPD: fs/NarrowPath.hxx Source File

MPD 0.20.15

NarrowPath.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_NARROW_PATH_HXX
21 #define MPD_FS_NARROW_PATH_HXX
22
23 #include "check.h"
24 #include "Path.hxx"
25 #include "util/Macros.hxx"
26
27 #ifdef _UNICODE
28 #include "lib/icu/Win32.hxx"
29 #include "util/AllocatedString.hxx"
30 #include <windows.h>
31 #else
32 #include "util/StringPointer.hxx"
33 #endif
34
40 class NarrowPath {
41 #ifdef _UNICODE
42 typedef AllocatedString<> Value;
43 #else
44 typedef StringPointer<> Value;
45 #endif
46 typedef typename Value::const_pointer_type const_pointer_type;
47
48 Value value;
49
50 public:
51 #ifdef _UNICODE
52 explicit NarrowPath(Path _path)
53 :value(WideCharToMultiByte(CP_ACP, _path.c_str())) {
54 if (value.IsNull())
55 /* fall back to empty string */
56 value = Value::Empty();
57 }
58 #else
59 explicit NarrowPath(Path _path):value(_path.c_str()) {}
60 #endif
61
62 operator const_pointer_type() const {
63 return c_str();
64 }
65
66 const_pointer_type c_str() const {
67 return value.c_str();
68 }
69 };
70
71 #endif
check.h
StringPointer
Simple OO wrapper for a const string pointer.
Definition: StringPointer.hxx:37
WideCharToMultiByte
gcc_pure gcc_nonnull_all AllocatedString< char > WideCharToMultiByte(unsigned code_page, const wchar_t *src)
Throws std::system_error on error.
AllocatedString.hxx
Path::c_str
gcc_pure const_pointer_type c_str() const noexcept
Returns the value as a const C string.
Definition: Path.hxx:107
NarrowPath::NarrowPath
NarrowPath(Path _path)
Definition: NarrowPath.hxx:59
NarrowPath::c_str
const_pointer_type c_str() const
Definition: NarrowPath.hxx:66
Path
A path name in the native file system character set.
Definition: Path.hxx:39
Path.hxx
AllocatedString
A string pointer whose memory is managed by this class.
Definition: Util.hxx:29
StringPointer::const_pointer_type
const T * const_pointer_type
Definition: StringPointer.hxx:43
Macros.hxx
NarrowPath
A path name that uses the regular (narrow) "char".
Definition: NarrowPath.hxx:40
StringPointer.hxx
StringPointer::c_str
constexpr const_pointer_type c_str() const
Definition: StringPointer.hxx:63
Win32.hxx
StringPointer::IsNull
constexpr bool IsNull() const
Check if this is a "nulled" instance.
Definition: StringPointer.hxx:59