From 17e060966e0b1191b8a86e020ca3cdaa7746a7ef Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 5 Mar 2003 00:22:41 +0000 Subject: [PATCH] prompt user on error --- pandatool/src/cvscopy/cvsCopy.cxx | 77 +++++++++++++++++++++++++++++-- pandatool/src/cvscopy/cvsCopy.h | 10 ++-- 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/pandatool/src/cvscopy/cvsCopy.cxx b/pandatool/src/cvscopy/cvsCopy.cxx index 0e8858953b..189ad9afab 100644 --- a/pandatool/src/cvscopy/cvsCopy.cxx +++ b/pandatool/src/cvscopy/cvsCopy.cxx @@ -32,6 +32,7 @@ CVSCopy() { _model_dirname = "."; _key_filename = "Sources.pp"; _cvs_binary = "cvs"; + _user_aborted = false; _model_dir = (CVSSourceDirectory *)NULL; _map_dir = (CVSSourceDirectory *)NULL; @@ -146,16 +147,52 @@ import(const Filename &source, void *extra_data, nout << "Copying " << basename << " to " << dir->get_path() << "\n"; if (!copy_file(source, dest, dir, extra_data, new_file)) { - return (CVSSourceDirectory *)NULL; - } - if (new_file) { - cvs_add(dest); + if (!continue_after_error()) { + return (CVSSourceDirectory *)NULL; + } + } else { + if (new_file) { + cvs_add(dest); + } } } return dir; } +//////////////////////////////////////////////////////////////////// +// Function: CVSCopy::continue_after_error +// Access: Public +// Description: Prompts the user (unless -f was specified) if he +// wants to continue the copy operation after some error +// has occurred. Returns true to continue, false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool CVSCopy:: +continue_after_error() { + if (_force) { + return true; + } + if (_user_aborted) { + return false; + } + + while (true) { + string result = prompt("Error occurred during copy! Continue (y/n)? "); + nassertr(!result.empty(), false); + if (result.size() == 1) { + if (tolower(result[0]) == 'y') { + return true; + } else if (tolower(result[0]) == 'n') { + _user_aborted = true; + return false; + } + } + + nout << "*** Invalid response: " << result << "\n\n"; + } +} + //////////////////////////////////////////////////////////////////// // Function: CVSCopy::handle_args @@ -467,3 +504,35 @@ scan_for_root(const string &dirname) { return scan_for_root(dirname + "/.."); } + +//////////////////////////////////////////////////////////////////// +// Function: CVSCopy::prompt +// Access: Private +// Description: Issues a prompt to the user and waits for a typed +// response. Returns the response (which will not be +// empty). +//////////////////////////////////////////////////////////////////// +string CVSCopy:: +prompt(const string &message) { + nout << flush; + while (true) { + cerr << message << flush; + string response; + getline(cin, response); + + // Remove leading and trailing whitespace. + size_t p = 0; + while (p < response.length() && isspace(response[p])) { + p++; + } + + size_t q = response.length(); + while (q > p && isspace(response[q - 1])) { + q--; + } + + if (q > p) { + return response.substr(p, q - p); + } + } +} diff --git a/pandatool/src/cvscopy/cvsCopy.h b/pandatool/src/cvscopy/cvsCopy.h index 3cc001ed9d..f5e3b5a9af 100644 --- a/pandatool/src/cvscopy/cvsCopy.h +++ b/pandatool/src/cvscopy/cvsCopy.h @@ -19,12 +19,12 @@ #ifndef CVSCOPY_H #define CVSCOPY_H -#include +#include "pandatoolbase.h" #include "cvsSourceTree.h" -#include -#include +#include "programBase.h" +#include "filename.h" //////////////////////////////////////////////////////////////////// // Class : CVSCopy @@ -41,6 +41,8 @@ public: import(const Filename &source, void *extra_data, CVSSourceDirectory *suggested_dir); + bool continue_after_error(); + protected: virtual bool handle_args(Args &args); virtual bool post_command_line(); @@ -63,6 +65,7 @@ protected: private: bool scan_hierarchy(); bool scan_for_root(const string &dirname); + string prompt(const string &message); protected: bool _force; @@ -76,6 +79,7 @@ protected: Filename _key_filename; bool _no_cvs; string _cvs_binary; + bool _user_aborted; typedef vector_string SourceFiles; SourceFiles _source_files;