NarrowPath.hxx 1.83 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_FS_NARROW_PATH_HXX
#define MPD_FS_NARROW_PATH_HXX

#include "check.h"
#include "Path.hxx"
25 26 27
#include "util/Macros.hxx"

#ifdef _UNICODE
28 29
#include "lib/icu/Win32.hxx"
#include "util/AllocatedString.hxx"
30
#include <windows.h>
31 32
#else
#include "util/StringPointer.hxx"
33
#endif
34 35 36 37 38 39 40

/**
 * A path name that uses the regular (narrow) "char".  This is used to
 * pass a #Path (which may be represented by wchar_t) to a library
 * that accepts only "const char *".
 */
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 53 54 55 56
	explicit NarrowPath(Path _path)
		:value(WideCharToMultiByte(CP_ACP, _path.c_str())) {
		if (value.IsNull())
			/* fall back to empty string */
			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