blob: 8b2367b3afa2d0fe485193de2004afa61618c72d [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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]15af80e2008-08-07 03:11:424
5#include "base/sys_string_conversions.h"
6
7#include <windows.h>
8
[email protected]4bdaceb42008-08-19 13:19:249#include "base/string_piece.h"
10
[email protected]15af80e2008-08-07 03:11:4211namespace base {
12
13// Do not assert in this function since it is used by the asssertion code!
14std::string SysWideToUTF8(const std::wstring& wide) {
15 return SysWideToMultiByte(wide, CP_UTF8);
16}
17
18// Do not assert in this function since it is used by the asssertion code!
[email protected]6c82baf2008-08-20 11:17:0019std::wstring SysUTF8ToWide(const StringPiece& utf8) {
[email protected]15af80e2008-08-07 03:11:4220 return SysMultiByteToWide(utf8, CP_UTF8);
21}
22
23std::string SysWideToNativeMB(const std::wstring& wide) {
24 return SysWideToMultiByte(wide, CP_ACP);
25}
26
[email protected]6c82baf2008-08-20 11:17:0027std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
[email protected]15af80e2008-08-07 03:11:4228 return SysMultiByteToWide(native_mb, CP_ACP);
29}
30
31// Do not assert in this function since it is used by the asssertion code!
[email protected]6c82baf2008-08-20 11:17:0032std::wstring SysMultiByteToWide(const StringPiece& mb, uint32 code_page) {
[email protected]4bdaceb42008-08-19 13:19:2433 if (mb.empty())
[email protected]15af80e2008-08-07 03:11:4234 return std::wstring();
35
[email protected]4bdaceb42008-08-19 13:19:2436 int mb_length = static_cast<int>(mb.length());
[email protected]15af80e2008-08-07 03:11:4237 // Compute the length of the buffer.
38 int charcount = MultiByteToWideChar(code_page, 0,
39 mb.data(), mb_length, NULL, 0);
40 if (charcount == 0)
41 return std::wstring();
42
43 std::wstring wide;
44 wide.resize(charcount);
45 MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount);
46
47 return wide;
48}
49
50// Do not assert in this function since it is used by the asssertion code!
51std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page) {
52 int wide_length = static_cast<int>(wide.length());
53 if (wide_length == 0)
54 return std::string();
55
56 // Compute the length of the buffer we'll need.
57 int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
58 NULL, 0, NULL, NULL);
59 if (charcount == 0)
60 return std::string();
61
62 std::string mb;
63 mb.resize(charcount);
64 WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
65 &mb[0], charcount, NULL, NULL);
66
67 return mb;
68}
69
70} // namespace base