-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
80 lines (63 loc) · 1.45 KB
/
util.cpp
File metadata and controls
80 lines (63 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/***********************************************************
util.cpp
Various utility functions.
************************************************************/
#include "util.h"
/*
Assists with password input masking.
taken from:
http://www.cplusplus.com/articles/E6vU7k9E/
*/
int getch() {
int ch;
struct termios t_old, t_new;
tcgetattr(STDIN_FILENO, &t_old);
t_new = t_old;
t_new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
return ch;
}
/*
Assists with password input masking.
taken from:
http://www.cplusplus.com/articles/E6vU7k9E/
*/
string getpass(const char *prompt, bool show_asterisk)
{
const char BACKSPACE=127;
const char RETURN=10;
string password;
unsigned char ch=0;
cout << prompt << endl;
while((ch=getch())!=RETURN)
{
if(ch==BACKSPACE)
{
if(password.length()!=0)
{
if(show_asterisk)
cout <<"\b \b";
password.resize(password.length()-1);
}
}
else
{
password+=ch;
if(show_asterisk)
cout <<'*';
}
}
cout << endl;
return password;
}
/*
Check existence of a specified file (by opening it).
*/
bool fileExists(const string& name)
{
ifstream f(name.c_str());
return f.is_open();
f.close();
}