瀏覽代碼

prompt user on error

David Rose 23 年之前
父節點
當前提交
17e060966e
共有 2 個文件被更改,包括 80 次插入7 次删除
  1. 73 4
      pandatool/src/cvscopy/cvsCopy.cxx
  2. 7 3
      pandatool/src/cvscopy/cvsCopy.h

+ 73 - 4
pandatool/src/cvscopy/cvsCopy.cxx

@@ -32,6 +32,7 @@ CVSCopy() {
   _model_dirname = ".";
   _model_dirname = ".";
   _key_filename = "Sources.pp";
   _key_filename = "Sources.pp";
   _cvs_binary = "cvs";
   _cvs_binary = "cvs";
+  _user_aborted = false;
   _model_dir = (CVSSourceDirectory *)NULL;
   _model_dir = (CVSSourceDirectory *)NULL;
   _map_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";
     nout << "Copying " << basename << " to " << dir->get_path() << "\n";
 
 
     if (!copy_file(source, dest, dir, extra_data, new_file)) {
     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;
   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
 //     Function: CVSCopy::handle_args
@@ -467,3 +504,35 @@ scan_for_root(const string &dirname) {
 
 
   return scan_for_root(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);
+    }
+  }
+}

+ 7 - 3
pandatool/src/cvscopy/cvsCopy.h

@@ -19,12 +19,12 @@
 #ifndef CVSCOPY_H
 #ifndef CVSCOPY_H
 #define CVSCOPY_H
 #define CVSCOPY_H
 
 
-#include <pandatoolbase.h>
+#include "pandatoolbase.h"
 
 
 #include "cvsSourceTree.h"
 #include "cvsSourceTree.h"
 
 
-#include <programBase.h>
-#include <filename.h>
+#include "programBase.h"
+#include "filename.h"
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : CVSCopy
 //       Class : CVSCopy
@@ -41,6 +41,8 @@ public:
   import(const Filename &source, void *extra_data,
   import(const Filename &source, void *extra_data,
          CVSSourceDirectory *suggested_dir);
          CVSSourceDirectory *suggested_dir);
 
 
+  bool continue_after_error();
+
 protected:
 protected:
   virtual bool handle_args(Args &args);
   virtual bool handle_args(Args &args);
   virtual bool post_command_line();
   virtual bool post_command_line();
@@ -63,6 +65,7 @@ protected:
 private:
 private:
   bool scan_hierarchy();
   bool scan_hierarchy();
   bool scan_for_root(const string &dirname);
   bool scan_for_root(const string &dirname);
+  string prompt(const string &message);
 
 
 protected:
 protected:
   bool _force;
   bool _force;
@@ -76,6 +79,7 @@ protected:
   Filename _key_filename;
   Filename _key_filename;
   bool _no_cvs;
   bool _no_cvs;
   string _cvs_binary;
   string _cvs_binary;
+  bool _user_aborted;
 
 
   typedef vector_string SourceFiles;
   typedef vector_string SourceFiles;
   SourceFiles _source_files;
   SourceFiles _source_files;