console functions work fine under linux and not under windows
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:14 +0100
changeset 21 403742321c65
parent 20 5fec678f931b
child 22 0db9235d68d9
console functions work fine under linux and not under windows committer: Markus Bröker <mbroeker@largo.homelinux.org>
db_bridge/console.cpp
db_bridge/include/console.h
--- a/db_bridge/console.cpp
+++ b/db_bridge/console.cpp
@@ -2,16 +2,29 @@
 #include <cstdio>
 
 #ifdef WIN32
+#include <windows.h>
 #include <conio.h>
+
 int Console::getch ()
 {
-    return::getch ();
+    int pressed = 0;
+    int ch = -1;
+
+    while (!pressed) {
+        if (kbhit ()) {
+            ch =::getch ();
+            pressed = 1;
+        }
+        Sleep (10);
+    }
+    return ch;
 }
 #else
 #include <termios.h>
+
 int Console::getch ()
 {
-    static int ch = -1, fd = 0;
+    int ch = -1, fd = 0;
     struct termios neu, alt;
 
     fd = fileno (stdin);
@@ -31,12 +44,17 @@
     int c;
     char buffer[17];
 
-    while ((c = Console::getch ()) != '\n' && i < 17) {
-        buffer[i++] = c;
-        std::cout << "\b*";
+    while (((c = Console::getch ()) != '\n') && i < 17) {
+        if (c != BS) {
+            buffer[i++] = c;
+            std::cout << "*";
+        } else if (i > 0) {
+            i--;
+            std::cout << "\b \b";
+        }
     }
     buffer[i] = 0;
+    std::cout << std::endl;
 
     password = std::string (buffer);
-    std::cout << std::endl;
 }
--- a/db_bridge/include/console.h
+++ b/db_bridge/include/console.h
@@ -1,6 +1,12 @@
 #ifndef CONSOLE_H
 #define CONSOLE_H
 
+#ifdef WIN32
+#define BS 127
+#else
+#define BS 127
+#endif
+
 #include <iostream>
 
 namespace Console {