diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp index 9524f98a9c..2da85d35f9 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp @@ -1,99 +1,102 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ //Poco headers #include "Poco/Zip/Compress.h" #include "Poco/Path.h" #include "Poco/File.h" #include "mitkNavigationToolStorageSerializer.h" #include "mitkNavigationToolWriter.h" +#include "mitkIGTException.h" +#include "mitkIGTIOException.h" #include #include #include #include -//for exceptions -#include "mitkIGTException.h" -#include "mitkIGTIOException.h" + + mitk::NavigationToolStorageSerializer::NavigationToolStorageSerializer() { //create temp directory mitk::UIDGenerator myUIDGen = mitk::UIDGenerator("",16); m_tempDirectory = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory() + Poco::Path::separator() + "tempNavigationToolSerializer_" + myUIDGen.GetUID(); Poco::File myFile(m_tempDirectory); myFile.createDirectory(); } mitk::NavigationToolStorageSerializer::~NavigationToolStorageSerializer() { //remove temp directory Poco::File myFile(m_tempDirectory); try -{ - if (myFile.exists()) myFile.remove(true); -} -catch(...) -{ + { + if (myFile.exists()) myFile.remove(true); + } + 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; - } + { + std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; + if (!myToolWriter->DoWrite(fileName,storage->GetTool(i))) + { + mitkThrowException(mitk::IGTIOException) << "Could not write tool to tempory directory: " << filename; + } + } //add all files to zip archive std::ofstream file( filename.c_str(), std::ios::binary | std::ios::out); - if (!file.good()) - { - //Exception if cannot open a zip file for writing - mitkThrowException(mitk::IGTException)<<"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; - } + if (!file.good()) //test if the zip archive is ready for writing + { + //first: clean up + for (int i=0; iGetToolCount();i++) + { + std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; + std::remove(fileName.c_str()); + } + //then: throw an exception + mitkThrowException(mitk::IGTIOException) << "Could not open a file for writing: " << filename; + } 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()); - } + { + std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; + zipper.addFile(fileName,myToolWriter->GetFileWithoutPath(fileName)); + std::remove(fileName.c_str()); //delete file + } 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; } diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.h b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.h index 8924e80fe4..e1aa9389c9 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.h +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.h @@ -1,61 +1,68 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef NAVIGATIONTOOLSTORAGESERIALIZER_H_INCLUDED #define NAVIGATIONTOOLSTORAGESERIALIZER_H_INCLUDED //itk headers #include //mitk headers #include #include "mitkNavigationToolStorage.h" #include namespace mitk { /**Documentation * \brief This class offers methods to save an object of the class NavigationToolStorage * to the harddisc. * * \ingroup IGT */ class MitkIGT_EXPORT NavigationToolStorageSerializer : public itk::Object { public: mitkClassMacro(NavigationToolStorageSerializer,itk::Object); itkNewMacro(Self); /** * @brief Saves a mitk navigation tool storage to a file. - * @return Returns true if the file was saved successfully. False if not. - * @throws Throws mitk::IGTException if zip-file cannot be opened for writing + * @return Returns true always true since error handling was converted to exception handling. + * The return value is decrepated. Will be changed to void. + * @throw mitk::IGTIOException Throws an exception if the given filename cannot be opened for writing or + * if the temp directory is not accessible. */ bool Serialize(std::string filename, mitk::NavigationToolStorage::Pointer storage); + /** + * @brief This method is decrepated. Exceptions are used for error handling now! + * @return Returns always an empty string since error handling was converted to exception handling. + */ itkGetMacro(ErrorMessage,std::string); + protected: NavigationToolStorageSerializer(); ~NavigationToolStorageSerializer(); std::string m_ErrorMessage; std::string convertIntToString(int i); std::string m_tempDirectory; }; } // namespace mitk #endif //NAVIGATIONTOOLSTORAGESERIALIZER