db_bridge/console.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:53 +0200
changeset 77 49e0babccb23
parent 69 b5912e5f899f
permissions -rw-r--r--
HEADER TAGS The std header tags are now subdir/file.ext 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
65
76514757b0d6 GNU Indent cannot handle C++ Source Files...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 23
diff changeset
     6
#include <console.hpp>
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
69
b5912e5f899f Useless cross_getch hack removed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 65
diff changeset
    16
    ch =::getch ();
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
    17
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    18
    return ch;
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    19
}
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    20
#else
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    21
#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
    22
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
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
    24
{
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    25
    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
    26
    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
    27
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    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
    29
    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
    30
    neu = alt;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    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
    32
    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
    33
    ch = getchar ();
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, &alt);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    return ch;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
}
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    37
#endif
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
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
    40
{
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    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
    42
    int c;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    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
    44
21
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    45
    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
    46
        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
    47
            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
    48
            std::cout << "*";
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    49
        } 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
    50
            i--;
403742321c65 console functions work fine under linux and not under windows
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 20
diff changeset
    51
            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
    52
        }
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
    }
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    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
    55
    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
    56
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    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
    58
}