#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library 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 IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_STORAGE_SOURCE 1 #include "PathHelper.h" #if defined(NUCLEX_STORAGE_WIN32) #include "../../Helpers/Utf8/checked.h" #include // for std::vector namespace Nuclex { namespace Storage { namespace FileSystem { namespace Windows { // ------------------------------------------------------------------------------------------- // /// Converts a UTF-8 path into a UTF-16 path /// String containing a UTF-8 path /// The UTF-16 path with magic prefix to eliminate the path length limit std::wstring PathHelper::PrefixedUtf16FromUtf8Path(const std::string &utf8Path) { if(utf8Path.empty()) { return std::wstring(); } // We guess that we need as many UTF-16 characters as we needed UTF-8 characters // based on the assumption that most text will only use ascii characters. std::wstring utf16Path; utf16Path.reserve(utf8Path.length() + 4); // According to Microsoft, this is how you lift the 260 char MAX_PATH limit. // Also skips the internal call to GetFullPathName() every API method does internally, // so paths have to be normalized and const wchar_t prefix[] = L"\\\\?\\"; utf16Path.push_back(prefix[0]); utf16Path.push_back(prefix[1]); utf16Path.push_back(prefix[2]); utf16Path.push_back(prefix[3]); // Do the conversions. If the vector was too short, it will be grown in factors // of 2 usually (depending on the standard library implementation) utf8::utf8to16(utf8Path.begin(), utf8Path.end(), std::back_inserter(utf16Path)); return utf16Path; } // ------------------------------------------------------------------------------------------- // }}}} // namespace Nuclex::Storage::FileSystem::Windows #endif // defined(NUCLEX_STORAGE_WIN32)