Orfeo Toolbox  3.16
itkFEMLightObject.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkFEMLightObject.cxx,v $
5  Language: C++
6  Date: $Date: 2009-01-29 21:55:14 $
7  Version: $Revision: 1.19 $
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  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 // disable debug warnings in MS compiler
19 #ifdef _MSC_VER
20 #pragma warning(disable: 4786)
21 #endif
22 
23 #include "itkFEMLightObject.h"
24 #include "itkNumericTraits.h"
25 
26 namespace itk {
27 namespace fem {
28 
33 void FEMLightObject::Read( std::istream& f, void* )
34 {
35  int n;
36 
38  this->SkipWhiteSpace(f); f>>n; if(!f) { goto out; }
39  this->GN=n;
40 
41  out:
42 
43  if( !f )
44  {
45  throw FEMExceptionIO(__FILE__,__LINE__,"FEMLightObject::Read","Error reading FEM object!");
46  }
47 }
48 
57 void FEMLightObject::Write( std::ostream& f ) const
58 {
59  // first write the class name
60  f<<'<'<<FEMObjectFactory<Self>::ID2ClassName(this->ClassID())<<">\n";
61 
62  // then the global object number
63  f<<"\t"<<GN<<"\t% Global object number\n";
64 
65  // check for errors
66  if (!f)
67  {
68  throw FEMExceptionIO(__FILE__,__LINE__,"FEMLightObject::Write","Error writing FEM object!");
69  }
70 }
71 
77 CreateFromStream( std::istream& f, void *info )
78 {
79 
80 // local variables
81  std::streampos l(0);
82  char buf[256];
83  std::string s;
84  std::string::size_type b,e;
85  int clID;
87  std::string errorMessage;
88 
89 start:
90 #ifndef __sgi
91  l=f.tellg(); // remember the stream position
92 #endif
93  SkipWhiteSpace(f); // skip comments and whitespaces
94  if ( f.eof() ) return 0; // end of stream. all was good
95 
96  char c;
97  if ( (c = f.get()) != '<' )
98  {
99  std::string rest;
100  std::getline(f,rest);
101  errorMessage = "Expected < token not found. Instead found '";
102  errorMessage += c;
103  errorMessage += "'.\nRest of line is '";
104  errorMessage += rest;
105  errorMessage += "'.\n";
106  goto out; // we expect a token
107  }
108  f.getline(buf,256,'>'); // read up to 256 characters until '>' is reached. we read and discard the '>'
109  s=std::string(buf);
110 
111  // get rid of the whitespaces in front of and the back of token
112  b=s.find_first_not_of(whitespaces); // end of whitespaces in the beginning
113  if ( (e=s.find_first_of(whitespaces,b)) == std::string::npos ) // beginning of whitespaces at the end
114  {
115  e=s.size();
116  }
117  s=s.substr(b,e-b);
118 
119  if ( s=="END" )
120  {
121  /*
122  * We can ignore this token. Start again by reading the next object.
123  */
124  goto start;
125  }
126  clID=FEMOF::ClassName2ID(s); // obtain the class ID from FEMObjectFactory
127  if (clID<0)
128  {
129  errorMessage = "Could not obtain class ID from FEMObjectFactory for '";
130  errorMessage += s;
131  errorMessage += "'.";
132  goto out; // class not found
133  }
134  // create a new object of the correct class
135  a=FEMOF::Create(clID);
136  if (!a)
137  {
138  errorMessage = "Error creating new object of the derived class";
139  goto out; // error creating new object of the derived class
140  }
141  /*
142  * Now we have to read additional data, which is
143  * specific to the class of object we just created
144  */
145  try
146  {
147  a->Read(f,info);
148  }
153  catch (...)
154  {
155 #ifndef FEM_USE_SMART_POINTERS
156  delete a; // if something went wrong, we need to destroy the already created object
157 #endif
158  a=0;
159  throw; // rethrow the same exception
160  }
161 
167  if (a) { return a; }
168 
169  out:
170 
175 #ifndef __sgi
176  f.seekg(l);
177 #endif
178 
179  /*
180  * Throw an IO exception
181  */
182  throw FEMExceptionIO(__FILE__,__LINE__,"FEMLightObject::ReadAnyObjectFromStream()",errorMessage);
183 
184 }
185 
186 // Helper function to skip all whitespaces and comments in input stream
187 void
189 SkipWhiteSpace(std::istream& f)
190 {
191  std::string skip;
192  while(f && !f.eof() && (std::ws(f).peek()) == '%' )
193  {
194  std::getline(f,skip);
195  }
196 }
197 
198 // string containing all whitespace characters
199 const std::string
201 ::whitespaces=" \t\n\r";
202 
203 }} // end namespace itk::fem

Generated at Sat Feb 2 2013 23:36:46 for Orfeo Toolbox with doxygen 1.8.1.1