7 #include <winapi/path.hpp>
8 #include <winapi/utf8.hpp>
21 std::wstring do_canonicalize(
const std::wstring& path) {
22 static constexpr std::size_t init_buffer_size = MAX_PATH;
23 static_assert(init_buffer_size > 0,
"init_buffer_size must be positive");
25 std::vector<wchar_t> buffer;
26 buffer.resize(init_buffer_size);
29 if (buffer.size() > std::numeric_limits<DWORD>::max())
30 throw std::range_error{
"Path buffer is too large"};
31 const auto nch = ::GetFullPathNameW(
32 path.c_str(),
static_cast<DWORD
>(buffer.size()), buffer.data(), NULL);
35 throw error::windows(GetLastError(),
"GetFullPathNameW");
38 if (nch < buffer.size()) {
39 return {buffer.data(), nch};
42 if (nch > buffer.size()) {
43 buffer.resize(2 * buffer.size());
50 CanonicalPath::CanonicalPath(
const std::string& path) : m_path(canonicalize(path)) {}
52 std::string CanonicalPath::canonicalize(
const std::string& path) {
53 return narrow(do_canonicalize(widen(path)));
Make std::system_error work with GetLastError().