YSTest: Consoles.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

Consoles.cpp
浏览该文件的文档.
1 /*
2 © 2013-2016 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_YCLib_Platform
31 #if YCL_Win32
32 # include YFM_Win32_YCLib_Consoles // for UTF8ToWCS;
33 # include YFM_YSLib_Core_YCoreUtilities // for YSLib::CheckPositive;
34 # include YFM_Win32_YCLib_NLS // for platform_ex::MBCSToWCS;
35
36 using namespace YSLib;
37 #endif
38
39 namespace platform_ex
40 {
41
42 #if YCL_Win32
43
44 inline namespace Windows
45 {
46
48 namespace
49 {
50
52 int WINAPI
53 ConsoleHandler(unsigned long ctrl)
54 {
55 switch (ctrl)
56 {
57 case CTRL_C_EVENT:
58 case CTRL_BREAK_EVENT:
59 case CTRL_CLOSE_EVENT:
60 case CTRL_LOGOFF_EVENT:
61 case CTRL_SHUTDOWN_EVENT:
62 std::_Exit(int(STATUS_CONTROL_C_EXIT));
63 }
64 return 0;
65 }
66
67 } // unnamed namespace;
68
69 void
70 FixConsoleHandler(int(WINAPI* handler)(unsigned long), bool add)
71 {
72 YCL_CallF_Win32(SetConsoleCtrlHandler, handler ? handler : ConsoleHandler,
73 add);
74 }
75
76
77 WConsole::WConsole(unsigned long dev)
78 : WConsole(::GetStdHandle(dev))
79 {}
80 WConsole::WConsole(::HANDLE h)
81 : h_std(h), saved_attr(GetScreenBufferInfo().wAttributes),
82 Attributes(saved_attr)
83 {
84 YAssert(h_std && h_std != INVALID_HANDLE_VALUE, "Invalid handle found.");
85 }
86 WConsole::~WConsole()
87 {
88 FilterExceptions(std::bind(&WConsole::RestoreAttributes, this), yfsig);
89 }
90
91 ::CONSOLE_SCREEN_BUFFER_INFO
92 WConsole::GetScreenBufferInfo() const
93 {
94 ::CONSOLE_SCREEN_BUFFER_INFO info;
95
96 YCL_CallF_Win32(GetConsoleScreenBufferInfo, h_std, &info);
97 return info;
98 }
99
100 void
101 WConsole::SetSystemColor(std::uint8_t color)
102 {
103 char cmd[9];
104
105 std::sprintf(cmd, "COLOR %02x", int(color));
106 std::system(cmd);
107 }
108
109 void
110 WConsole::SetBackColor(std::uint8_t bc) ynothrow
111 {
112 Attributes = ComposeAttributes(FetchForeColor(Attributes), bc);
113 }
114 void
115 WConsole::SetCursorPosition(::COORD pos)
116 {
117 // NOTE: %::SetConsoleCursorPosition expects 1-based.
118 YCL_CallF_Win32(SetConsoleCursorPosition, h_std, {short(pos.X + 1), short(pos.Y + 1)});
119 }
120 void
121 WConsole::SetForeColor(std::uint8_t fc) ynothrow
122 {
123 Attributes = ComposeAttributes(fc, FetchBackColor(Attributes));
124 }
125
126 ::WORD
127 WConsole::ComposeAttributes(std::uint8_t fore, std::uint8_t back) ynothrow
128 {
129 return (fore & 15) | ((back & 15) << 4);
130 }
131
132 void
133 WConsole::CursorUp(size_t num_rows)
134 {
135 if(num_rows != 0)
136 {
137 const auto pos(GetCursorPosition());
138
139 // XXX: Conversion to 'short' might be implementation-defined.
140 SetCursorPosition({short(pos.Y - short(num_rows)), pos.X});
141 }
142 }
143
144 void
145 WConsole::Erase(wchar_t c)
146 {
147 const auto size(GetScreenBufferInfo().dwSize);
148
149 Fill({short(), short()}, CheckPositive<unsigned long>(size.X)
150 * CheckPositive<unsigned long>(size.Y), c);
151 }
152
153 void
154 WConsole::Fill(::COORD coord, unsigned long n, wchar_t c)
155 {
156 YCL_CallF_Win32(FillConsoleOutputCharacterW, h_std, c, n, coord, {});
157 YCL_CallF_Win32(FillConsoleOutputAttribute, h_std, Attributes, n, coord, {});
158 YCL_CallF_Win32(SetConsoleCursorPosition, h_std, {coord.X, coord.Y});
159 }
160
161 void
162 WConsole::RestoreAttributes()
163 {
164 // SetColor();
165 Update(GetSavedAttributes());
166 }
167
168 void
169 WConsole::Update()
170 {
171 Update(Attributes);
172 }
173 void
174 WConsole::Update(::WORD value)
175 {
176 YCL_CallF_Win32(SetConsoleTextAttribute, h_std, value);
177 }
178
179 void
180 WConsole::UpdateBackColor(std::uint8_t fc)
181 {
182 SetBackColor(fc);
183 Update();
184 }
185
186 void
187 WConsole::UpdateForeColor(std::uint8_t fc)
188 {
189 SetForeColor(fc);
190 Update();
191 }
192
193 size_t
194 WConsole::WriteString(string_view sv)
195 {
196 YAssertNonnull(sv.data());
197 // FIXME: Support for non-BMP characters.
198 return WriteString(UTF8ToWCS(sv));
199 }
200 size_t
201 WConsole::WriteString(string_view sv, unsigned cp)
202 {
203 YAssertNonnull(sv.data());
204 return WriteString(MBCSToWCS(sv, cp));
205 }
206 size_t
207 WConsole::WriteString(wstring_view sv)
208 {
209 YAssertNonnull(sv.data());
210
211 unsigned long n;
212
213 YCL_CallF_Win32(WriteConsoleW, h_std, sv.data(),
214 static_cast<unsigned long>(sv.length()), &n, yimpl({}));
215 return size_t(n);
216 }
217
218 unique_ptr<WConsole>
219 MakeWConsole(unsigned long h)
220 {
221 unique_ptr<WConsole> p_con;
222
223 TryExpr(p_con.reset(new WConsole(h)))
224 CatchIgnore(Win32Exception&)
225 return p_con;
226 }
227
228 } // inline namespace Windows;
229
230 #endif
231
232 } // namespace platform_ex;
233
platform_ex::Windows::WConsole::WConsole
WConsole(unsigned long h=STD_OUTPUT_HANDLE)
YSLib::UI::fc
fc
Definition: Console.h:93
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
yfsig
#define yfsig
展开为表示函数签名的预定义变量的宏。
Definition: ydef.h:728
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:120
CatchIgnore
#define CatchIgnore(_ex)
指定异常处理器忽略指定异常。
Definition: YBaseMacro.h:133
YCL_CallF_Win32
#define YCL_CallF_Win32(_fn,...)
Definition: MinGW32.h:190
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
ystdex::wstring_view
basic_string_view< wchar_t > wstring_view
Definition: string_view.hpp:418
YSLib::Drawing::Fill
YF_API void Fill(const Graphics &, Color)
使用指定颜色填充图形接口上下文缓冲区。
Definition: YBlit.cpp:94
YSLib::FilterExceptions
bool FilterExceptions(_func f, const char *desc={}, RecordLevel lv=Alert, ExceptionTracer trace=ExtractAndTrace) ynothrow
调用函数并过滤宿主异常。
Definition: YException.h:203
platform_ex::Windows::FixConsoleHandler
YF_API void FixConsoleHandler(int(WINAPI *)(unsigned long)={}, bool=true)
安装控制台处理器。
YModules.h
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
platform_ex::Windows::UTF8ToWCS
UTF8ToWCS
Definition: NLS.h:72
platform_ex
平台扩展命名空间。
Definition: Android.h:45
CHRLib::c
c
Definition: CharacterProcessing.h:81
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
platform_ex::Windows::MBCSToWCS
YF_API wstring MBCSToWCS(string_view, unsigned=CP_ACP)