db_bridge/console.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:13 +0100
changeset 20 5fec678f931b
parent 19 933d86c1ff71
child 21 403742321c65
permissions -rw-r--r--
getpass for windows does not work committer: Markus Bröker <mbroeker@largo.homelinux.org>

#include <console.h>
#include <cstdio>

#ifdef WIN32
#include <conio.h>
int Console::getch ()
{
    return::getch ();
}
#else
#include <termios.h>
int Console::getch ()
{
    static int ch = -1, fd = 0;
    struct termios neu, alt;

    fd = fileno (stdin);
    tcgetattr (fd, &alt);
    neu = alt;
    neu.c_lflag &= ~(ICANON | ECHO);
    tcsetattr (fd, TCSANOW, &neu);
    ch = getchar ();
    tcsetattr (fd, TCSANOW, &alt);
    return ch;
}
#endif

void Console::getpass (std::string & password)
{
    int i = 0;
    int c;
    char buffer[17];

    while ((c = Console::getch ()) != '\n' && i < 17) {
        buffer[i++] = c;
        std::cout << "\b*";
    }
    buffer[i] = 0;

    password = std::string (buffer);
    std::cout << std::endl;
}