PSCF v1.1
XdrFileOArchive.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 "XdrFileOArchive.h"
9#include <string.h>
10
11namespace Util
12{
13
14 /*
15 * Constructor.
16 */
18 : xdr_(),
19 filePtr_(0),
20 version_(0)
21 {}
22
23 /*
24 * Constructor.
25 */
27 : xdr_(),
28 filePtr_(0),
29 version_(0)
30 {
31 filePtr_ = fopen(filename.c_str(), "wb+");
32 if (filePtr_ == NULL) {
33 std::string msg = "Failure to open C file: ";
34 msg += filename.c_str();
35 UTIL_THROW(msg.c_str());
36 }
37 xdrstdio_create(&xdr_, filePtr_, XDR_ENCODE);
38 }
39
40 /*
41 * Destructor.
42 */
44 {}
45
46 /*
47 * Initialize if default constructed.
48 */
49 void XdrFileOArchive::init(FILE* file)
50 {
51 filePtr_ = file;
52 xdrstdio_create(&xdr_, filePtr_, XDR_ENCODE);
53 }
54
55 /*
56 * Save a std::string to a XdrFileOArchive.
57 */
58 template <>
59 void serialize(XdrFileOArchive& ar, std::string& data,
60 const unsigned int version)
61 {
62 static char* temp = 0;
63 static unsigned int tempsize = 0;
64
65 unsigned int size = data.size() + 1; // +1 for the '\0'
66 xdr_u_int(ar.xdrPtr(), &size);
67
68 if (temp) {
69 if (size > tempsize) {
70 tempsize = size;
71 delete [] temp;
72 temp = new char[tempsize];
73 }
74 } else {
75 tempsize = 256;
76 if (size > tempsize) {
77 tempsize = size;
78 }
79 temp = new char[tempsize];
80 }
81 strcpy(temp, data.c_str());
82 xdr_string(ar.xdrPtr(), &temp, size);
83 }
84
85}
Saving / output archive for binary XDR file.
XDR * xdrPtr()
Get a pointer to the enclosed XDR object.
XdrFileOArchive()
Constructor.
FILE * file()
Get the underlying ifstream by reference.
virtual ~XdrFileOArchive()
Destructor.
void init(FILE *file)
Associate with an open file and initialize.
#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