foamVtkAsciiFormatter.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-2021 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 
28 #include "foamVtkAsciiFormatter.H"
29 #include "foamVtkOutputOptions.H"
30 #include <limits>
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const char* Foam::vtk::asciiFormatter::name_ = "ascii";
35 
37 Foam::vtk::asciiFormatter::opts_(formatType::INLINE_ASCII);
38 
39 
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 
42 inline void Foam::vtk::asciiFormatter::next()
43 {
44  if (pos_ >= itemsPerLine_)
45  {
46  os()<< '\n';
47  pos_ = 0;
48  }
49  else if (pos_)
50  {
51  os()<< ' ';
52  }
53  ++pos_;
54 }
55 
56 
57 inline void Foam::vtk::asciiFormatter::done()
58 {
59  if (pos_)
60  {
61  os()<< '\n';
62  }
63  pos_ = 0;
64 }
65 
66 
67 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
68 
69 Foam::vtk::asciiFormatter::asciiFormatter(std::ostream& os)
70 :
71  formatter(os),
72  pos_(0)
73 {}
74 
75 
76 Foam::vtk::asciiFormatter::asciiFormatter
77 (
78  std::ostream& os,
79  unsigned precision
80 )
81 :
82  formatter(os),
83  pos_(0)
84 {
85  os.precision(precision);
86 }
87 
88 
89 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
90 
92 {
93  done();
94 }
95 
96 
97 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
98 
101 {
102  return opts_;
103 }
104 
105 
107 {
108  return name_;
109 }
110 
111 
113 {
114  return name_;
115 }
116 
117 
119 {
120  return false;
121 }
122 
123 
124 void Foam::vtk::asciiFormatter::write(const uint8_t val)
125 {
126  next();
127  os()<< int(val);
128 }
129 
130 
131 void Foam::vtk::asciiFormatter::write(const label val)
132 {
133  next();
134  os()<< val;
135 }
136 
137 
138 void Foam::vtk::asciiFormatter::write(const float val)
139 {
140  next();
141  os()<< val;
142 }
143 
144 
145 void Foam::vtk::asciiFormatter::write(const double val)
146 {
147  // Limit range of double to float conversion
148  if (val >= std::numeric_limits<float>::max())
149  {
151  }
152  else if (val <= std::numeric_limits<float>::lowest())
153  {
154  write(std::numeric_limits<float>::lowest());
155  }
156  else
157  {
158  float copy(val);
159  write(copy);
160  }
161 }
162 
163 
165 {
166  done();
167 }
168 
169 
170 std::size_t Foam::vtk::asciiFormatter::encodedLength(std::size_t) const
171 {
172  return 0;
173 }
174 
175 
176 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
Foam::vtk::asciiFormatter::opts
virtual const outputOptions & opts() const
The output is INLINE_ASCII.
Definition: foamVtkAsciiFormatter.C:100
Foam::vtk::formatter::os
std::ostream & os()
Access to the underlying output stream.
Definition: foamVtkFormatterI.H:70
Foam::vtk::asciiFormatter::write
virtual void write(const uint8_t val)
Definition: foamVtkAsciiFormatter.C:124
Foam::vtk::asciiFormatter::~asciiFormatter
virtual ~asciiFormatter()
Destructor. Finishes the output line as required.
Definition: foamVtkAsciiFormatter.C:91
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
os
OBJstream os(runTime.globalPath()/outputName)
Foam::vtk::asciiFormatter::encodedLength
virtual std::size_t encodedLength(std::size_t ignored) const
The encoded length for ascii output is not applicable.
Definition: foamVtkAsciiFormatter.C:170
foamVtkOutputOptions.H
Foam::vtk::asciiFormatter::flush
virtual void flush()
Write a newline if needed to finish a line of output.
Definition: foamVtkAsciiFormatter.C:164
Foam::vtk::asciiFormatter::encoding
virtual const char * encoding() const
Name for the XML append encoding - unused.
Definition: foamVtkAsciiFormatter.C:112
Foam::vtk::asciiFormatter::name
virtual const char * name() const
Name for the XML output type ("ascii")
Definition: foamVtkAsciiFormatter.C:106
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::vtk::asciiFormatter::writeSize
virtual bool writeSize(const uint64_t ignored)
Write leading size - this is a no-op for ascii output.
Definition: foamVtkAsciiFormatter.C:118
foamVtkAsciiFormatter.H
Foam::vtk::formatter
Abstract class for a VTK output stream formatter.
Definition: foamVtkFormatter.H:68