1
+ /*
2
+ *--------------------------------------------------------------------
3
+ * Project: VM65 - Virtual Machine/CPU emulator programming
4
+ * framework.
5
+ *
6
+ * File: ConsoleIO.cpp
7
+ *
8
+ * Purpose: Implementation of ConsoleIO class.
9
+ * The ConsoleIO class has methods helpful when
10
+ * UI is utilizing STDIO (DOS) console.
11
+ *
12
+ * Date: 8/26/2016
13
+ *
14
+ * Copyright: (C) by Marek Karcz 2016. All rights reserved.
15
+ *
16
+
17
+ *
18
+ * License Agreement and Warranty:
19
+
20
+ This software is provided with No Warranty.
21
+ I (Marek Karcz) will not be held responsible for any damage to
22
+ computer systems, data or user's health resulting from use.
23
+ Please proceed responsibly and apply common sense.
24
+ This software is provided in hope that it will be useful.
25
+ It is free of charge for non-commercial and educational use.
26
+ Distribution of this software in non-commercial and educational
27
+ derivative work is permitted under condition that original
28
+ copyright notices and comments are preserved. Some 3-rd party work
29
+ included with this project may require separate application for
30
+ permission from their respective authors/copyright owners.
31
+
32
+ *--------------------------------------------------------------------
33
+ */
34
+ #include < stdio.h>
35
+ #include < iostream>
36
+ #include < sstream>
37
+ #include < string.h>
38
+ #include " system.h"
39
+ #include " ConsoleIO.h"
40
+
41
+ #include " MKGenException.h"
42
+
43
+ #if defined(WINDOWS)
44
+ #include < conio.h>
45
+ #endif
46
+
47
+ using namespace std ;
48
+
49
+ namespace MKBasic {
50
+
51
+ /*
52
+ *--------------------------------------------------------------------
53
+ * Method:
54
+ * Purpose:
55
+ * Arguments:
56
+ * Returns:
57
+ *--------------------------------------------------------------------
58
+ */
59
+ ConsoleIO::ConsoleIO ()
60
+ {
61
+
62
+ }
63
+
64
+ /*
65
+ *--------------------------------------------------------------------
66
+ * Method:
67
+ * Purpose:
68
+ * Arguments:
69
+ * Returns:
70
+ *--------------------------------------------------------------------
71
+ */
72
+ ConsoleIO::~ConsoleIO ()
73
+ {
74
+
75
+ }
76
+
77
+ #if defined(WINDOWS)
78
+
79
+ /*
80
+ *--------------------------------------------------------------------
81
+ * Method: ClearScreen()
82
+ * Purpose: Clear the console screen.
83
+ * Arguments: n/a
84
+ * Returns: n/a
85
+ *--------------------------------------------------------------------
86
+ */
87
+ void ConsoleIO::ClearScreen ()
88
+ {
89
+ HANDLE hStdOut;
90
+ CONSOLE_SCREEN_BUFFER_INFO csbi;
91
+ DWORD count;
92
+ DWORD cellCount;
93
+ COORD homeCoords = { 0 , 0 };
94
+
95
+ hStdOut = GetStdHandle ( STD_OUTPUT_HANDLE );
96
+ if (hStdOut == INVALID_HANDLE_VALUE) return ;
97
+
98
+ /* Get the number of cells in the current buffer */
99
+ if (!GetConsoleScreenBufferInfo ( hStdOut, &csbi )) return ;
100
+ cellCount = csbi.dwSize .X *csbi.dwSize .Y ;
101
+
102
+ /* Fill the entire buffer with spaces */
103
+ if (!FillConsoleOutputCharacter (
104
+ hStdOut,
105
+ (TCHAR) ' ' ,
106
+ cellCount,
107
+ homeCoords,
108
+ &count
109
+ )) return ;
110
+
111
+ /* Fill the entire buffer with the current colors and attributes */
112
+ if (!FillConsoleOutputAttribute (
113
+ hStdOut,
114
+ csbi.wAttributes ,
115
+ cellCount,
116
+ homeCoords,
117
+ &count
118
+ )) return ;
119
+
120
+ /* Move the cursor home */
121
+ SetConsoleCursorPosition ( hStdOut, homeCoords );
122
+ }
123
+
124
+ /*
125
+ *--------------------------------------------------------------------
126
+ * Method: ScrHome()
127
+ * Purpose: Bring the console cursor to home position.
128
+ * Arguments: n/a
129
+ * Returns: n/a
130
+ *--------------------------------------------------------------------
131
+ */
132
+ void ConsoleIO::ScrHome ()
133
+ {
134
+ HANDLE hStdOut;
135
+ COORD homeCoords = { 0 , 0 };
136
+
137
+ hStdOut = GetStdHandle ( STD_OUTPUT_HANDLE );
138
+ if (hStdOut == INVALID_HANDLE_VALUE) return ;
139
+
140
+ /* Move the cursor home */
141
+ SetConsoleCursorPosition ( hStdOut, homeCoords );
142
+ }
143
+
144
+ #endif
145
+
146
+ #if defined(LINUX)
147
+
148
+ /*
149
+ *--------------------------------------------------------------------
150
+ * Method: ClearScreen()
151
+ * Purpose: Clear the console screen.
152
+ * Arguments: n/a
153
+ * Returns: n/a
154
+ *--------------------------------------------------------------------
155
+ */
156
+ void ConsoleIO::ClearScreen ()
157
+ {
158
+ system (" clear" );
159
+ }
160
+
161
+ /*
162
+ *--------------------------------------------------------------------
163
+ * Method: ScrHome()
164
+ * Purpose: Bring the console cursor to home position.
165
+ * Arguments: n/a
166
+ * Returns: n/a
167
+ *--------------------------------------------------------------------
168
+ */
169
+ void ConsoleIO::ScrHome ()
170
+ {
171
+ cout << " \033 [1;1H" ;
172
+ }
173
+
174
+ #endif // #devine LINUX
175
+
176
+ } // END namespace MKBasic
0 commit comments