Files
sdk/runtime/bin/utils_win.h
T
Zichang Guo f8fbefd951 Revert "Enable long path on Windows"
This reverts commit 08662f0575.

Reason for revert: failure on pkg-win-release, https://dart-ci.appspot.com/log/pkg-win-release/unittest-asserts-release-win/12878/pkg/dartdev/test/commands/test_test

Original change's description:
> Enable long path on Windows
> 
> File APIs on Windows can now handle files and directories identified by
> long paths (greater than 260 characters). For directory, the limit is
> 248.
> 
> Some restrictions from Windows:
> 1. The size limit for long path is 32,767 characters.
> 2. Each component separated by backslashes should not be more than 255
> characters.
> 
> Reference: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation.
> 
> Note that `Directory.current` does not work with long path.
> 
> Bug: https://github.com/dart-lang/sdk/issues/42416
> Change-Id: Ia1b4608d393fb36f1d843858c6f076f3c825dc83
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/152736
> Reviewed-by: Alexander Aprelev <aam@google.com>
> Commit-Queue: Zichang Guo <zichangguo@google.com>

TBR=aam@google.com,zra@google.com,zichangguo@google.com

Change-Id: If6701c887e616cd62f20faac43f601cfb53ed349
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: https://github.com/dart-lang/sdk/issues/42416
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/162104
Reviewed-by: Zichang Guo <zichangguo@google.com>
Commit-Queue: Zichang Guo <zichangguo@google.com>
2020-09-08 22:34:50 +00:00

100 lines
3.2 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 <utility>
#include "platform/utils.h"
namespace dart {
namespace bin {
void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length);
// 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 = NULL);
static const char* WideToUtf8(const wchar_t* wide,
intptr_t len = -1,
intptr_t* result_len = NULL);
static wchar_t* Utf8ToWide(char* utf8,
intptr_t len = -1,
intptr_t* result_len = NULL);
static const wchar_t* Utf8ToWide(const char* utf8,
intptr_t len = -1,
intptr_t* result_len = NULL);
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, NULL, 0, NULL, NULL);
char* utf8 = reinterpret_cast<char*>(malloc(utf8_len));
WideCharToMultiByte(CP_UTF8, 0, wide, -1, utf8, utf8_len, NULL, NULL);
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);
};
class Utf8ToWideScope {
public:
explicit Utf8ToWideScope(const char* utf8, intptr_t length = -1) {
int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, length, NULL, 0);
wchar_t* wide =
reinterpret_cast<wchar_t*>(malloc(sizeof(wchar_t) * wide_len));
MultiByteToWideChar(CP_UTF8, 0, utf8, length, wide, wide_len);
length_ = wide_len;
wide_ = wide;
}
~Utf8ToWideScope() { free(wide_); }
wchar_t* wide() const { return wide_; }
intptr_t length() const { return length_; }
intptr_t size_in_bytes() const { return length_ * sizeof(*wide_); }
private:
intptr_t length_;
wchar_t* wide_;
DISALLOW_ALLOCATION();
DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8ToWideScope);
};
} // namespace bin
} // namespace dart
#endif // RUNTIME_BIN_UTILS_WIN_H_