bux API Reference 1.9.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
XConsole.cpp
Go to the documentation of this file.
1#include <iostream> // std::cerr
2#include <cstring> // strchr()
3#include "XConsole.h"
4#ifdef _WIN32
5 #include <conio.h> // _kbhit(), _getch()
6 #include <io.h> // _access()
7 #include <windows.h> // GetStdHandle(), SetConsoleCursorPosition()
8#elif defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
9 #include <termios.h> // struct termios
10 #include <unistd.h> // access()
11 #include <fcntl.h> // fcntl()
12#else
13 #error "Platform other than Windows and Unix-like not supported yet"
14#endif
15
16namespace {
17
18using namespace bux;
19
20//
21// In-Module Types
22//
23#if defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
24class FC_GetChar
25{
26public:
27
28 // Nonvirtuals
29 FC_GetChar();
30 ~FC_GetChar();
31 int operator()();
32 void bufferOff();
33 void bufferOn();
34 void echoOff();
35 void echoOn();
36 bool kbhit();
37
38private:
39
40 // Data
41 struct termios m_oldAttr, m_curAttr;
42 bool m_dirty;
43
44 // Nonvirtuals
45 void updateAttr();
46};
47#endif
48
49//
50// In-Module Functions
51//
52#ifdef _MSC_VER
53void clreol()
54{
55 CONSOLE_SCREEN_BUFFER_INFO info;
56 DWORD _;
57 const HANDLE hConsole =GetStdHandle(STD_OUTPUT_HANDLE);
58 if (INVALID_HANDLE_VALUE != hConsole &&
59 GetConsoleScreenBufferInfo(hConsole, &info))
60 FillConsoleOutputCharacter(hConsole, ' ', info.dwSize.X-info.dwCursorPosition.X,
61 info.dwCursorPosition, &_);
62}
63
64void gotoxy(COORD pos)
65{
66 const HANDLE hConsole =GetStdHandle(STD_OUTPUT_HANDLE);
67 if (INVALID_HANDLE_VALUE != hConsole)
68 SetConsoleCursorPosition(hConsole, pos);
69}
70
71COORD cur_pos()
72{
73 CONSOLE_SCREEN_BUFFER_INFO info;
74 const HANDLE hConsole =GetStdHandle(STD_OUTPUT_HANDLE);
75 if (INVALID_HANDLE_VALUE != hConsole &&
76 GetConsoleScreenBufferInfo(hConsole, &info))
77 return info.dwCursorPosition;
78
79 static const COORD def ={0, 0};
80 return def;
81}
82#endif
83
84//
85// Implement Classes
86//
87#if defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
88FC_GetChar::FC_GetChar(): m_dirty(false)
89{
90 tcgetattr(0, &m_oldAttr);
91 m_curAttr = m_oldAttr;
92}
93
94FC_GetChar::~FC_GetChar()
95{
96 if (m_curAttr.c_lflag != m_oldAttr.c_lflag)
97 tcsetattr(0, TCSANOW, &m_oldAttr);
98}
99
100int FC_GetChar::operator()()
101{
102 updateAttr();
103 return getchar();
104}
105
106void FC_GetChar::bufferOff()
107{
108 m_curAttr.c_lflag &= unsigned(~ICANON);
109 m_dirty = true;
110}
111
112void FC_GetChar::echoOff()
113{
114 m_curAttr.c_lflag &= unsigned(~ECHO);
115 m_dirty = true;
116}
117
118#if 0
119void FC_GetChar::bufferOn()
120{
121 m_curAttr.c_lflag |= ICANON;
122 m_dirty = true;
123}
124
125void FC_GetChar::echoOn()
126{
127 m_curAttr.c_lflag |= ECHO;
128 m_dirty = true;
129}
130
131bool FC_GetChar::kbhit()
132{
133 updateAttr();
134 const int oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
135 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
136 const int ch = getchar();
137 fcntl(STDIN_FILENO, F_SETFL, oldf);
138
139 if (ch != EOF)
140 {
141 ungetc(ch, stdin);
142 return true;
143 }
144 return false;
145}
146#endif
147
148void FC_GetChar::updateAttr()
149{
150 if (m_dirty)
151 {
152 tcsetattr(0, TCSANOW, &m_curAttr);
153 m_dirty = false;
154 }
155}
156#endif
157
158} // namespace
159
160namespace bux {
161
162//
163// Functions
164//
165void getKeyPunch(std::string_view msg, int &key, const char *keyFilter)
166{
167 std::cerr <<msg;
168 int c;
169#ifdef _WIN32
170 do c =_getch();
171#elif defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
172 FC_GetChar getc;
173 getc.bufferOff();
174 getc.echoOff();
175 do c = getc();
176#endif
177 while (keyFilter && !strchr(keyFilter, c));
178
179 std::cerr <<char(c) <<'\n';
180 key = c;
181}
182
184{
185#ifdef _WIN32
186 _getch();
187#elif defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
188 FC_GetChar getc;
189 getc.bufferOff();
190 getc.echoOff();
191 getc();
192#endif
193}
194
195bool testWritability(const char *file)
196{
197#ifdef _WIN32
198 if (!_access(file, 0))
199#elif defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
200 if (!access(file, F_OK))
201#endif
202 {
203 int c;
204 getKeyPunch(std::string(file).append(" already exists. Overwrite it ?(y/n)"), c, "ynYN");
205 if (c == 'n' || c == 'N')
206 return false;
207 }
208 return true;
209}
210
211} // namespace bux
THE common namespace of bux library.
Definition AtomiX.cpp:3
bool testWritability(const char *file)
Definition XConsole.cpp:195
void pressAKey()
Definition XConsole.cpp:183
void getKeyPunch(std::string_view msg, int &key, const char *keyFilter)
Definition XConsole.cpp:165
void append(const T &src, std::string &dst)
Definition Serialize.h:14