Loading...
Searching...
No Matches
binaryUtils.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 "binaryUtils.hpp"
26
27namespace bitpit {
28
29namespace utils {
30
31namespace binary {
32
41void write(std::ostream &stream, const std::vector<bool> &container)
42{
43 size_t size = container.size();
44 write(stream, size);
45
46 std::copy(container.begin(), container.end(), std::ostream_iterator<char>(stream));
47}
48
57void write(std::ostream &stream, const std::string &string)
58{
59 size_t size = string.length() + 1;
60 write(stream, size);
61 write(stream, string.data(), size);
62}
63
72void read(std::istream &stream, std::vector<bool> &container)
73{
74 std::size_t size;
75 read(stream, size);
76
77 container.resize(size);
78 for (std::size_t n = 0; n < size; ++n) {
79 char value;
80 read(stream, value);
81 container[n] = value;
82 }
83}
84
93void read(std::istream &stream, std::vector<bool>::reference value)
94{
95 bool bool_value;
96 utils::binary::read(stream, bool_value);
97 value = bool_value;
98}
99
108void read(std::istream &stream, std::string &string)
109{
110 size_t size;
111 read(stream, size);
112 char * cstring = new char [size];
113 read(stream, cstring, size);
114 string = cstring;
115 delete[] cstring;
116}
117
118}
119
120}
121
122}
void write(std::ostream &stream, const std::vector< bool > &container)
void read(std::istream &stream, std::vector< bool > &container)
--- layout: doxygen_footer ---