Simpatico  v1.10
XdrFileIArchive.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "XdrFileIArchive.h"
9 #include <string.h>
10 
11 namespace Util
12 {
13 
14  /*
15  * Constructor.
16  */
18  : xdr_(),
19  filePtr_(0),
20  version_(0),
21  createdFile_(false)
22  {}
23 
24  /*
25  * Constructor.
26  */
27  XdrFileIArchive::XdrFileIArchive(std::string filename)
28  : xdr_(),
29  filePtr_(0),
30  version_(0),
31  createdFile_(true)
32  {
33  filePtr_ = fopen(filename.c_str(), "rb");
34  if (filePtr_ == NULL) {
35  std::string msg = "Failure to open C file: ";
36  msg += filename.c_str();
37  UTIL_THROW(msg.c_str());
38  }
39  xdrstdio_create(&xdr_, filePtr_, XDR_DECODE);
40  }
41 
42  /*
43  * Destructor.
44  */
46  {}
47 
48  /*
49  * Initialize if default constructed.
50  */
52  {
53  filePtr_ = file;
54  xdrstdio_create(&xdr_, filePtr_, XDR_DECODE);
55  }
56 
57  /*
58  * Load a std::string from a XdrFileIArchive.
59  */
60  template <>
61  void serialize(XdrFileIArchive& ar, std::string& data,
62  const unsigned int version)
63  {
64  static char* temp = 0;
65  static unsigned int tempsize = 0;
66 
67  unsigned int size;
68  xdr_u_int(ar.xdrPtr(), &size);
69 
70  if (temp) {
71  if (size > tempsize) {
72  tempsize = size;
73  delete [] temp;
74  temp = new char[tempsize];
75  }
76  } else {
77  tempsize = 256;
78  if (size > tempsize) {
79  tempsize = size;
80  }
81  temp = new char[tempsize];
82  }
83  xdr_string(ar.xdrPtr(), &temp, size);
84  data = temp;
85  }
86 
87 }
XDR * xdrPtr()
Get a pointer to the enclosed XDR object.
FILE * file()
Get the underlying file handle.
virtual ~XdrFileIArchive()
Destructor.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1
Loading / input archive for binary XDR file.
XdrFileIArchive()
Constructor.
void init(FILE *file)
Initialize by associating with an open file.