From 803acc23f8cea3079681e9e624702e104adfd775 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 10 Oct 2015 06:12:31 +0200 Subject: Implement disfigure step in build command --- bpkg/utility.cxx | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'bpkg/utility.cxx') diff --git a/bpkg/utility.cxx b/bpkg/utility.cxx index 9b194df..6cf6c35 100644 --- a/bpkg/utility.cxx +++ b/bpkg/utility.cxx @@ -4,6 +4,7 @@ #include +#include // cout cin #include #include @@ -17,6 +18,46 @@ using namespace butl; namespace bpkg { bool + yn_prompt (const char* prompt, char def) + { + // Writing a robust Y/N prompt is more difficult than one would + // expect... + // + string a; + do + { + *diag_stream << prompt << ' '; + + // getline() will set the failbit if it failed to extract anything, + // not even the delimiter and eofbit if it reached eof before seeing + // the delimiter. + // + // + getline (cin, a); + + bool f (cin.fail ()); + bool e (cin.eof ()); + + if (f || e) + *diag_stream << endl; // Assume no delimiter (newline). + + if (f) + fail << "unable to read y/n answer from STDOUT"; + + if (a.empty () && def != '\0') + { + // Don't treat eof as the default answer. We need to see the + // actual newline. + // + if (!e) + a = def; + } + } while (a != "y" && a != "n"); + + return a == "y"; + } + + bool exists (const path& f) { try -- cgit v1.1