| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 | 6 |
| 7 #include "bin/utils.h" | 7 #include "bin/utils.h" |
| 8 #include "bin/log.h" | 8 #include "bin/log.h" |
| 9 | 9 |
| 10 static void FormatMessageIntoBuffer(DWORD code, | 10 static void FormatMessageIntoBuffer(DWORD code, |
| 11 char* buffer, | 11 wchar_t* buffer, |
| 12 int buffer_length) { | 12 int buffer_length) { |
| 13 DWORD message_size = | 13 DWORD message_size = |
| 14 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | 14 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 15 NULL, | 15 NULL, |
| 16 code, | 16 code, |
| 17 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | 17 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 18 buffer, | 18 buffer, |
| 19 buffer_length, | 19 buffer_length, |
| 20 NULL); | 20 NULL); |
| 21 if (message_size == 0) { | 21 if (message_size == 0) { |
| 22 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { | 22 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 23 Log::PrintErr("FormatMessage failed %d\n", GetLastError()); | 23 Log::PrintErr("FormatMessage failed %d\n", GetLastError()); |
| 24 } | 24 } |
| 25 snprintf(buffer, buffer_length, "OS Error %d", code); | 25 _snwprintf(buffer, buffer_length, L"OS Error %d", code); |
| 26 } | 26 } |
| 27 buffer[buffer_length - 1] = '\0'; | 27 buffer[buffer_length - 1] = '\0'; |
| 28 } | 28 } |
| 29 | 29 |
| 30 | 30 |
| 31 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { | 31 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { |
| 32 set_code(GetLastError()); | 32 set_code(GetLastError()); |
| 33 | 33 |
| 34 static const int kMaxMessageLength = 256; | 34 static const int kMaxMessageLength = 256; |
| 35 char message[kMaxMessageLength]; | 35 wchar_t message[kMaxMessageLength]; |
| 36 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); | 36 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); |
| 37 SetMessage(message); | 37 char* utf8 = StringUtils::WideToUtf8(message); |
| 38 SetMessage(utf8); |
| 39 free(utf8); |
| 38 } | 40 } |
| 39 | 41 |
| 40 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { | 42 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { |
| 41 set_sub_system(sub_system); | 43 set_sub_system(sub_system); |
| 42 set_code(code); | 44 set_code(code); |
| 43 | 45 |
| 44 static const int kMaxMessageLength = 256; | 46 static const int kMaxMessageLength = 256; |
| 45 char message[kMaxMessageLength]; | 47 wchar_t message[kMaxMessageLength]; |
| 46 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); | 48 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); |
| 47 SetMessage(message); | 49 char* utf8 = StringUtils::WideToUtf8(message); |
| 50 SetMessage(utf8); |
| 51 free(utf8); |
| 48 } | 52 } |
| 49 | 53 |
| 50 char* StringUtils::SystemStringToUtf8(char* str) { | 54 char* StringUtils::ConsoleStringToUtf8(char* str) { |
| 51 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); | 55 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); |
| 52 wchar_t* unicode = new wchar_t[len+1]; | 56 wchar_t* unicode = new wchar_t[len+1]; |
| 53 MultiByteToWideChar(CP_ACP, 0, str, -1, unicode, len); | 57 MultiByteToWideChar(CP_ACP, 0, str, -1, unicode, len); |
| 54 unicode[len] = '\0'; | 58 unicode[len] = '\0'; |
| 55 len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL); | 59 char* utf8 = StringUtils::WideToUtf8(unicode); |
| 56 char* utf8 = reinterpret_cast<char*>(malloc(len+1)); | |
| 57 WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len, NULL, NULL); | |
| 58 utf8[len] = '\0'; | |
| 59 delete[] unicode; | 60 delete[] unicode; |
| 60 return utf8; | 61 return utf8; |
| 61 } | 62 } |
| 62 | 63 |
| 63 char* StringUtils::Utf8ToSystemString(char* utf8) { | 64 char* StringUtils::Utf8ToConsoleString(char* utf8) { |
| 64 int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); | 65 wchar_t* unicode = Utf8ToWide(utf8); |
| 65 wchar_t* unicode = new wchar_t[len+1]; | 66 int len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL); |
| 66 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, unicode, len); | 67 char* ansi = reinterpret_cast<char*>(malloc(len + 1)); |
| 67 unicode[len] = '\0'; | |
| 68 len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL); | |
| 69 char* ansi = reinterpret_cast<char*>(malloc(len+1)); | |
| 70 WideCharToMultiByte(CP_ACP, 0, unicode, -1, ansi, len, NULL, NULL); | 68 WideCharToMultiByte(CP_ACP, 0, unicode, -1, ansi, len, NULL, NULL); |
| 71 ansi[len] = '\0'; | 69 ansi[len] = '\0'; |
| 72 delete[] unicode; | 70 free(unicode); |
| 73 return ansi; | 71 return ansi; |
| 74 } | 72 } |
| 75 | 73 |
| 76 const char* StringUtils::Utf8ToSystemString(const char* utf8) { | 74 char* StringUtils::WideToUtf8(wchar_t* wide) { |
| 77 return const_cast<const char*>(Utf8ToSystemString(const_cast<char*>(utf8))); | 75 int len = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL); |
| 76 char* utf8 = reinterpret_cast<char*>(malloc(len + 1)); |
| 77 WideCharToMultiByte(CP_UTF8, 0, wide, -1, utf8, len, NULL, NULL); |
| 78 utf8[len] = '\0'; |
| 79 return utf8; |
| 78 } | 80 } |
| 79 | 81 |
| 80 const char* StringUtils::SystemStringToUtf8(const char* str) { | 82 |
| 81 return const_cast<const char*>(Utf8ToSystemString(const_cast<char*>(str))); | 83 wchar_t* StringUtils::Utf8ToWide(char* utf8) { |
| 84 int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); |
| 85 wchar_t* unicode = |
| 86 reinterpret_cast<wchar_t*>(malloc((len + 1) * sizeof(wchar_t))); |
| 87 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, unicode, len); |
| 88 unicode[len] = '\0'; |
| 89 return unicode; |
| 82 } | 90 } |
| 91 |
| 92 const char* StringUtils::Utf8ToConsoleString(const char* utf8) { |
| 93 return const_cast<const char*>(Utf8ToConsoleString(const_cast<char*>(utf8))); |
| 94 } |
| 95 |
| 96 const char* StringUtils::ConsoleStringToUtf8(const char* str) { |
| 97 return const_cast<const char*>(ConsoleStringToUtf8(const_cast<char*>(str))); |
| 98 } |
| 99 |
| 100 const char* StringUtils::WideToUtf8(const wchar_t* wide) { |
| 101 return const_cast<const char*>(WideToUtf8(const_cast<wchar_t*>(wide))); |
| 102 } |
| 103 |
| 104 const wchar_t* StringUtils::Utf8ToWide(const char* utf8) { |
| 105 return const_cast<const wchar_t*>(Utf8ToWide(const_cast<char*>(utf8))); |
| 106 } |
| OLD | NEW |