Orfeo Toolbox  3.16
itkBrains2HeaderBase.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkBrains2HeaderBase.cxx,v $
5  Language: C++
6  Date: $Date: 2009-04-05 10:56:47 $
7  Version: $Revision: 1.12 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  Portions of this code are covered under the VTK copyright.
13  See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
14 
15  This software is distributed WITHOUT ANY WARRANTY; without even
16  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17  PURPOSE. See the above copyright notices for more information.
18 
19 =========================================================================*/
20 #include "itkExceptionObject.h"
21 #include "itkBrains2HeaderBase.h"
23 #include <iostream>
24 #include <stdlib.h>
25 
26 namespace itk {
28 {
29  //Nothing to be done here.
30 }
32 {
33  this->ClearHeader();
34 }
35 
36 void Brains2HeaderBase::ReadBrains2Header(std::string filename)
37 {
38  std::ifstream local_InputStream;
39  local_InputStream.open( filename.c_str(), std::ios::in | std::ios::binary );
40  if( !local_InputStream )
41  {
42  // itk::itkExceptionMacro("Error opening image data file for reading.");
43  }
44  ReadBrains2Header(local_InputStream);
45  local_InputStream.close();
46 }
47 
49 {
50  //Clear out all children, While children exist do
51  while(m_Child.size() != 0)
52  {
53  //Get value of front of list
54  Brains2HeaderBase * pi=m_Child.front();
55  //remove value from list
56  m_Child.pop_front();
57  //delte value
58  delete pi;
59  }
60  this->clear();
61 }
62 
63 
64 void Brains2HeaderBase::WriteBrains2Header(std::string filename) const
65 {
66  std::ofstream local_OutputStream;
67  local_OutputStream.open( filename.c_str(), std::ios::out | std::ios::binary );
68  if( !local_OutputStream )
69  {
70  // itk::itkExceptionMacro("Error opening image data file for reading.");
71  }
72  WriteBrains2Header(local_OutputStream);
73  local_OutputStream.close();
74 }
75 
76 std::ifstream & Brains2HeaderBase::ReadBrains2Header(std::ifstream & inputstream)
77 {
78  std::string Key;
79  //NOTE: tellg returns the position in number of bytes from the begining of the stream.
80  const std::streampos FileStartPos = inputstream.tellg();
81  //NOTE: the >> operator on a stream reads "words" at a time.
82  inputstream >> Key; //The first word to read must be "IPL_HEADER_BEGIN", with no value
83  //std::cout << "Looking for " << this->GetHeaderBeginTag() <<" but found " << Key << std::endl;
84  if(Key.find(this->GetHeaderBeginTag()) == std::string::npos )
85  {
86  //Return the file to it's position before attempting read.
87  inputstream.seekg( FileStartPos );
88  return inputstream;
89  }
90  else
91  {
92  this->push_back(std::list< std::pair<std::string,std::string> >::value_type(Key,""));
93  }
94  itk::Brains2HeaderFactory MyBrains2HdrFac;
95  std::streampos PreKeyReadPosition = inputstream.tellg();
96  inputstream >> Key; //Read key that follows "IPL_HEADER_BEGIN"
97 //DEBUG: std::cerr << "ReadBrains2Header: GetHeaderEndTag is |" << this->GetHeaderEndTag() << "|" << std::endl;
98  while(Key != this->GetHeaderEndTag() ) //If key = "IPL_HEADER_END", then there is no value
99  {
100 //DEBUG: std::cerr << "ReadBrains2Header: Key is |" << Key << "|" << std::endl;
101  if(inputstream.eof() == true)
102  {
103  itkGenericExceptionMacro(<< "Unexpected end of file");
104  }
105  //Check for the case where the specific header type begins and ends
106  //i.e. MASK_HEADER_BEGIN and MASK_HEADER_END, or IMAGE_HEADER_BEGIN and IMAGE_HEADER_END
107  if ( Key.find("HEADER_BEGIN") != std::string::npos)
108  {
109  this->push_back(std::list< std::pair<std::string,std::string> >::value_type("--BEGIN_CHILD--",""));
110  //Rewind to befor the key.
111  inputstream.seekg( PreKeyReadPosition );
112  //Need Factory Here to produce proper factory based on Key.
113  this->m_Child.push_back(MyBrains2HdrFac.CreateBrains2HeaderReader(Key));
114  if(this->m_Child.back() == NULL)
115  {
116  //DEBUG: Throw error
117  return inputstream;
118  }
119  this->m_Child.back()->ReadBrains2Header(inputstream);
120  PreKeyReadPosition = inputstream.tellg();
121  inputstream >> Key;
122  continue;
123  }
124  std::string Value; //If key does not == "IPL_HEADER_END", then there must be a value
125  inputstream >> Value;
126  this->push_back(std::list< std::pair<std::string,std::string> >::value_type(Key,Value));
127  //Read Next Key
128  PreKeyReadPosition = inputstream.tellg();
129  inputstream >> Key;
130  }
131  //pusch back the end key
132  this->push_back(std::list< std::pair<std::string,std::string> >::value_type(Key,""));
133  return inputstream;
134 }
135 std::ofstream & Brains2HeaderBase::WriteBrains2Header(std::ofstream & outputstream) const
136 {
137  return outputstream;
138 }
139 void Brains2HeaderBase::PrintSelf(std::ostream &os) const
140 {
141  std::list<Brains2HeaderBase *>::const_iterator childiterator=this->m_Child.begin();
142  //For each element in internal list
143  for(std::list< std::pair<std::string,std::string> >::const_iterator pi=this->begin();
144  pi != this->end(); pi++)
145  {
146  if(pi->first == "--BEGIN_CHILD--")
147  {
148  //std::cout <<"Size of m_Child " << m_Child.size() << std::endl;
149  (*childiterator)->PrintSelf(os);
150  childiterator++;
151  continue;
152  }
153  //os << "Key " << pi->first << " Value " << pi->second << std::endl;
154  os << pi->first << " " << pi->second << std::endl;
155  }
156 }
157 
158 bool Brains2HeaderBase::DoesKeyExist(const std::string &KeyID) const
159 {
160  std::list<Brains2HeaderBase *>::const_iterator childiterator=this->m_Child.begin();
161  //For each element in internal list
162  for(std::list< std::pair<std::string,std::string> >::const_iterator pi=this->begin();
163  pi != this->end(); pi++)
164  {
165  if(pi->first == KeyID)
166  {
167  return true;
168  }
169  else if(pi->first == "--BEGIN_CHILD--")
170  {
171  if((*childiterator)->DoesKeyExist(KeyID) == true)
172  {
173  return true;
174  }
175  childiterator++;
176  continue;
177  }
178  }
179  //Return an empty string
180  return false;
181 }
182 
183 
184 std::string Brains2HeaderBase::getString(const std::string &KeyID) const
185 {
186  //this->PrintSelf(std::cout);
187  std::list<Brains2HeaderBase *>::const_iterator childiterator=this->m_Child.begin();
188  //For each element in internal list
189  for(std::list< std::pair<std::string,std::string> >::const_iterator pi=this->begin();
190  pi != this->end(); pi++)
191  {
192  if(pi->first == KeyID)
193  {
194  return pi->second;
195  }
196  else if(pi->first == "--BEGIN_CHILD--")
197  {
198  //std::cout <<"Size of m_Child " << m_Child.size() << std::endl;
199  std::string TempStringValue=(*childiterator)->getString(KeyID);
200  if(TempStringValue.length() != 0)
201  {
202  return TempStringValue;
203  }
204  childiterator++;
205  continue;
206  }
207  }
208  //Return an empty string
209  return std::string("");
210 }
211 
212 float Brains2HeaderBase::getFloat(const std::string &KeyID) const
213 {
214  std::string TempStringValue=this->getString(KeyID);
215  if(TempStringValue.length() != 0)
216  {
217  return static_cast< float >( atof( TempStringValue.c_str() ) );
218  }
219  return 0.0F;
220 }
221 
222 int Brains2HeaderBase::getInt(const std::string &KeyID) const
223 {
224  std::string TempStringValue=this->getString(KeyID);
225  if( static_cast< int >( TempStringValue.length() ) != 0 )
226  {
227  return atoi(TempStringValue.c_str());
228  }
229  return 0;
230 }
231 } //End namespace itk

Generated at Sat May 18 2013 23:31:53 for Orfeo Toolbox with doxygen 1.8.3.1