foamVtkFormatter.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-2018 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#include "foamVtkFormatter.H"
28
29// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30
32{
33 if (!inTag_)
34 {
36 << "xml attribute '" << k << "' but not inside a tag!" << endl;
37 }
38
39 return inTag_;
40}
41
42
43bool Foam::vtk::formatter::canWriteToplevel(const char* what) const
44{
45 if (inTag_)
46 {
48 << "Cannot add " << what << " inside a tag!"
49 << endl;
50 }
51
52 return !inTag_;
53}
54
55
56// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
57
59{
60 quote_ = quote;
61}
62
63
64uint64_t Foam::vtk::formatter::offset(const uint64_t)
65{
66 return formatter::npos;
67}
68
69
70std::size_t Foam::vtk::formatter::encodedLength(std::size_t n) const
71{
72 return n;
73}
74
75
77{
78 if (inTag_)
79 {
81 << "open xml tag '" << tagName << "', but already within a tag!"
82 << endl;
83
84 return false;
85 }
86
87 // Emit, before changing the stack or the state.
88 indent();
89 os_ << '<' << tagName;
90
91 // Add to the stack and change the state.
92 xmlTags_.append(tagName);
93 inTag_ = true;
94
95 return true;
96}
97
98
100{
101 if (!inTag_)
102 {
104 << "attempt to close xml tag, but not within a tag!"
105 << endl;
106 }
107 else
108 {
109 // Change the state
110 inTag_ = false;
111
112 if (isEmpty)
113 {
114 // Eg, <tag ... />
115 xmlTags_.remove();
116 os_ << " /";
117 }
118 os_ << '>' << nl;
119 }
120
121 return *this;
122}
123
124
125Foam::vtk::formatter& Foam::vtk::formatter::endTag(const word& tagName)
126{
127 const word curr(xmlTags_.remove());
128 indent();
129
130 if (inTag_)
131 {
132 WarningInFunction
133 << "adding xml endTag '" << curr
134 << "' but already in another tag!"
135 << endl;
136
137 // Also suppress further output, or not?
138 }
139
140 // Verify expected end tag
141 if (!tagName.empty() && tagName != curr)
142 {
143 WarningInFunction
144 << "expecting to end xml tag '" << tagName
145 << "' but found '" << curr << "' instead"
146 << endl;
147 }
148
149 os_ << "</" << curr << '>' << nl;
150
151 inTag_ = false;
152
153 return *this;
154}
155
156
157Foam::vtk::formatter& Foam::vtk::formatter::beginVTKFile
158(
159 const word& contentType,
160 const word& contentVersion,
161 const bool leaveOpen
162)
163{
164 openTag(vtk::fileTag::VTK_FILE);
165 xmlAttr("type", contentType);
166 xmlAttr("version", contentVersion);
167 xmlAttr("byte_order", vtkPTraits<Foam::endian>::typeName);
168 xmlAttr("header_type", vtkPTraits<headerType>::typeName);
169 closeTag();
170
171 openTag(contentType);
172 if (!leaveOpen)
173 {
174 closeTag();
175 }
176
177 return *this;
178}
179
180
181Foam::vtk::formatter& Foam::vtk::formatter::beginAppendedData()
182{
183 openTag("AppendedData");
184 xmlAttr("encoding", encoding());
185 closeTag();
186 os_ << '_';
187
188 return *this;
189}
190
191
192Foam::vtk::formatter& Foam::vtk::formatter::endAppendedData()
193{
194 flush(); // Flush any pending encoded content
195 os_ << nl; // Ensure clear separation from content
196 return endTag("AppendedData");
197}
198
199
200Foam::vtk::formatter& Foam::vtk::formatter::beginBlock
201(
202 label index,
203 std::string name
204)
205{
206 openTag(vtk::fileTag::BLOCK);
207 if (index >= 0)
208 {
209 xmlAttr("index", index);
210 }
211 if (name.size())
212 {
213 xmlAttr("name", name);
214 }
215 closeTag();
216
217 return *this;
218}
219
220
221Foam::vtk::formatter& Foam::vtk::formatter::beginPiece
222(
223 label index,
224 std::string name
225)
226{
227 openTag(vtk::fileTag::PIECE);
228 if (index >= 0)
229 {
230 xmlAttr("index", index);
231 }
232 if (name.size())
233 {
234 xmlAttr("name", name);
235 }
236 closeTag();
237
238 return *this;
239}
240
241
242Foam::vtk::formatter& Foam::vtk::formatter::DataSet
243(
244 label index,
245 std::string file,
246 bool autoName
247)
248{
249 openTag(vtk::fileTag::DATA_SET);
250
251 if (index >= 0)
252 {
253 xmlAttr("index", index);
254 }
255 if (file.size())
256 {
257 if (autoName)
258 {
259 xmlAttr("name", fileName::nameLessExt(file));
260 }
261 xmlAttr("file", file);
262 }
263 closeTag(true); // Empty tag. ie, <DataSet ... />
264
265 return *this;
266}
267
268
269Foam::vtk::formatter& Foam::vtk::formatter::DataSet
270(
271 label index,
272 std::string file,
273 std::string name
274)
275{
276 openTag(vtk::fileTag::DATA_SET);
277
278 if (index >= 0)
279 {
280 xmlAttr("index", index);
281 }
282 if (name.size())
283 {
284 xmlAttr("name", name);
285 }
286 if (file.size())
287 {
288 xmlAttr("file", file);
289 }
290 closeTag(true); // Empty tag. ie, <DataSet ... />
291
292 return *this;
293}
294
295
296Foam::vtk::formatter& Foam::vtk::formatter::writeTimeValue(scalar timeValue)
297{
298 // Emit "TimeValue" as FieldData
299 // NumberOfTuples="1" (required!)
300
301 uint64_t payLoad = vtk::sizeofData<float>(1);
302
303 beginDataArray<float,1,1>("TimeValue");
304 writeSize(payLoad);
305
306 write(timeValue);
307 flush();
308
309 endDataArray();
310
311 return *this;
312}
313
314
315// ************************************************************************* //
label k
label n
const vector & offset() const noexcept
Offset vector (from patch faces to destination mesh objects)
Abstract class for a VTK output stream formatter.
bool inTag_
Tag open/closed/ended state.
quoteChar
Quoting for XML attributes.
bool canWriteToplevel(const char *what) const
bool canWriteAttr(const word &k) const
static constexpr uint64_t npos
Out of range position or size.
formatter & closeTag(const bool isEmpty=false)
Finish an XML tag, optional as an empty container.
virtual std::size_t encodedLength(std::size_t n) const
The encoded length for binary output is pass-through.
void quoting(const quoteChar quote)
Change quoting char for XML attributes (default: SINGLE_QUOTE)
bool openTagImpl(const word &tagName)
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define WarningInFunction
Report a warning using Foam::Warning.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:342