Skip to content

Commit b230d2e

Browse files
Add CMake option for the user to choose whether they'd like to use the conio.h header file on Windows OS or not (by default)
1 parent 055a2e4 commit b230d2e

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
add_library(${CPP_SAFE_IO} STATIC cppsafeio.cpp)
22
target_include_directories(${CPP_SAFE_IO} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
3+
4+
if(NOT DEFINED INCLUDE_CONIO_FOR_IO)
5+
option(INCLUDE_CONIO_FOR_IO "Flag for including the conio.h header file" OFF)
6+
endif()
7+
8+
if(INCLUDE_CONIO_FOR_IO)
9+
message(STATUS "Set flag for including conio.h header file for I/O operations")
10+
if (NOT WIN32)
11+
message(FATAL_ERROR "Non-Windows OS detected.\nThe conio.h library is not available on other OSs. Terminating...")
12+
else()
13+
target_compile_definitions(${CPP_SAFE_IO} PRIVATE INCLUDE_CONIO_FOR_IO)
14+
endif()
15+
endif()

src/cppsafeio.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@
55
#include <stdexcept>
66
#include <limits>
77

8-
9-
#if defined _WIN32
10-
//#include <conio.h> //uncomment if your compiler supports conio.h
8+
#if defined(_WIN32) && defined(INCLUDE_CONIO_FOR_IO)
9+
#include <conio.h>
1110
#endif
1211

1312
void CppSafeIO::clearConsole()
1413
{
15-
#if defined _WIN32
14+
#if defined (_WIN32)
15+
16+
#if defined(INCLUDE_CONIO_FOR_IO)
17+
18+
clrsrc();
1619

17-
system("cls");
18-
//clrsrc(); //uncomment if your compiler supports conio.h, and comment out the previous statement
20+
#else
21+
22+
system("cls");
23+
24+
#endif
1925

2026
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
2127

@@ -40,8 +46,16 @@ void CppSafeIO::ignoreExceedingInput()
4046

4147
void CppSafeIO::pressEnterToContinue()
4248
{
43-
std::cin.get();
44-
ignoreExceedingInput();
49+
#if defined(_WIN32) && defined(INCLUDE_CONIO_FOR_IO)
50+
51+
while( getch() != '\r' );
52+
53+
#else
54+
55+
std::cin.get();
56+
ignoreExceedingInput();
57+
58+
#endif
4559
}
4660

4761
bool CppSafeIO::parseYesNoInput(char yesChar, char noChar) {

0 commit comments

Comments
 (0)