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-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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 
31 bool Foam::vtk::formatter::canWriteAttr(const word& k) const
32 {
33  if (!inTag_)
34  {
36  << "xml attribute '" << k << "' but not inside a tag!" << endl;
37  }
38 
39  return inTag_;
40 }
41 
42 
43 bool 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 
64 uint64_t Foam::vtk::formatter::offset(const uint64_t)
65 {
66  return formatter::npos;
67 }
68 
69 
70 std::size_t Foam::vtk::formatter::encodedLength(std::size_t n) const
71 {
72  return n;
73 }
74 
75 
76 bool Foam::vtk::formatter::openTagImpl(const word& tagName)
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_.pop_back();
116  os_ << " /";
117  }
118  os_ << '>' << nl;
119  }
120 
121  return *this;
122 }
123 
124 
125 Foam::vtk::formatter& Foam::vtk::formatter::endTag(const word& tagName)
126 {
127  const word curr(std::move(xmlTags_.back()));
128  xmlTags_.pop_back();
129  indent();
130 
131  if (inTag_)
132  {
133  WarningInFunction
134  << "adding xml endTag '" << curr
135  << "' but already in another tag!"
136  << endl;
137 
138  // Also suppress further output, or not?
139  }
140 
141  // Verify expected end tag
142  if (!tagName.empty() && tagName != curr)
143  {
144  WarningInFunction
145  << "expecting to end xml tag '" << tagName
146  << "' but found '" << curr << "' instead"
147  << endl;
148  }
149 
150  os_ << "</" << curr << '>' << nl;
151 
152  inTag_ = false;
153 
154  return *this;
155 }
156 
157 
158 Foam::vtk::formatter& Foam::vtk::formatter::beginVTKFile
159 (
160  const word& contentType,
161  const word& contentVersion,
162  const bool leaveOpen
163 )
164 {
165  openTag(vtk::fileTag::VTK_FILE);
166  xmlAttr("type", contentType);
167  xmlAttr("version", contentVersion);
168  xmlAttr("byte_order", vtkPTraits<Foam::endian>::typeName);
169  xmlAttr("header_type", vtkPTraits<headerType>::typeName);
170  closeTag();
171 
172  openTag(contentType);
173  if (!leaveOpen)
174  {
175  closeTag();
176  }
177 
178  return *this;
179 }
180 
181 
182 Foam::vtk::formatter& Foam::vtk::formatter::beginAppendedData()
183 {
184  openTag("AppendedData");
185  xmlAttr("encoding", encoding());
186  closeTag();
187  os_ << '_';
188 
189  return *this;
190 }
191 
192 
193 Foam::vtk::formatter& Foam::vtk::formatter::endAppendedData()
194 {
195  flush(); // Flush any pending encoded content
196  os_ << nl; // Ensure clear separation from content
197  return endTag("AppendedData");
198 }
199 
200 
201 Foam::vtk::formatter& Foam::vtk::formatter::beginBlock
202 (
203  label index,
204  std::string name
205 )
206 {
207  openTag(vtk::fileTag::BLOCK);
208  if (index >= 0)
209  {
210  xmlAttr("index", index);
211  }
212  if (name.size())
213  {
214  xmlAttr("name", name);
215  }
216  closeTag();
217 
218  return *this;
219 }
220 
221 
222 Foam::vtk::formatter& Foam::vtk::formatter::beginPiece
223 (
224  label index,
225  std::string name
226 )
227 {
228  openTag(vtk::fileTag::PIECE);
229  if (index >= 0)
230  {
231  xmlAttr("index", index);
232  }
233  if (name.size())
234  {
235  xmlAttr("name", name);
236  }
237  closeTag();
238 
239  return *this;
240 }
241 
242 
243 Foam::vtk::formatter& Foam::vtk::formatter::DataSet
244 (
245  label index,
246  std::string file,
247  bool autoName
248 )
249 {
250  openTag(vtk::fileTag::DATA_SET);
251 
252  if (index >= 0)
253  {
254  xmlAttr("index", index);
255  }
256  if (file.size())
257  {
258  if (autoName)
259  {
260  xmlAttr("name", fileName::stem(file));
261  }
262  xmlAttr("file", file);
263  }
264  closeTag(true); // Empty tag. ie, <DataSet ... />
265 
266  return *this;
267 }
268 
269 
270 Foam::vtk::formatter& Foam::vtk::formatter::DataSet
271 (
272  label index,
273  std::string file,
274  std::string name
275 )
276 {
277  openTag(vtk::fileTag::DATA_SET);
278 
279  if (index >= 0)
280  {
281  xmlAttr("index", index);
282  }
283  if (name.size())
284  {
285  xmlAttr("name", name);
286  }
287  if (file.size())
288  {
289  xmlAttr("file", file);
290  }
291  closeTag(true); // Empty tag. ie, <DataSet ... />
292 
293  return *this;
294 }
295 
296 
297 Foam::vtk::formatter& Foam::vtk::formatter::writeTimeValue(scalar timeValue)
298 {
299  // Emit "TimeValue" as FieldData
300  // NumberOfTuples="1" (required!)
301 
302  uint64_t payLoad = vtk::sizeofData<float>(1);
303 
304  beginDataArray<float,1,1>("TimeValue");
305  writeSize(payLoad);
306 
307  write(timeValue);
308  flush();
309 
310  endDataArray();
311 
312  return *this;
313 }
314 
315 
316 // ************************************************************************* //
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
bool inTag_
Tag open/closed/ended state.
Abstract class for a VTK output stream formatter.
quoteChar
Quoting for XML attributes.
formatter & closeTag(const bool isEmpty=false)
Finish an XML tag, optional as an empty container.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual uint64_t offset(const uint64_t numbytes)
Increase the append data offset by numbytes and sizeof(uint64_t).
label k
Boltzmann constant.
A class for handling words, derived from Foam::string.
Definition: word.H:63
static constexpr uint64_t npos
Out of range position or size.
virtual std::size_t encodedLength(std::size_t n) const
The encoded length for binary output is pass-through.
#define WarningInFunction
Report a warning using Foam::Warning.
label n
bool canWriteToplevel(const char *what) const
Can write tag-like top-level content (eg, comment, ...) when not already inside a tag...
bool openTagImpl(const word &tagName)
Open XML tag (implementation), checking if not already inside another tag. Emit warning and return fa...
void quoting(const quoteChar quote)
Change quoting char for XML attributes (default: SINGLE_QUOTE)
bool canWriteAttr(const word &k) const
Can write XML key/value attribute pair when inside a tag. Emit warning and return false if this condi...