fs/NarrowPath: use the WideCharToMultiByte() wrapper

This commit is contained in:
Max Kellermann 2015-06-23 13:09:36 +02:00
parent aecfcaa8a2
commit d551d40886

View File

@ -25,7 +25,11 @@
#include "util/Macros.hxx" #include "util/Macros.hxx"
#ifdef _UNICODE #ifdef _UNICODE
#include "lib/icu/Win32.hxx"
#include "util/AllocatedString.hxx"
#include <windows.h> #include <windows.h>
#else
#include "util/StringPointer.hxx"
#endif #endif
/** /**
@ -34,35 +38,33 @@
* that accepts only "const char *". * that accepts only "const char *".
*/ */
class NarrowPath { class NarrowPath {
typedef char value_type;
typedef const char *const_pointer;
#ifdef _UNICODE #ifdef _UNICODE
char value[PATH_MAX]; typedef AllocatedString<> Value;
#else #else
const_pointer value; typedef StringPointer<> Value;
#endif #endif
typedef typename Value::const_pointer const_pointer;
Value value;
public: public:
#ifdef _UNICODE #ifdef _UNICODE
explicit NarrowPath(Path _path) { explicit NarrowPath(Path _path)
auto result = WideCharToMultiByte(CP_ACP, 0, :value(WideCharToMultiByte(CP_ACP, _path.c_str())) {
_path.c_str(), -1, if (value.IsNull())
value, ARRAY_SIZE(value), /* fall back to empty string */
nullptr, nullptr); value = Value::Empty();
if (result < 0)
value[0] = 0;
} }
#else #else
explicit NarrowPath(Path _path):value(_path.c_str()) {} explicit NarrowPath(Path _path):value(_path.c_str()) {}
#endif #endif
operator const_pointer() const { operator const_pointer() const {
return value; return c_str();
} }
const_pointer c_str() const { const_pointer c_str() const {
return value; return value.c_str();
} }
}; };