Loading...
Searching...
No Matches
binary_archive.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include "bitpit_common.hpp"
26
27#include "binary_archive.hpp"
28
29namespace bitpit {
30
41const std::string BinaryArchive::EXTENSION_DEFAULT = "dat";
42
51std::string BinaryArchive::generatePath(const std::string &name, int block)
52{
53 return generatePath(name, EXTENSION_DEFAULT, block);
54}
55
65std::string BinaryArchive::generatePath(const std::string &name, const std::string &extension, int block)
66{
67 FileHandler fileHandler("", name, extension);
68 if (block >= 0) {
69 fileHandler.setParallel(true);
70 fileHandler.setBlock(block);
71 }
72
73 return fileHandler.getPath();
74}
75
80 : m_version(VERSION_UNDEFINED)
81{
82}
83
88{
89 close();
90}
91
102void BinaryArchive::open(const std::string &name, const std::string &extension,
103 ios_base::openmode mode, int block)
104{
105 if (is_open()) {
106 close();
107 }
108
109 m_path = generatePath(name, extension, block);
110 std::fstream::open(m_path.c_str(), std::ios::binary | mode);
111 if (!good()) {
112 throw std::runtime_error("Unable to open the binary archive \"" + getPath() + "\".");
113 }
114}
115
122{
123 return m_version;
124}
125
131std::string BinaryArchive::getHeader() const
132{
133 return m_header;
134}
135
141std::string BinaryArchive::getPath() const
142{
143 return m_path;
144}
145
162IBinaryArchive::IBinaryArchive(const std::string &name, int block)
163{
164 open(name, EXTENSION_DEFAULT, block);
165}
166
175IBinaryArchive::IBinaryArchive(const std::string &name, const std::string &extension, int block)
176{
177 open(name, extension, block);
178}
179
188void IBinaryArchive::open(const std::string &name, int block)
189{
190 open(name, EXTENSION_DEFAULT, block);
191}
192
202void IBinaryArchive::open(const std::string &name, const std::string &extension, int block)
203{
204 // Reset the header
205 m_header.clear();
206
207 // Open the stream
208 BinaryArchive::open(name, extension, std::ios::in | std::ios_base::binary, block);
209
210 // Read the header
211 std::vector<char> rawHeader(HEADER_SIZE);
212 utils::binary::read(*this, rawHeader.data(), rawHeader.size());
213 m_header = std::string(rawHeader.begin(), rawHeader.end());
214
215 // Read the version
216 utils::binary::read(*this, m_version);
217}
218
225{
226 return *this;
227}
228
237{
238 return (version == m_version);
239}
240
258OBinaryArchive::OBinaryArchive(const std::string &name, int version, int block)
259{
260 open(name, EXTENSION_DEFAULT, version, "", block);
261}
262
273OBinaryArchive::OBinaryArchive(const std::string &name, int version,
274 const std::string &header, int block)
275{
276 open(name, EXTENSION_DEFAULT, version, header, block);
277}
278
288OBinaryArchive::OBinaryArchive(const std::string &name, const std::string &extension,
289 int version, int block)
290{
291 open(name, extension, version, "", block);
292}
293
294
306OBinaryArchive::OBinaryArchive(const std::string &name, const std::string &extension,
307 int version, const std::string &header, int block)
308{
309 open(name, extension, version, header, block);
310}
311
321void OBinaryArchive::open(const std::string &name, int version, int block)
322{
323 open(name, EXTENSION_DEFAULT, version, "", block);
324}
325
337void OBinaryArchive::open(const std::string &name, int version,
338 const std::string &header, int block)
339{
340 open(name, EXTENSION_DEFAULT, version, header, block);
341}
342
353void OBinaryArchive::open(const std::string &name, const std::string &extension,
354 int version, int block)
355{
356 open(name, extension, version, "", block);
357}
358
371void OBinaryArchive::open(const std::string &name, const std::string &extension,
372 int version, const std::string &header, int block)
373{
374 // Open the stream
375 BinaryArchive::open(name, extension, std::ios::out | std::ios_base::binary, block);
376
377 // Write the header
378 //
379 // The header should be written using the write overload that takes in
380 // input characters, because the one that takes in input strings would
381 // write into the archive also the string length.
382 std::string archiveHeader(header);
383 archiveHeader.resize(HEADER_SIZE, ' ');
384 utils::binary::write(*this, archiveHeader.data(), archiveHeader.size());
385
386 // Write the version
387 utils::binary::write(*this, version);
388}
389
396{
397 return *this;
398}
399
400}
std::string getHeader() const
static std::string generatePath(const std::string &name, int block=-1)
void open(const std::string &name, const std::string &extension, ios_base::openmode mode, int block=-1)
std::string getPath() const
Creates file names and checks status.
std::string getPath() const
void setParallel(bool p_)
void setBlock(int b_)
bool checkVersion(int version)
void open(const std::string &name, int block=-1)
std::istream & getStream()
void open(const std::string &name, const int version, int block=-1)
std::ostream & getStream()
void write(std::ostream &stream, const std::vector< bool > &container)
void read(std::istream &stream, std::vector< bool > &container)
--- layout: doxygen_footer ---