diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.cpp b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.cpp index 0c68ef0ac6..6b67761c75 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.cpp +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.cpp @@ -1,110 +1,117 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-05-28 17:19:30 +0200 (Do, 28 Mai 2009) $ Version: $Revision $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ //Poco headers #include "Poco/Zip/Decompress.h" #include "Poco/Path.h" #include "Poco/File.h" #include "mitkNavigationToolStorageDeserializer.h" #include #include #include "mitkNavigationToolReader.h" //POCO #include mitk::NavigationToolStorageDeserializer::NavigationToolStorageDeserializer(mitk::DataStorage::Pointer dataStorage) { m_DataStorage = dataStorage; //create temp directory for this reader m_tempDirectory = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory() + Poco::Path::separator() + "tempNavigationToolDeserializer"; Poco::File myFile(m_tempDirectory); myFile.createDirectory(); } mitk::NavigationToolStorageDeserializer::~NavigationToolStorageDeserializer() { //remove temp directory Poco::File myFile(m_tempDirectory); - if (myFile.exists()) myFile.remove(true); + try + { + if (myFile.exists()) myFile.remove(); + } + catch(...) + { + MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!"; + } } mitk::NavigationToolStorage::Pointer mitk::NavigationToolStorageDeserializer::Deserialize(std::string filename) { bool success = false; //decomress zip file into temporary directory success = decomressFiles(filename,m_tempDirectory); //currently returns an empty storage in case of an error. TODO when exception handling is availiable in MITK: Throw an exception? if (!success) {return mitk::NavigationToolStorage::New();} //now read all files and convert them to navigation tools mitk::NavigationToolStorage::Pointer returnValue = mitk::NavigationToolStorage::New(m_DataStorage); bool cont = true; int i; for (i=0; cont==true; i++) { std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New(); mitk::NavigationTool::Pointer readTool = myReader->DoRead(fileName); if (readTool.IsNull()) cont = false; else returnValue->AddTool(readTool); //delete file std::remove(fileName.c_str()); } if(i==1) { m_ErrorMessage = "Error: did not find any tool. \n Is this a tool storage file?"; MITK_ERROR << "Error: did not find any tool. Is this a tool storage file?"; } return returnValue; } std::string mitk::NavigationToolStorageDeserializer::convertIntToString(int i) { std::string s; std::stringstream out; out << i; s = out.str(); return s; } bool mitk::NavigationToolStorageDeserializer::decomressFiles(std::string filename,std::string path) { std::ifstream file( filename.c_str(), std::ios::binary ); if (!file.good()) { m_ErrorMessage = "Cannot open '" + filename + "' for reading"; return false; } try { Poco::Zip::Decompress unzipper( file, Poco::Path( path ) ); unzipper.decompressAllFiles(); file.close(); } catch(Poco::IllegalStateException e) //temporary solution: replace this by defined exception handling later! { m_ErrorMessage = "Error: wrong file format! \n (please only load tool storage files)"; MITK_ERROR << "Error: wrong file format! (please only load tool storage files)"; return false; } return true; - } + } \ No newline at end of file diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp index 9ba5328b4c..7eed30687d 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp @@ -1,89 +1,96 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-05-28 17:19:30 +0200 (Do, 28 Mai 2009) $ Version: $Revision $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ //Poco headers #include "Poco/Zip/Compress.h" #include "Poco/Path.h" #include "Poco/File.h" #include "mitkNavigationToolStorageSerializer.h" #include "mitkNavigationToolWriter.h" #include #include #include #include mitk::NavigationToolStorageSerializer::NavigationToolStorageSerializer() { //create temp directory m_tempDirectory = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory() + Poco::Path::separator() + "tempNavigationToolSerializer"; Poco::File myFile(m_tempDirectory); myFile.createDirectory(); } mitk::NavigationToolStorageSerializer::~NavigationToolStorageSerializer() { //remove temp directory Poco::File myFile(m_tempDirectory); - if (myFile.exists()) myFile.remove(); + try + { + if (myFile.exists()) myFile.remove(); + } + catch(...) + { + MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!"; + } } bool mitk::NavigationToolStorageSerializer::Serialize(std::string filename, mitk::NavigationToolStorage::Pointer storage) { //save every tool to temp directory mitk::NavigationToolWriter::Pointer myToolWriter = mitk::NavigationToolWriter::New(); for(int i=0; iGetToolCount();i++) { std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; if (!myToolWriter->DoWrite(fileName,storage->GetTool(i))) return false; } //add all files to zip archive std::ofstream file( filename.c_str(), std::ios::binary | std::ios::out); if (!file.good()) { m_ErrorMessage = "Could not open a zip file for writing: '" + filename + "'"; for (int i=0; iGetToolCount();i++) { std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; std::remove(fileName.c_str()); } return false; } Poco::Zip::Compress zipper( file, true ); for (int i=0; iGetToolCount();i++) { std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; zipper.addFile(fileName,myToolWriter->GetFileWithoutPath(fileName)); //delete file: std::remove(fileName.c_str()); } zipper.close(); file.close(); return true; } std::string mitk::NavigationToolStorageSerializer::convertIntToString(int i) { std::string s; std::stringstream out; out << i; s = out.str(); return s; }