File: itkFEMLightObject.cxx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80,672 kB
  • ctags: 85,253
  • sloc: cpp: 458,133; ansic: 196,222; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 428; csh: 220; perl: 193; xml: 20
file content (203 lines) | stat: -rw-r--r-- 5,282 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkFEMLightObject.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

// disable debug warnings in MS compiler
#ifdef _MSC_VER
#pragma warning(disable: 4786)
#endif

#include "itkFEMLightObject.h"
#include "itkNumericTraits.h"

namespace itk {
namespace fem {

/**
 * Here we just read the global number from the stream.
 * This should be the first function called when reading object data.
 */
void FEMLightObject::Read( std::istream& f, void* )
{
  int n;

  /** Read and set the global object number */
  this->SkipWhiteSpace(f); f>>n; if(!f) { goto out; }
  this->GN=n;

  out:

  if( !f )
    {
    throw FEMExceptionIO(__FILE__,__LINE__,"FEMLightObject::Read","Error reading FEM object!");
    }
}

/**
 * Here we just write the class name and GN.
 * This should be the first function called when writing object data, so
 * every derived class should first call the parent's write function.
 * The Write function in base (this one) class knows which class is
 * being written by calling the virtual ClassID() function and can write
 * the class name properly.
 */
void FEMLightObject::Write( std::ostream& f ) const
{
  // first write the class name
  f<<'<'<<FEMObjectFactory<Self>::ID2ClassName(this->ClassID())<<">\n";

  // then the global object number
  f<<"\t"<<GN<<"\t% Global object number\n";

  // check for errors
  if (!f)
    {
    throw FEMExceptionIO(__FILE__,__LINE__,"FEMLightObject::Write","Error writing FEM object!");
    }
}

/**
 * Read and create object of any derived class from stream
 */
FEMLightObject::Pointer
FEMLightObject::
CreateFromStream( std::istream& f, void *info )
{

// local variables
  std::streampos l(0);
  char buf[256];
  std::string s;
  std::string::size_type b,e;
  int clID;
  FEMLightObject::Pointer a=0;
  std::string errorMessage;

start:
#ifndef __sgi
  l=f.tellg();    // remember the stream position
#endif
  SkipWhiteSpace(f);      // skip comments and whitespaces
  if ( f.eof() ) return 0; // end of stream. all was good

  char c;
  if ( (c = f.get()) != '<' )
    {
    std::string rest;
    std::getline(f,rest);
    errorMessage = "Expected < token not found. Instead found '";
    errorMessage += c;
    errorMessage += "'.\nRest of line is '";
    errorMessage += rest;
    errorMessage += "'.\n";
    goto out; // we expect a token
    }
  f.getline(buf,256,'>');  // read up to 256 characters until '>' is reached. we read and discard the '>'
  s=std::string(buf);

  // get rid of the whitespaces in front of and the back of token
  b=s.find_first_not_of(whitespaces); // end of whitespaces in the beginning 
  if ( (e=s.find_first_of(whitespaces,b)) == std::string::npos )  // beginning of whitespaces at the end
    {
    e=s.size();
    }
  s=s.substr(b,e-b);

  if ( s=="END" )
    {
    /*
     * We can ignore this token. Start again by reading the next object.
     */
    goto start;
    }
  clID=FEMOF::ClassName2ID(s);  // obtain the class ID from FEMObjectFactory
  if (clID<0)
    {
    errorMessage = "Could not obtain class ID from FEMObjectFactory for '";
    errorMessage += s;
    errorMessage += "'.";
    goto out;  // class not found
    }
  // create a new object of the correct class
  a=FEMOF::Create(clID);
  if (!a)
    {
    errorMessage = "Error creating new object of the derived class";
    goto out;    // error creating new object of the derived class
    }
  /*
   * Now we have to read additional data, which is
   * specific to the class of object we just created
   */
  try
    {
    a->Read(f,info);
    }
  /**
   * Catch possible exceptions while 
   * reading object's data from stream
   */
  catch (...)
    {
#ifndef FEM_USE_SMART_POINTERS
    delete a;  // if something went wrong, we need to destroy the already created object
#endif
    a=0;
    throw;     // rethrow the same exception
    }

  /**
   * Return a pointer to a newly created object if all was OK
   * Technically everithing should be fine here (a!=0), but we
   * check again, just in case.
   */
  if (a) { return a; }

  out:

  /**
   * Something went wrong.
   * Reset the stream position to where it was before reading the object.
   */
#ifndef __sgi
  f.seekg(l);
#endif
  
  /*
   * Throw an IO exception
   */
  throw FEMExceptionIO(__FILE__,__LINE__,"FEMLightObject::ReadAnyObjectFromStream()",errorMessage);

}

// Helper function to skip all whitespaces and comments in input stream
void
FEMLightObject::
SkipWhiteSpace(std::istream& f)
{
  std::string skip;
  while(f && !f.eof() && (std::ws(f).peek()) == '%' )
    {
    std::getline(f,skip);
    }
}

// string containing all whitespace characters
const std::string
FEMLightObject
::whitespaces=" \t\n\r";

}} // end namespace itk::fem