db_bridge/console.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:15 +0100
changeset 22 0db9235d68d9
parent 21 403742321c65
child 23 7acfc5eda7ed
permissions -rw-r--r--
mysql_db is untested because we have no sponsor with a mysql database committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
#include <console.h>
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     2
#include <cstdio>
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     4
#ifdef WIN32
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
     5
#include <windows.h>
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     6
#include <conio.h>
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
     7
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     8
int Console::getch ()
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     9
{
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    10
    int pressed = 0;
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    11
    int ch = -1;
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    12
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    13
    while (!pressed) {
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    14
        if (kbhit ()) {
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    15
            ch =::getch ();
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    16
            pressed = 1;
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    17
        }
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    18
        Sleep (10);
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    19
    }
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    20
    return ch;
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    21
}
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    22
#else
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    23
#include <termios.h>
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    24
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
int Console::getch ()
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
{
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    27
    int ch = -1, fd = 0;
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    struct termios neu, alt;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    fd = fileno (stdin);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    tcgetattr (fd, &alt);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    neu = alt;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    neu.c_lflag &= ~(ICANON | ECHO);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    tcsetattr (fd, TCSANOW, &neu);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    ch = getchar ();
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    tcsetattr (fd, TCSANOW, &alt);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    return ch;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
}
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    39
#endif
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
void Console::getpass (std::string & password)
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
{
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    int i = 0;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    int c;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    char buffer[17];
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    47
    while (((c = Console::getch ()) != '\n') && i < 17) {
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    48
        if (c != BS) {
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    49
            buffer[i++] = c;
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    50
            std::cout << "*";
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    51
        } else if (i > 0) {
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    52
            i--;
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    53
            std::cout << "\b \b";
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    54
        }
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
    }
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    buffer[i] = 0;
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    57
    std::cout << std::endl;
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
    password = std::string (buffer);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
}