fs/NarrowPath: add class FromNarrowPath

Move code from ParseCommandLine().
This commit is contained in:
Max Kellermann
2020-04-02 16:18:06 +02:00
parent 61d7b436a2
commit 4d453a8313
3 changed files with 56 additions and 12 deletions
+19
View File
@@ -22,6 +22,8 @@
#ifdef _UNICODE
#include "lib/icu/Win32.hxx"
#include "system/Error.hxx"
#include "util/Macros.hxx"
#include <windows.h>
@@ -33,4 +35,21 @@ NarrowPath::NarrowPath(Path _path) noexcept
value = Value::Empty();
}
static AllocatedPath
AcpToAllocatedPath(const char *s)
{
wchar_t buffer[MAX_PATH];
auto result = MultiByteToWideChar(CP_ACP, 0, s, -1,
buffer, ARRAY_SIZE(buffer));
if (result <= 0)
throw MakeLastError("MultiByteToWideChar() failed");
return AllocatedPath::FromFS(buffer);
}
FromNarrowPath::FromNarrowPath(const char *s)
:value(AcpToAllocatedPath(s))
{
}
#endif /* _UNICODE */
+35
View File
@@ -23,6 +23,7 @@
#include "Path.hxx"
#ifdef _UNICODE
#include "AllocatedPath.hxx"
#include "util/AllocatedString.hxx"
#else
#include "util/StringPointer.hxx"
@@ -59,4 +60,38 @@ public:
}
};
/**
* A path name converted from a "narrow" string. This is used to
* import an existing narrow string to a #Path.
*/
class FromNarrowPath {
#ifdef _UNICODE
using Value = AllocatedPath;
#else
using Value = Path;
#endif
Value value{nullptr};
public:
FromNarrowPath() = default;
#ifdef _UNICODE
/**
* Throws on error.
*/
FromNarrowPath(const char *s);
#else
constexpr FromNarrowPath(const char *s) noexcept
:value(Value::FromFS(s)) {}
#endif
#ifndef _UNICODE
constexpr
#endif
operator Path() const noexcept {
return value;
}
};
#endif