diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp index 4a1e4757e4..029a289966 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.cpp @@ -1,95 +1,98 @@ /*=================================================================== 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 #include #include #include +//for exceptions +#include "mitkIGTException.h" +#include "mitkIGTIOException.h" 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); - try - { - if (myFile.exists()) myFile.remove(); - } - catch(...) - { - MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!"; - } - } +{ + //remove temp directory + Poco::File myFile(m_tempDirectory); + 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; - } - + { + 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()); - } + { + //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; - } + } 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(); + { + 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; - } + 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 cd2ce9dd34..8924e80fe4 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.h +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageSerializer.h @@ -1,62 +1,61 @@ /*=================================================================== 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. + * @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 */ bool Serialize(std::string filename, mitk::NavigationToolStorage::Pointer storage); itkGetMacro(ErrorMessage,std::string); - protected: NavigationToolStorageSerializer(); ~NavigationToolStorageSerializer(); - std::string m_ErrorMessage; + std::string m_ErrorMessage; - std::string convertIntToString(int i); + std::string convertIntToString(int i); - std::string m_tempDirectory; + std::string m_tempDirectory; }; } // namespace mitk #endif //NAVIGATIONTOOLSTORAGESERIALIZER