YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
MinGW32.cpp
浏览该文件的文档.
1 /*
2  © 2013-2014 FrankHB.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
29 #include "YCLib/YModules.h"
30 #include YFM_MinGW32_YCLib_MinGW32
31 
32 using namespace YSLib;
33 
34 namespace platform_ex
35 {
36 
37 namespace Windows
38 {
39 
40 Win32Exception::Win32Exception(ErrorCode ec, const std::string& s, LevelType l)
41  ynothrow
42  : Exception([&]{
43  try
44  {
45  return s + ": " + FormatMessage(ec);
46  }
47  catch(...)
48  {}
49  return s;
50  }(), l),
51  err(ec)
52 {
53  YAssert(ec != 0, "No error should be thrown.");
54 }
55 
57 Win32Exception::FormatMessage(ErrorCode ec) ynothrow
58 {
59  try
60  {
61  wchar_t* buf{};
62 
63  ::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
64  | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, {},
65  ec, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
66  reinterpret_cast<wchar_t*>(&buf), 1, {});
67 
68  auto res(WCSToMBCS(buf, CP_UTF8));
69 
70  ::LocalFree(buf);
71  return res;
72  }
73  catch(...)
74  {}
75  return {};
76 }
77 
78 
79 bool
81 {
82  try
83  {
84  RegisterKey k1(HKEY_CURRENT_USER, L"Software\\Wine");
85  RegisterKey k2(HKEY_LOCAL_MACHINE, L"Software\\Wine");
86 
87  yunused(k1),
88  yunused(k2);
89  return true;
90  }
91  catch(Win32Exception&)
92  {}
93  return false;
94 }
95 
96 
98 MBCSToMBCS(const char* str, std::size_t len, int cp_src, int cp_dst)
99 {
100  if (cp_src == cp_dst)
101  return str;
102 
103  const int w_len(::MultiByteToWideChar(cp_src, 0, str, len, {}, 0));
104  std::wstring wstr(w_len, wchar_t());
105  wchar_t* w_str = &wstr[0];
106 
107  ::MultiByteToWideChar(cp_src, 0, str, len, w_str, w_len);
108 
109  return WCSToMBCS(w_str, w_len, cp_dst);
110 }
111 
113 WCSToMBCS(const wchar_t* str, std::size_t len, int cp)
114 {
115  const int r_len(::WideCharToMultiByte(cp, 0, str, len,
116  nullptr, 0, nullptr, nullptr));
117  std::string mbcs(r_len, char());
118 
119  ::WideCharToMultiByte(cp, 0, str, len, &mbcs[0], r_len, {}, {});
120  return mbcs;
121 }
122 
123 std::wstring
124 MBCSToWCS(const char* str, std::size_t len, int cp)
125 {
126  const auto w_len(::MultiByteToWideChar(cp, 0, str, len, {}, 0));
127  std::wstring res(w_len, wchar_t());
128  const auto w_str = &res[0];
129 
130  ::MultiByteToWideChar(cp, 0, str, len, w_str, w_len);
131 
132  return res;
133 }
134 
135 } // namespace Windows;
136 
137 } // namespace YSLib;
138 
std::wstring MBCSToWCS(const char *str, std::size_t len, int cp)
Definition: MinGW32.cpp:124
#define yunused(...)
标记未使用的表达式。
Definition: ydef.h:697
YF_API std::string MBCSToMBCS(const char *, std::size_t, int=CP_UTF8, int=CP_ACP)
转换第一个 int 参数指定编码的字符串为第二个 int 参数指定的编码。
Definition: MinGW32.cpp:98
std::exception Exception
YSLib 异常基类。
Definition: yexcept.h:44
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
Win32 错误引起的宿主异常。
std::string WCSToMBCS(const wchar_t *str, std::size_t len, int cp)
Definition: MinGW32.cpp:113
YF_API bool CheckWine()
判断是否在 Wine 环境下运行。
Definition: MinGW32.cpp:80
#define YAssert(_expr, _msg)
Definition: cassert.h:73