Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 110 lines (92 sloc) 3.535 kB
13b9cbc @candera Copied remotely
authored
1 /*
93cda4e @candera Updated copyright date to 2011.
authored
2 Copyright (c) 2011 Wangdera Corporation (hobocopy@wangdera.com)
13b9cbc @candera Copied remotely
authored
3
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #pragma once
25
26 #include "Utilities.h"
27
28 class Console
29 {
30 private:
31 static HANDLE s_hStdOut;
32 public:
33 static HANDLE get_StandardOutput(void)
34 {
35 if (s_hStdOut == INVALID_HANDLE_VALUE)
36 {
37 HANDLE hOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
38
39 if (hOut != INVALID_HANDLE_VALUE)
40 {
41 ::DuplicateHandle(::GetCurrentProcess(),
42 hOut,
43 ::GetCurrentProcess(),
44 &s_hStdOut,
45 0,
46 FALSE,
47 DUPLICATE_SAME_ACCESS);
48 }
49 }
50 return s_hStdOut;
51 }
52
53 static TCHAR ReadChar()
54 {
55 HANDLE hStdIn = ::GetStdHandle(STD_INPUT_HANDLE);
56
57 TCHAR buffer[1];
58 DWORD charsRead;
59 BOOL bWorked = ::ReadConsole(hStdIn,
60 buffer,
61 1,
62 &charsRead,
63 NULL);
64
65 if (!bWorked)
66 {
67 DWORD error = ::GetLastError();
68 CString errorMessage;
69 Utilities::FormatErrorMessage(error, errorMessage);
70 CString message;
71 message.AppendFormat(TEXT("There was an error calling ReadConsole. Error %s"), errorMessage);
72 throw new CHoboCopyException(message);
73 }
74
75 if (charsRead != 1)
76 {
77 throw new CHoboCopyException(TEXT("ReadConsole was unable to read a character."));
78 }
79
80 return buffer[0];
81 }
82
83 static void Write(LPCTSTR message)
84 {
85 CString messageString(message);
86
87 LPCSTR narrowMessage = Utilities::ConvertToMultibyteString(message);
88
89 DWORD charsWritten;
90 BOOL bWorked = ::WriteFile(get_StandardOutput(), narrowMessage, messageString.GetLength(), &charsWritten, NULL);
91
92 Utilities::Free(narrowMessage);
93
94 if (!bWorked)
95 {
96 DWORD error = ::GetLastError();
97 CString errorMessage;
98 Utilities::FormatErrorMessage(error, errorMessage);
99 CString message;
100 message.AppendFormat(TEXT("Unable to write to the console. %s."), errorMessage);
101 throw new CHoboCopyException(message);
102 }
103 }
104
105 static void WriteLine(LPCTSTR message)
106 {
107 Write(message);
108 Write(TEXT("\r\n"));
109 }
110 };
Something went wrong with that request. Please try again.