ipc/chromium/src/base/sys_string_conversions_win.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/sys_string_conversions_win.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/sys_string_conversions.h"
     1.9 +
    1.10 +#include <windows.h>
    1.11 +
    1.12 +#include "base/string_piece.h"
    1.13 +
    1.14 +namespace base {
    1.15 +
    1.16 +// Do not assert in this function since it is used by the asssertion code!
    1.17 +std::string SysWideToUTF8(const std::wstring& wide) {
    1.18 +  return SysWideToMultiByte(wide, CP_UTF8);
    1.19 +}
    1.20 +
    1.21 +// Do not assert in this function since it is used by the asssertion code!
    1.22 +std::wstring SysUTF8ToWide(const StringPiece& utf8) {
    1.23 +  return SysMultiByteToWide(utf8, CP_UTF8);
    1.24 +}
    1.25 +
    1.26 +std::string SysWideToNativeMB(const std::wstring& wide) {
    1.27 +  return SysWideToMultiByte(wide, CP_ACP);
    1.28 +}
    1.29 +
    1.30 +std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
    1.31 +  return SysMultiByteToWide(native_mb, CP_ACP);
    1.32 +}
    1.33 +
    1.34 +// Do not assert in this function since it is used by the asssertion code!
    1.35 +std::wstring SysMultiByteToWide(const StringPiece& mb, uint32_t code_page) {
    1.36 +  if (mb.empty())
    1.37 +    return std::wstring();
    1.38 +
    1.39 +  int mb_length = static_cast<int>(mb.length());
    1.40 +  // Compute the length of the buffer.
    1.41 +  int charcount = MultiByteToWideChar(code_page, 0,
    1.42 +                                      mb.data(), mb_length, NULL, 0);
    1.43 +  if (charcount == 0)
    1.44 +    return std::wstring();
    1.45 +
    1.46 +  std::wstring wide;
    1.47 +  wide.resize(charcount);
    1.48 +  MultiByteToWideChar(code_page, 0, mb.data(), mb_length, &wide[0], charcount);
    1.49 +
    1.50 +  return wide;
    1.51 +}
    1.52 +
    1.53 +// Do not assert in this function since it is used by the asssertion code!
    1.54 +std::string SysWideToMultiByte(const std::wstring& wide, uint32_t code_page) {
    1.55 +  int wide_length = static_cast<int>(wide.length());
    1.56 +  if (wide_length == 0)
    1.57 +    return std::string();
    1.58 +
    1.59 +  // Compute the length of the buffer we'll need.
    1.60 +  int charcount = WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
    1.61 +                                      NULL, 0, NULL, NULL);
    1.62 +  if (charcount == 0)
    1.63 +    return std::string();
    1.64 +
    1.65 +  std::string mb;
    1.66 +  mb.resize(charcount);
    1.67 +  WideCharToMultiByte(code_page, 0, wide.data(), wide_length,
    1.68 +                      &mb[0], charcount, NULL, NULL);
    1.69 +
    1.70 +  return mb;
    1.71 +}
    1.72 +
    1.73 +}  // namespace base

mercurial