NarrowPath.hxx 1.76 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
 * 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 "Path.hxx"
24 25

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

/**
 * 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 {
39
#ifdef _UNICODE
40
	typedef AllocatedString<> Value;
41
#else
42
	typedef StringPointer<> Value;
43
#endif
44
	typedef typename Value::const_pointer const_pointer;
45 46

	Value value;
47 48

public:
49
#ifdef _UNICODE
50 51 52 53 54
	explicit NarrowPath(Path _path)
		:value(WideCharToMultiByte(CP_ACP, _path.c_str())) {
		if (value.IsNull())
			/* fall back to empty string */
			value = Value::Empty();
55 56
	}
#else
57
	explicit NarrowPath(Path _path):value(_path.c_str()) {}
58
#endif
59

60
	operator const_pointer() const {
61
		return c_str();
62 63
	}

64
	const_pointer c_str() const {
65
		return value.c_str();
66 67 68 69
	}
};

#endif