Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 //Poco headers
18 #include <Poco/Zip/Compress.h>
19 #include <Poco/Path.h>
20 #include <Poco/File.h>
21 
24 #include "mitkIGTException.h"
25 #include "mitkIGTIOException.h"
27 #include <mitkSceneIO.h>
28 #include <mitkIOUtil.h>
29 
30 #include <sstream>
31 
32 
33 
35 {
36  //create temp directory
37  m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageSerializerTmp_XXXXXX",mitk::IOUtil::GetProgramPath());
38 }
39 
41 {
42  //remove temp directory
43  Poco::File myFile(m_tempDirectory);
44  try
45  {
46  if (myFile.exists()) myFile.remove(true);
47  }
48  catch(...)
49  {
50  MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!";
51  }
52 }
53 
55 {
56  //save every tool to temp directory
58  for(int i=0; i<storage->GetToolCount();i++)
59  {
60  std::string tempFileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool";
61  if (!myToolWriter->DoWrite(tempFileName,storage->GetTool(i)))
62  {
63  mitkThrowException(mitk::IGTIOException) << "Could not write tool to tempory directory: " << tempFileName;
64  }
65  }
66  //add all files to zip archive
67  std::ofstream file( filename.c_str(), std::ios::binary | std::ios::out);
68  if (!file.good()) //test if the zip archive is ready for writing
69  {
70  //first: clean up
71  for (int i=0; i<storage->GetToolCount();i++)
72  {
73  std::string tempFileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool";
74  std::remove(tempFileName.c_str());
75  }
76  //then: throw an exception
77  mitkThrowException(mitk::IGTIOException) << "Could not open a file for writing: " << filename;
78  }
79  Poco::Zip::Compress zipper( file, true );
80  for (int i=0; i<storage->GetToolCount();i++)
81  {
82  std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool";
83  zipper.addFile(fileName,myToolWriter->GetFileWithoutPath(fileName));
84  std::remove(fileName.c_str()); //delete file
85  }
86  zipper.close();
87  file.close();
88 
89  return true;
90  }
91 
93  {
94  std::string s;
95  std::stringstream out;
96  out << i;
97  s = out.str();
98  return s;
99  }
itk::SmartPointer< Self > Pointer
#define MITK_ERROR
Definition: mitkLogMacros.h:24
bool Serialize(std::string filename, mitk::NavigationToolStorage::Pointer storage)
Saves a mitk navigation tool storage to a file.
An object of this class represents an exception of the MITK-IGT module which are releated to the inpu...
static const std::string filename
static std::string CreateTemporaryDirectory(const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:452
static std::string GetProgramPath()
Definition: mitkIOUtil.cpp:350
#define mitkThrowException(classname)