blob: 3f358fa14bc08b43b51eec953eaf01daa8f5248d (
plain) (
blame)
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
|
#include "spm.h"
/**
* Basic case-insensitive interactive choice function
* @param answer
* @param answer_default
* @return
*/
int spm_user_yesno(int answer, int empty_input_is_yes) {
int result = 0;
answer = tolower(answer);
if (answer == 'y') {
result = 1;
} else if (answer == 'n') {
result = 0;
} else {
if (empty_input_is_yes) {
result = 1;
} else {
result = -1;
}
}
return result;
}
int spm_prompt_user(const char *msg, int empty_input_is_yes) {
int user_choice = 0;
int status_choice = 0;
char ch_yes = 'y';
char ch_no = 'n';
if (empty_input_is_yes) {
ch_yes = 'Y';
} else {
ch_no = 'N';
}
printf("\n%s [%c/%c] ", msg, ch_yes, ch_no);
while ((user_choice = getchar())) {
status_choice = spm_user_yesno(user_choice, 1);
if (status_choice == 0) { // No
break;
} else if (status_choice == 1) { // Yes
break;
} else { // Only triggers when spm_user_yesno's second argument is zero
puts("Please answer 'y' or 'n'...");
}
}
puts("");
return status_choice;
}
void spm_user_yesno_test() {
int choice;
int status;
while ((choice = getchar())) {
status = spm_user_yesno(choice, 1);
if (status == -1) {
puts("Please answer Y or N");
continue;
}
else if (status == 0) {
puts("You answered no");
break;
}
else if (status == 1) {
puts("You answered yes");
break;
}
}
}
|