c2e9ceeb6b
* Switch to `std::unique_ptr<wchar_t[]>` to represent a dynamically strings instead of wrappers like `StringRAII` and `Utf8ToWideScope`; * Avoid back and forth conversion between UTF8 and UTF16: convert to UTF16 first then work on that. This also simplifies code - previously it tried to work with strings allocated in different ways uniformly, which is actually unnecessary if resulting string needs to be converted to UTF16 (and allocated with `malloc`) anyway; * Fix a bug in `File::CreateLink`: it was handling relative links with long paths incorrectly. Change file_long_path_test to cover this case and make it run on non-Windows systems as well to ensure that all behavior that can match actually matches. TEST=ci Change-Id: I1279aff1d2cdace5e2ce8633c2f7ea69a34fe41a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/356680 Commit-Queue: Slava Egorov <vegorov@google.com> Reviewed-by: Alexander Aprelev <aam@google.com>
85 lines
2.8 KiB
C++
85 lines
2.8 KiB
C++
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#ifndef RUNTIME_BIN_UTILS_WIN_H_
|
|
#define RUNTIME_BIN_UTILS_WIN_H_
|
|
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
#include "platform/utils.h"
|
|
|
|
#include "platform/allocation.h"
|
|
|
|
#define MAX_LONG_PATH 32767
|
|
|
|
namespace dart {
|
|
namespace bin {
|
|
|
|
void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length);
|
|
|
|
// Convert from milliseconds since the Unix epoch to a FILETIME.
|
|
FILETIME GetFiletimeFromMillis(int64_t millis);
|
|
|
|
// These string utility functions return strings that have been allocated with
|
|
// Dart_ScopeAllocate(). They should be used only when we are inside an API
|
|
// scope. If a string returned by one of these functions must persist beyond
|
|
// the scope, then copy the results into a suitable buffer that you have
|
|
// allocated.
|
|
class StringUtilsWin {
|
|
public:
|
|
static char* WideToUtf8(wchar_t* wide,
|
|
intptr_t len = -1,
|
|
intptr_t* result_len = nullptr);
|
|
static const char* WideToUtf8(const wchar_t* wide,
|
|
intptr_t len = -1,
|
|
intptr_t* result_len = nullptr);
|
|
static wchar_t* Utf8ToWide(char* utf8,
|
|
intptr_t len = -1,
|
|
intptr_t* result_len = nullptr);
|
|
static const wchar_t* Utf8ToWide(const char* utf8,
|
|
intptr_t len = -1,
|
|
intptr_t* result_len = nullptr);
|
|
|
|
private:
|
|
DISALLOW_ALLOCATION();
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(StringUtilsWin);
|
|
};
|
|
|
|
// These scopes provide strings converted as indicated by the scope names.
|
|
// The provided strings are allocated with 'malloc' and have the same lifetime
|
|
// as the scope.
|
|
class WideToUtf8Scope {
|
|
public:
|
|
explicit WideToUtf8Scope(const wchar_t* wide)
|
|
: utf8_(Utils::CreateCStringUniquePtr(nullptr)) {
|
|
intptr_t utf8_len =
|
|
WideCharToMultiByte(CP_UTF8, 0, wide, -1, nullptr, 0, nullptr, nullptr);
|
|
char* utf8 = reinterpret_cast<char*>(malloc(utf8_len));
|
|
WideCharToMultiByte(CP_UTF8, 0, wide, -1, utf8, utf8_len, nullptr, nullptr);
|
|
length_ = utf8_len;
|
|
utf8_ = Utils::CreateCStringUniquePtr(utf8);
|
|
}
|
|
|
|
char* utf8() const { return utf8_.get(); }
|
|
intptr_t length() const { return length_; }
|
|
|
|
// Release the ownership of the converted string and return it.
|
|
Utils::CStringUniquePtr release() { return std::move(utf8_); }
|
|
|
|
private:
|
|
intptr_t length_;
|
|
Utils::CStringUniquePtr utf8_;
|
|
|
|
DISALLOW_ALLOCATION();
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(WideToUtf8Scope);
|
|
};
|
|
|
|
std::unique_ptr<wchar_t[]> Utf8ToWideChar(const char* path);
|
|
|
|
} // namespace bin
|
|
} // namespace dart
|
|
|
|
#endif // RUNTIME_BIN_UTILS_WIN_H_
|