be more forging of user provided file names
authorDiane Trout <diane@caltech.edu>
Tue, 17 Oct 2006 23:21:33 +0000 (23:21 +0000)
committerDiane Trout <diane@caltech.edu>
Tue, 17 Oct 2006 23:21:33 +0000 (23:21 +0000)
ticket:192
It turned out my use of boost::filesystem::path could toss an exception
if there were spaces in the file name on OS X. I needed to do a bit more
work to construct a safe filename for saving analyses and motifs.

Also I discovered that runtime_error was not the root exception class,
std::exception was. So I adjusted my try/catch to catch that exception
in the hope that I can provide more meaningful error messages.

alg/mussa.cpp
qui/MussaWindow.cpp

index 6eb8e7c3f3cb88b2ecc950d368f0af330d5a3c4e..19591914e50beeb933cc196d550b11f6d5847ff7 100644 (file)
@@ -541,7 +541,6 @@ Mussa::nway()
 void
 Mussa::save(fs::path save_path)
 {
-  fs::path flp_filepath;
   fs::fstream save_file;
   ostringstream append_info;
   int dir_create_status;
@@ -572,8 +571,12 @@ Mussa::save(fs::path save_path)
   if (not fs::exists(save_path)) {
     fs::create_directory(save_path);
   }
+  
+  std::string basename = save_path.leaf(); 
+  fs::path museq(basename + ".museq", fs::native);
+  
   // save sequence and annots to a special mussa file
-  save_file.open(save_path / (save_path.leaf()+".museq"), ios::out);
+  save_file.open(save_path / museq, ios::out);
   save_file << "<Mussa_Sequence>" << endl;
 
   for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
@@ -586,18 +589,21 @@ Mussa::save(fs::path save_path)
 
   // if we have any motifs, save them.
   if (motif_sequences.size()) {
-    save_motifs(save_path/(save_path.leaf()+".mtl"));
+    fs::path mtl(basename + ".mtl", fs::native);
+    save_motifs(save_path / mtl);
   }
 
   // save nway paths to its mussa save file
-  the_paths.save(save_path / (save_path.leaf()+ ".muway"));
+  fs::path muway(basename + ".muway", fs::native);
+  the_paths.save(save_path / muway);
 
   for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++) {
     for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
     {
       append_info.str("");
       append_info <<  "_sp_" << i << "v" << i2;
-      all_comps[i][i2].save(save_path/(save_path.leaf()+append_info.str()+".flp"));
+      fs::path flp(basename+append_info.str()+".flp", fs::native);
+      all_comps[i][i2].save(save_path / flp);
     }
   }
 
index 2ab6299d925ac36226d56a69593b6bb13c8005e9..6733c338e83cffa145c531c163cb4ef915fab498 100644 (file)
@@ -349,7 +349,15 @@ void MussaWindow::saveAnalysis()
       // this doesn't work when a sequence changes something (like its
       // name)
       //else if (analysis->is_dirty())
-      analysis->save();
+      try { 
+        analysis->save();
+      } catch (std::exception e) {
+        QMessageBox::critical(this, 
+                              tr("Mussa Save Error"),
+                              tr(e.what()),
+                              QMessageBox::Ok, 0, 0);
+  }
+      
     }
   }
 }
@@ -370,23 +378,30 @@ void MussaWindow::saveAnalysisAs()
   if (fileNames.size() != 1) {
     return;
   }
-  fs::path save_path(fileNames[0].toStdString(), fs::native);
-  // do you want to overwrite?
-  if (fs::exists(save_path) and 
-      QMessageBox::question(
-        this,
-        tr("Overwrite File? -- Mussa"),
-        tr("A file called %1 already exists"
-           "do you want to overwrite it?")
-           .arg(fileNames[0]),
-        tr("&Yes"), tr("&No"),
-        QString(), 0, 1)
-      ) {
-    return;
+  try {
+    fs::path save_path(fileNames[0].toStdString(), fs::native);
+    // do you want to overwrite?
+    if (fs::exists(save_path) and 
+        QMessageBox::question(
+          this,
+          tr("Overwrite File? -- Mussa"),
+          tr("A file called %1 already exists"
+             "do you want to overwrite it?")
+             .arg(fileNames[0]),
+          tr("&Yes"), tr("&No"),
+          QString(), 0, 1)
+        ) {
+      return;
+    }
+    analysis->save(save_path);
+    fs::path normalized_path = (save_path / "..").normalize();   
+    default_dir->setPath(normalized_path.native_directory_string().c_str());
+  } catch (std::exception e) {
+    QMessageBox::critical(this, 
+                          tr("Mussa Save Error"),
+                          tr(e.what()),
+                          QMessageBox::Ok, 0, 0);
   }
-  analysis->save(save_path);
-  fs::path normalized_path = (save_path / "..").normalize();   
-  default_dir->setPath(normalized_path.native_directory_string().c_str());
 }
 
 bool MussaWindow::isClearingAnalysisSafe()
@@ -447,7 +462,7 @@ void MussaWindow::loadMotifList()
     analysis->load_motifs(converted_path);
     default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
     emit changedMotifs();
-  } catch (runtime_error e) {
+  } catch (std::exception e) {
     QString msg("Unable to load ");
     msg += path;
     msg += "\n";
@@ -472,11 +487,13 @@ void MussaWindow::saveMotifList()
     fs::path converted_path(path.toStdString(), fs::native);
     if (fs::extension(converted_path).size() == 0) {
       // no extension, so add one
-      converted_path = converted_path.string() + ".mtl";
+      fs::path base_path = converted_path.branch_path();
+      fs::path filename(converted_path.leaf() + ".mtl", fs::native);
+      converted_path = base_path / filename;
     }
     analysis->save_motifs(converted_path);
     default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
-  } catch (runtime_error e) {
+  } catch (std::exception e) {
     QString msg("Unable to save ");
     msg += path;
     msg += "\n";