Medical Imaging Interaction Toolkit  2018.4.99-1640525a
Medical Imaging Interaction Toolkit
mitkNavigationToolStorageSerializer.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
15 #include "mitkIGTException.h"
16 #include "mitkIGTIOException.h"
18 #include <mitkSceneIO.h>
19 #include <mitkIOUtil.h>
20 
21 // Poco
22 #include <Poco/Zip/Compress.h>
23 
24 //ITKsys
25 #include <itksys/SystemTools.hxx>
26 
27 #include <sstream>
28 
30 {
31  //create temp directory
32  m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageSerializerTmp_XXXXXX");
33 }
34 
36 {
37  //remove temp directory
38 
39  try
40  {
41  if (itksys::SystemTools::FileExists(m_tempDirectory.c_str())) std::remove(m_tempDirectory.c_str());
42  }
43  catch(...)
44  {
45  MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!";
46  }
47 }
48 
49 void mitk::NavigationToolStorageSerializer::Serialize(const std::string& filename, mitk::NavigationToolStorage::Pointer storage)
50 {
51  //save every tool to temp directory
52  mitk::NavigationToolWriter::Pointer myToolWriter = mitk::NavigationToolWriter::New();
53  for(unsigned int i=0; i<storage->GetToolCount();i++)
54  {
55  std::string tempFileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool";
56  if (!myToolWriter->DoWrite(tempFileName,storage->GetTool(i)))
57  {
58  mitkThrowException(mitk::IGTIOException) << "Could not write tool to tempory directory: " << tempFileName;
59  }
60  }
61  //add all files to zip archive
62  std::ofstream file( filename.c_str(), std::ios::binary | std::ios::out);
63  if (!file.good()) //test if the zip archive is ready for writing
64  {
65  //first: clean up
66  for (unsigned int i=0; i<storage->GetToolCount();i++)
67  {
68  std::string tempFileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool";
69  std::remove(tempFileName.c_str());
70  }
71  //then: throw an exception
72  mitkThrowException(mitk::IGTIOException) << "Could not open a file for writing: " << filename;
73  }
74  Poco::Zip::Compress zipper( file, true );
75  for (unsigned int i=0; i<storage->GetToolCount();i++)
76  {
77  std::string fileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool";
78  zipper.addFile(fileName, myToolWriter->GetFileWithoutPath(fileName));
79  std::remove(fileName.c_str()); //delete file
80  }
81  zipper.close();
82  file.close();
83 }
84 
86  {
87  std::string s;
88  std::stringstream out;
89  out << i;
90  s = out.str();
91  return s;
92  }
static char GetDirectorySeparator()
Definition: mitkIOUtil.cpp:369
#define MITK_ERROR
Definition: mitkLogMacros.h:20
An object of this class represents an exception of the MITK-IGT module which are releated to the inpu...
static std::string CreateTemporaryDirectory(const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:458
#define mitkThrowException(classname)
void Serialize(const std::string &filename, mitk::NavigationToolStorage::Pointer storage)
Saves a mitk navigation tool storage to a file.