Medical Imaging Interaction Toolkit  2018.4.99-f51274ea
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 (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 
14 
15 // mitk includes
16 #include "mitkExampleIOMimeTypes.h"
17 #include "mitkGeometry3D.h"
18 #include <mitkCustomMimeType.h>
19 
20 // itk includes
21 #include "itksys/SystemTools.hxx"
22 
23 namespace mitk
24 {
26  : mitk::AbstractFileReader(other)
27  {
28  }
29 
32  "Default reader for the example data structure")
33  {
34  m_ServiceReg = this->RegisterService();
35  }
36 
38  std::vector<itk::SmartPointer<BaseData>> ExampleDataStructureReaderService::DoRead()
39  {
40  std::vector<itk::SmartPointer<mitk::BaseData>> result;
41  std::string location = GetInputLocation();
42 
43  std::string ext = itksys::SystemTools::GetFilenameLastExtension(location);
44  ext = itksys::SystemTools::LowerCase(ext);
45 
46  if (location == "")
47  {
48  MITK_ERROR << "No file name specified.";
49  }
50  try
51  {
52  std::ifstream file(location);
53  std::string content("");
54  std::string line("");
55  if (file.is_open())
56  {
57  while (getline(file, line))
58  {
59  content += line;
60  content += "\n";
61  }
62  }
63  else
64  {
65  mitkThrow() << "Could not open file " << this->GetInputLocation() << " for reading.";
66  }
67 
69  outputData->SetData(content);
70  result.push_back(outputData.GetPointer());
71  MITK_INFO << "Example file read";
72  }
73 
74  catch (const mitk::Exception& e)
75  {
76  MITK_ERROR << e.GetDescription();
77  }
78  catch (...)
79  {
80  MITK_ERROR << "Unknown error occurred while trying to read file.";
81  }
82 
83  return result;
84  }
85 
86 } // namespace MITK
87 
88 mitk::ExampleDataStructureReaderService *mitk::ExampleDataStructureReaderService::Clone() const
89 {
90  return new ExampleDataStructureReaderService(*this);
91 }
static char * line
Definition: svm.cpp:2870
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
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&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define mitkThrow()
std::vector< itk::SmartPointer< BaseData > > DoRead() override
The reader service for the MITK example data type.
Base class for creating mitk::BaseData objects from files or streams.
std::string GetInputLocation() const override
Get the current input location.