xref: /haiku/src/bin/pkgman/DecisionProvider.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <stdio.h>
8 #include <string.h>
9 
10 #include "DecisionProvider.h"
11 
12 
13 bool
14 DecisionProvider::YesNoDecisionNeeded(const BString& description,
15 	const BString& question, const BString& yes, const BString& no,
16 	const BString& defaultChoice)
17 {
18 	if (description.Length() > 0)
19 		printf("%s\n", description.String());
20 
21 	bool haveDefault = defaultChoice.Length() > 0;
22 
23 	while (true) {
24 		printf("%s [%s/%s]%s: ", question.String(), yes.String(), no.String(),
25 			haveDefault
26 				? (BString(" (") << defaultChoice << ") ").String() : "");
27 
28 		char buffer[32];
29 		if (fgets(buffer, 32, stdin)) {
30 			if (haveDefault &&  (buffer[0] == '\n' || buffer[0] == '\0'))
31 				return defaultChoice == yes;
32 			int length = strlen(buffer);
33 			for (int i = 1; i <= length; ++i) {
34 				if (yes.ICompare(buffer, i) == 0) {
35 					if (no.ICompare(buffer, i) != 0)
36 						return true;
37 				} else if (no.Compare(buffer, i) == 0) {
38 					if (yes.ICompare(buffer, i) != 0)
39 						return false;
40 				} else
41 					break;
42 			}
43 			fprintf(stderr, "*** please enter '%s' or '%s'\n", yes.String(),
44 				no.String());
45 		}
46 	}
47 }
48