blob: c73654933c71e0f17fa925a644de854c47c29c3d [file] [log] [blame]
[email protected]15af80e2008-08-07 03:11:421// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "base/sys_string_conversions.h"
31
32#include <windows.h>
33
34namespace base {
35
36// Do not assert in this function since it is used by the asssertion code!
37std::string SysWideToUTF8(const std::wstring& wide) {
38 return SysWideToMultiByte(wide, CP_UTF8);
39}
40
41// Do not assert in this function since it is used by the asssertion code!
42std::wstring SysUTF8ToWide(const std::string& utf8) {
43 return SysMultiByteToWide(utf8, CP_UTF8);
44}
45
46std::string SysWideToNativeMB(const std::wstring& wide) {
47 return SysWideToMultiByte(wide, CP_ACP);
48}
49
50std::wstring SysNativeMBToWide(const std::string& native_mb) {
51 return SysMultiByteToWide(native_mb, CP_ACP);
52}
53
54// Do not assert in this function since it is used by the asssertion code!
55std::wstring SysMultiByteToWide(const std::string& mb, uint32 code_page) {
56 int mb_length = static_cast<int>(mb.length());
57 if (mb_length == 0)
58 return std::wstring();
59
60 // Compute the length of the buffer.
61 int charcount = MultiByteToWideChar(code_page, 0,
62 mb.data(), mb_length, NULL, 0);
63 if (charcount == 0)
64 return std::wstring();
65
66 std::wstring wide;
67 wide.resize(charcount);
68 MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount);
69
70 return wide;
71}
72
73// Do not assert in this function since it is used by the asssertion code!
74std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page) {
75 int wide_length = static_cast<int>(wide.length());
76 if (wide_length == 0)
77 return std::string();
78
79 // Compute the length of the buffer we'll need.
80 int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
81 NULL, 0, NULL, NULL);
82 if (charcount == 0)
83 return std::string();
84
85 std::string mb;
86 mb.resize(charcount);
87 WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
88 &mb[0], charcount, NULL, NULL);
89
90 return mb;
91}
92
93} // namespace base