Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkExampleDataStructureReaderService.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 
18 
19 // mitk includes
20 #include "mitkExampleIOMimeTypes.h"
21 #include "mitkGeometry3D.h"
22 #include <mitkCustomMimeType.h>
23 
24 // itk includes
25 #include "itksys/SystemTools.hxx"
26 
27 namespace mitk
28 {
30  : mitk::AbstractFileReader(other)
31  {
32  }
33 
36  "Default reader for the example data structure")
37  {
38  m_ServiceReg = this->RegisterService();
39  }
40 
42  std::vector<itk::SmartPointer<BaseData>> ExampleDataStructureReaderService::Read()
43  {
44  std::vector<itk::SmartPointer<mitk::BaseData>> result;
45  std::string location = GetInputLocation();
46 
47  std::string ext = itksys::SystemTools::GetFilenameLastExtension(location);
48  ext = itksys::SystemTools::LowerCase(ext);
49 
50  if (location == "")
51  {
52  MITK_ERROR << "No file name specified.";
53  }
54  try
55  {
56  std::ifstream file(location);
57  std::string content("");
58  std::string line("");
59  if (file.is_open())
60  {
61  while (getline(file, line))
62  {
63  content += line;
64  content += "\n";
65  }
66  }
67  else
68  {
69  mitkThrow() << "Could not open file " << this->GetInputLocation() << " for reading.";
70  }
71 
73  outputData->SetData(content);
74  result.push_back(outputData.GetPointer());
75  MITK_INFO << "Example file read";
76  }
77 
78  catch (mitk::Exception e)
79  {
80  MITK_ERROR << e.GetDescription();
81  }
82  catch (...)
83  {
84  MITK_ERROR << "Unknown error occurred while trying to read file.";
85  }
86 
87  return result;
88  }
89 
90 } // namespace MITK
91 
92 mitk::ExampleDataStructureReaderService *mitk::ExampleDataStructureReaderService::Clone() const
93 {
94  return new ExampleDataStructureReaderService(*this);
95 }
static char * line
Definition: svm.cpp:2884
virtual std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
#define mitkThrow()
The reader service for the MITK example data type.
Base class for creating mitk::BaseData objects from files or streams.
virtual std::string GetInputLocation() const override
Get the current input location.