db_bridge/console.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 63 5a82f89d607e
parent 23 7acfc5eda7ed
child 65 76514757b0d6
permissions -rw-r--r--
uint vs size_t and two bugfixes in fak and unicode * Better Description of copy.cc * mem2swap throws a perror on failure committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23
7acfc5eda7ed this seems to be the proper way under ms, but it won't work under wine
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 21
diff changeset
     1
/**
7acfc5eda7ed this seems to be the proper way under ms, but it won't work under wine
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 21
diff changeset
     2
 * test/demos/db_bridge/console.cpp
7acfc5eda7ed this seems to be the proper way under ms, but it won't work under wine
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 21
diff changeset
     3
 * Copyright (C) Markus Broeker
7acfc5eda7ed this seems to be the proper way under ms, but it won't work under wine
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 21
diff changeset
     4
 */
7acfc5eda7ed this seems to be the proper way under ms, but it won't work under wine
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 21
diff changeset
     5
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <console.h>
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     7
#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
     8
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     9
#ifdef WIN32
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    10
#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
    11
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    12
int Console::getch ()
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    13
{
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    14
    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
    15
23
7acfc5eda7ed this seems to be the proper way under ms, but it won't work under wine
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 21
diff changeset
    16
    while (!kbhit ()) {
7acfc5eda7ed this seems to be the proper way under ms, but it won't work under wine
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 21
diff changeset
    17
        ch =::getch ();
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    18
    }
23
7acfc5eda7ed this seems to be the proper way under ms, but it won't work under wine
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 21
diff changeset
    19
21
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
}