license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 4 | |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 5 | #include "base/strings/sys_string_conversions.h" |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 6 | |
| 7 | #include <windows.h> |
avi | 84f37e1 | 2015-12-25 09:31:42 | [diff] [blame] | 8 | #include <stdint.h> |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 9 | |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 10 | #include "base/strings/string_piece.h" |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 11 | |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 12 | namespace base { |
| 13 | |
| 14 | // Do not assert in this function since it is used by the asssertion code! |
| 15 | std::string SysWideToUTF8(const std::wstring& wide) { |
| 16 | return SysWideToMultiByte(wide, CP_UTF8); |
| 17 | } |
| 18 | |
| 19 | // Do not assert in this function since it is used by the asssertion code! |
Reilly Grant | 39aecc3 | 2018-01-04 00:52:52 | [diff] [blame^] | 20 | std::wstring SysUTF8ToWide(StringPiece utf8) { |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 21 | return SysMultiByteToWide(utf8, CP_UTF8); |
| 22 | } |
| 23 | |
| 24 | std::string SysWideToNativeMB(const std::wstring& wide) { |
| 25 | return SysWideToMultiByte(wide, CP_ACP); |
| 26 | } |
| 27 | |
Reilly Grant | 39aecc3 | 2018-01-04 00:52:52 | [diff] [blame^] | 28 | std::wstring SysNativeMBToWide(StringPiece native_mb) { |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 29 | return SysMultiByteToWide(native_mb, CP_ACP); |
| 30 | } |
| 31 | |
| 32 | // Do not assert in this function since it is used by the asssertion code! |
Reilly Grant | 39aecc3 | 2018-01-04 00:52:52 | [diff] [blame^] | 33 | std::wstring SysMultiByteToWide(StringPiece mb, uint32_t code_page) { |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 34 | if (mb.empty()) |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 35 | return std::wstring(); |
| 36 | |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 37 | int mb_length = static_cast<int>(mb.length()); |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 38 | // Compute the length of the buffer. |
| 39 | int charcount = MultiByteToWideChar(code_page, 0, |
| 40 | mb.data(), mb_length, NULL, 0); |
| 41 | if (charcount == 0) |
| 42 | return std::wstring(); |
| 43 | |
| 44 | std::wstring wide; |
| 45 | wide.resize(charcount); |
| 46 | MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount); |
| 47 | |
| 48 | return wide; |
| 49 | } |
| 50 | |
| 51 | // Do not assert in this function since it is used by the asssertion code! |
avi | 84f37e1 | 2015-12-25 09:31:42 | [diff] [blame] | 52 | std::string SysWideToMultiByte(const std::wstring& wide, uint32_t code_page) { |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 53 | int wide_length = static_cast<int>(wide.length()); |
| 54 | if (wide_length == 0) |
| 55 | return std::string(); |
| 56 | |
| 57 | // Compute the length of the buffer we'll need. |
| 58 | int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length, |
| 59 | NULL, 0, NULL, NULL); |
| 60 | if (charcount == 0) |
| 61 | return std::string(); |
| 62 | |
| 63 | std::string mb; |
| 64 | mb.resize(charcount); |
| 65 | WideCharToMultiByte(code_page, 0, wide.data(), wide_length, |
| 66 | &mb[0], charcount, NULL, NULL); |
| 67 | |
| 68 | return mb; |
| 69 | } |
| 70 | |
| 71 | } // namespace base |