Loading...
Searching...
No Matches
base64Layer.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2016-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "base64Layer.H"
29
30// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31
33//- The characters used for base-64
34static const unsigned char base64Chars[64] =
35{
36 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
37 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
38 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
39 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
40 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
41 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
42 'w', 'x', 'y', 'z', '0', '1', '2', '3',
43 '4', '5', '6', '7', '8', '9', '+', '/'
44};
46
47
48// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
49
50inline unsigned char Foam::base64Layer::encode0() const noexcept
51{
52 // Top 6 bits of char0
53 return base64Chars[((group_[0] & 0xFC) >> 2)];
54}
55
56inline unsigned char Foam::base64Layer::encode1() const noexcept
57{
58 // Bottom 2 bits of char0, Top 4 bits of char1
59 return base64Chars[((group_[0] & 0x03) << 4) | ((group_[1] & 0xF0) >> 4)];
60}
61
62inline unsigned char Foam::base64Layer::encode2() const noexcept
63{
64 // Bottom 4 bits of char1, Top 2 bits of char2
65 return base64Chars[((group_[1] & 0x0F) << 2) | ((group_[2] & 0xC0) >> 6)];
66}
67
68inline unsigned char Foam::base64Layer::encode3() const noexcept
69{
70 // Bottom 6 bits of char2
71 return base64Chars[(group_[2] & 0x3F)];
72}
73
74
75inline void Foam::base64Layer::add_char(char c)
76{
77 group_[groupLen_++] = static_cast<unsigned char>(c);
78
79 if (groupLen_ == 3)
80 {
81 unsigned char out[4];
82
83 out[0] = encode0();
84 out[1] = encode1();
85 out[2] = encode2();
86 out[3] = encode3();
87 os_.write(reinterpret_cast<char*>(out), 4);
88
89 groupLen_ = 0;
90 }
92
93
94// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
95
96void Foam::base64Layer::add(char c)
97{
98 add_char(c);
100
101
102// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
103
105:
106 os_(os),
107 groupLen_(0)
109
110
111// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
112
114{
115 close();
117
118
119// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120
121void Foam::base64Layer::write(const char* s, std::streamsize n)
122{
123 for (std::streamsize i=0; i < n; ++i)
124 {
125 add_char(s[i]);
126 }
127}
128
129
132 groupLen_ = 0;
133}
134
135
137{
138 bool had_pending = false;
139
140 unsigned char out[4];
141 if (groupLen_ == 1)
142 {
143 had_pending = true;
144 group_[1] = 0;
145
146 out[0] = encode0();
147 out[1] = encode1();
148 out[2] = '=';
149 out[3] = '=';
150 }
151 else if (groupLen_ == 2)
152 {
153 had_pending = true;
154 group_[2] = 0;
155
156 out[0] = encode0();
157 out[1] = encode1();
158 out[2] = encode2();
159 out[3] = '=';
160 }
161
162 // group-length == 0 (no content)
163 // group-length == 3 is not possible, already reset in add()
164 groupLen_ = 0;
165
166 if (had_pending)
167 {
168 os_.write(reinterpret_cast<char*>(out), 4);
169 }
170
171 return had_pending;
172}
173
174
175// ************************************************************************* //
label n
void reset() noexcept
Restart a new encoding sequence.
void add(char c)
Add a character to the group, outputting when the group is full.
Definition base64Layer.C:91
bool close()
End the encoding sequence, padding the final characters with '='.
base64Layer(std::ostream &os) noexcept
Attach to an output stream.
Definition base64Layer.C:99
~base64Layer()
Destructor. Performs close().
void write(const char *s, std::streamsize n)
Encode the character sequence, writing when possible.
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
const dimensionedScalar c
Speed of light in a vacuum.
const direction noexcept
Definition scalarImpl.H:265