PSCF v1.1
TextFileIArchive.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 "TextFileIArchive.h"
9
10#include <vector>
11
12namespace Util
13{
14
15 /*
16 * Constructor.
17 */
19 : filePtr_(0),
20 version_(0),
21 createdFile_(true)
22 { filePtr_ = new std::ifstream(); }
23
24 /*
25 * Constructor.
26 */
28 : filePtr_(0),
29 version_(0),
30 createdFile_(true)
31 { filePtr_ = new std::ifstream(filename.c_str()); }
32
33
34 /*
35 * Constructor.
36 */
38 : filePtr_(&file),
39 version_(0),
40 createdFile_(false)
41 {
42 if (!file.is_open()) {
43 UTIL_THROW("File not open");
44 }
45 }
46
47
48 /*
49 * Destructor.
50 */
52 {
53 if (filePtr_ && createdFile_) {
54 delete filePtr_;
55 }
56 }
57
58 /*
59 * Return underlying file by reference.
60 */
61 std::ifstream& TextFileIArchive::file()
62 { return *filePtr_; }
63
64 /*
65 * Load a std::string from TextFileIArchive.
66 */
67 template <>
68 void serialize(TextFileIArchive& ar, std::string& data,
69 const unsigned int version)
70 {
71 size_t size;
72 ar.unpack(size);
73 if (size > 0) {
74 static std::vector<char> charvec;
75 if (size > charvec.capacity()) {
76 charvec.reserve(size + 8);
77 }
78 // Read endline character after size
79 char endline;
80 ar.unpack(endline);
81 // Read actual string.
82 ar.unpack(&charvec[0], size);
83 data = &charvec[0];
84 } else {
85 data = "";
86 }
87 }
88
89}
Loading archive for text istream.
TextFileIArchive()
Constructor.
std::ifstream & file()
Get the underlying ifstream by reference.
void unpack(T &data)
Load a single T object.
virtual ~TextFileIArchive()
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