xref: /haiku/src/bin/pkgman/DecisionProvider.cpp (revision 83f63a216ee17213be3e9125c17f611bdb26e1b5)
17d7ed9bfSOliver Tappe /*
27d7ed9bfSOliver Tappe  * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
37d7ed9bfSOliver Tappe  * Distributed under the terms of the MIT License.
47d7ed9bfSOliver Tappe  */
57d7ed9bfSOliver Tappe 
67d7ed9bfSOliver Tappe 
77d7ed9bfSOliver Tappe #include <stdio.h>
87d7ed9bfSOliver Tappe #include <string.h>
97d7ed9bfSOliver Tappe 
107d7ed9bfSOliver Tappe #include "DecisionProvider.h"
117d7ed9bfSOliver Tappe 
127d7ed9bfSOliver Tappe 
DecisionProvider(bool interactive)13*83f63a21SIngo Weinhold DecisionProvider::DecisionProvider(bool interactive)
14*83f63a21SIngo Weinhold 	:
15*83f63a21SIngo Weinhold 	fInteractive(interactive)
16*83f63a21SIngo Weinhold {
17*83f63a21SIngo Weinhold }
18*83f63a21SIngo Weinhold 
19*83f63a21SIngo Weinhold 
207d7ed9bfSOliver Tappe bool
YesNoDecisionNeeded(const BString & description,const BString & question,const BString & yes,const BString & no,const BString & defaultChoice)217d7ed9bfSOliver Tappe DecisionProvider::YesNoDecisionNeeded(const BString& description,
227d7ed9bfSOliver Tappe 	const BString& question, const BString& yes, const BString& no,
237d7ed9bfSOliver Tappe 	const BString& defaultChoice)
247d7ed9bfSOliver Tappe {
257d7ed9bfSOliver Tappe 	if (description.Length() > 0)
267d7ed9bfSOliver Tappe 		printf("%s\n", description.String());
277d7ed9bfSOliver Tappe 
287d7ed9bfSOliver Tappe 	bool haveDefault = defaultChoice.Length() > 0;
297d7ed9bfSOliver Tappe 
307d7ed9bfSOliver Tappe 	while (true) {
317d7ed9bfSOliver Tappe 		printf("%s [%s/%s]%s: ", question.String(), yes.String(), no.String(),
327d7ed9bfSOliver Tappe 			haveDefault
337d7ed9bfSOliver Tappe 				? (BString(" (") << defaultChoice << ") ").String() : "");
347d7ed9bfSOliver Tappe 
35*83f63a21SIngo Weinhold 		if (!fInteractive) {
36*83f63a21SIngo Weinhold 			printf("%s\n", yes.String());
37*83f63a21SIngo Weinhold 			return true;
38*83f63a21SIngo Weinhold 		}
39*83f63a21SIngo Weinhold 
407d7ed9bfSOliver Tappe 		char buffer[32];
417d7ed9bfSOliver Tappe 		if (fgets(buffer, 32, stdin)) {
427d7ed9bfSOliver Tappe 			if (haveDefault &&  (buffer[0] == '\n' || buffer[0] == '\0'))
437d7ed9bfSOliver Tappe 				return defaultChoice == yes;
447d7ed9bfSOliver Tappe 			int length = strlen(buffer);
457d7ed9bfSOliver Tappe 			for (int i = 1; i <= length; ++i) {
467d7ed9bfSOliver Tappe 				if (yes.ICompare(buffer, i) == 0) {
477d7ed9bfSOliver Tappe 					if (no.ICompare(buffer, i) != 0)
487d7ed9bfSOliver Tappe 						return true;
497d7ed9bfSOliver Tappe 				} else if (no.Compare(buffer, i) == 0) {
507d7ed9bfSOliver Tappe 					if (yes.ICompare(buffer, i) != 0)
517d7ed9bfSOliver Tappe 						return false;
527d7ed9bfSOliver Tappe 				} else
537d7ed9bfSOliver Tappe 					break;
547d7ed9bfSOliver Tappe 			}
557d7ed9bfSOliver Tappe 			fprintf(stderr, "*** please enter '%s' or '%s'\n", yes.String(),
567d7ed9bfSOliver Tappe 				no.String());
577d7ed9bfSOliver Tappe 		}
587d7ed9bfSOliver Tappe 	}
597d7ed9bfSOliver Tappe }
60