-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.cc
More file actions
134 lines (121 loc) · 3.44 KB
/
utility.cc
File metadata and controls
134 lines (121 loc) · 3.44 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* utility.c
*
* Created on: Dec 31, 2012
* Author: cyberwizzard
*/
#include "utility.h"
/**
* Ask for an integer input.
* @param wnd The curses window to print the question in.
* @param q String holding the question to ask.
* @param ans A pointer to put the answer in.
* @param def The default value for the question, can be an illegal value to force the user to make a choice.
* @param min The minimum value to accept.
* @param max The maximum value to accept.
* @param step A step size to apply from the minimum value, for example increments of 10.
* @return False if the user pressed escape to abort the question.
*/
bool utility_ask_int(WINDOW *wnd, string q, int *ans, int def, int min, int max, int step) {
assert(wnd!=NULL);
assert(ans!=NULL);
assert(min<max);
assert(step > 0);
do {
wprintw(wnd, q.c_str());
wprintw(wnd, " [%i]: ", def);
wrefresh(wnd);
bool endline = false;
stringstream ss;
do {
// Read a keyboard press
int ch = getch();
// Correct the scan code for special keys like the arrow keys (consisting of 2 keystrokes)
if ( ch == 0 || ch == 224 )
ch = 256 + getch();
if(ch>='0'&&ch<='9') {
// Numeric entry
ss << (char)ch;
wprintw(wnd, "%c", ch);
wrefresh(wnd);
} else if(ch=='-') {
// Only allow a dash when the string buffer is empty
if(ss.str().length() == 0) {
ss << '-';
wprintw(wnd, "-");
wrefresh(wnd);
}
} else if(ch == 13 || ch == 10) {
// Enter, validate
int val = (ss.str().length()>0) ? strtol(ss.str().c_str(), NULL, 10) : def;
if(val < min || val > max) {
wprintw(wnd, "\nInvalid value %i: please enter a number between %i and %i\n", val, min, max);
} else if((val-min)%step != 0) {
wprintw(wnd, "\nInvalid value %i: please enter a multiple of %i starting from %i\n", val, step, min);
} else {
// Assign value
*ans = val;
wprintw(wnd,"\n");
wrefresh(wnd);
return true;
}
endline = true;
} else if(ch == 27) {
// Escape, abort
wprintw(wnd, "abort\n");
wrefresh(wnd);
return false;
}
} while(!endline);
} while(1);
return false;
}
/**
* Ask for an yes or no answer.
* @param wnd The curses window to print the question in.
* @param q String holding the question to ask.
* @param ans A pointer to put the answer in, True for yes, False for no.
* @param def The default value for the question, 0 for yes, any other value for no
* @return False if the user pressed escape to abort the question.
*/
bool utility_ask_bool(WINDOW *wnd, string q, bool *ans, int def) {
assert(wnd!=NULL);
assert(ans!=NULL);
do {
wprintw(wnd, q.c_str());
wprintw(wnd, " [%s]: ", (def ? "Y/n" : "y/N"));
wrefresh(wnd);
do {
// Read a keyboard press
int ch = getch();
// Correct the scan code for special keys like the arrow keys (consisting of 2 keystrokes)
if ( ch == 0 || ch == 224 )
ch = 256 + getch();
if(ch=='y'||ch=='Y') {
// Yes
wprintw(wnd, "%c\n", ch);
wrefresh(wnd);
*ans = true;
return true;
} else if(ch=='n'||ch=='N') {
// No
wprintw(wnd, "%c\n", ch);
wrefresh(wnd);
*ans = false;
return true;
} else if(ch == 13 || ch == 10) {
// Enter, return default
wprintw(wnd, "%c\n", def ? 'Y' : 'N');
wrefresh(wnd);
*ans = def;
return true;
} else if(ch == 27) {
// Escape, abort
wprintw(wnd, "abort\n");
wrefresh(wnd);
return false;
}
} while(1);
} while(1);
return false;
}