ensightFile.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "ensightFile.H"
27 #include <sstream>
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 bool Foam::ensightFile::allowUndef_ = false;
32 
33 Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGreat;
34 
35 // default is width 8
36 Foam::string Foam::ensightFile::mask_ = "********";
37 
38 Foam::string Foam::ensightFile::dirFmt_ = "%08d";
39 
40 
41 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
42 
44 {
45  return mask_;
46 }
47 
48 
50 {
51  char buf[32];
52 
53  sprintf(buf, dirFmt_.c_str(), n);
54  return buf;
55 }
56 
57 
59 {
60  // enforce max limit to avoid buffer overflow in subDir()
61  if (n < 1 || n > 31)
62  {
63  return;
64  }
65 
66  // appropriate printf format
67  std::ostringstream oss;
68  oss << "%0" << n << "d";
69  dirFmt_ = oss.str();
70 
71  // set mask accordingly
72  mask_.resize(n, '*');
73 }
74 
75 
77 {
78  return mask_.size();
79 }
80 
81 
82 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
83 
85 (
86  const fileName& pathname,
88 )
89 :
90  OFstream(pathname, format)
91 {
92  // ascii formatting specs
93  setf
94  (
96  ios_base::floatfield
97  );
98  precision(5);
99 }
100 
101 
102 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
103 
105 {}
106 
107 
108 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
109 
111 {
112  return allowUndef_;
113 }
114 
115 
117 {
118  bool old = allowUndef_;
119  allowUndef_ = value;
120  return old;
121 }
122 
123 
124 Foam::scalar Foam::ensightFile::undefValue(const scalar value)
125 {
126  // enable its use too
127  allowUndef_ = true;
128 
129  scalar old = undefValue_;
130  undefValue_ = value;
131  return old;
132 }
133 
134 
136 (
137  const char* buf,
138  std::streamsize count
139 )
140 {
141  stdStream().write(buf, count);
142  return *this;
143 }
144 
145 
147 {
148  return write(string(value));
149 }
150 
151 
153 {
154  char buf[80];
155 
156  for (string::size_type i = 0; i < 80; ++i)
157  {
158  buf[i] = 0;
159  }
160 
161  string::size_type n = value.size();
162  if (n >= 80)
163  {
164  n = 79;
165  }
166 
167  for (string::size_type i = 0; i < n; ++i)
168  {
169  buf[i] = value[i];
170  }
171 
172  if (format() == IOstream::BINARY)
173  {
174  write
175  (
176  reinterpret_cast<char const *>(buf),
177  sizeof(buf)
178  );
179  }
180  else
181  {
182  stdStream() << buf;
183  }
184 
185  return *this;
186 }
187 
188 
190 {
191  if (format() == IOstream::BINARY)
192  {
193  unsigned int ivalue(value);
194 
195  write
196  (
197  reinterpret_cast<char const *>(&ivalue),
198  sizeof(ivalue)
199  );
200  }
201  else
202  {
203  stdStream().width(10);
204  stdStream() << value;
205  }
206 
207  return *this;
208 }
209 
210 
212 (
213  const label value,
214  const label fieldWidth
215 )
216 {
217  if (format() == IOstream::BINARY)
218  {
219  unsigned int ivalue(value);
220 
221  write
222  (
223  reinterpret_cast<char const *>(&ivalue),
224  sizeof(ivalue)
225  );
226  }
227  else
228  {
229  stdStream().width(fieldWidth);
230  stdStream() << value;
231  }
232 
233  return *this;
234 }
235 
236 
238 {
239  float fvalue(value);
240 
241  if (format() == IOstream::BINARY)
242  {
243  write
244  (
245  reinterpret_cast<char const *>(&fvalue),
246  sizeof(fvalue)
247  );
248  }
249  else
250  {
251  stdStream().width(12);
252  stdStream() << fvalue;
253  }
254 
255  return *this;
256 }
257 
258 
260 {
261  if (format() == IOstream::ASCII)
262  {
263  stdStream() << nl;
264  }
265 }
266 
267 
269 {
270  write(undefValue_);
271  return *this;
272 }
273 
274 
276 {
277  if (allowUndef_)
278  {
279  write(string(key + " undef"));
280  newline();
281  write(undefValue_);
282  newline();
283  }
284  else
285  {
286  write(key);
287  newline();
288  }
289  return *this;
290 }
291 
292 
294 {
295  if (format() == IOstream::BINARY)
296  {
297  write("C Binary");
298  }
299 
300  return *this;
301 }
302 
303 
304 // ************************************************************************* //
Ostream & writeUndef()
Write undef value.
Definition: ensightFile.C:268
OFstream(const fileName &pathname, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED, const bool append=false)
Construct from pathname.
Definition: OFstream.C:115
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:79
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:246
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:496
static scalar undefValue(const scalar)
Assign the value to represent undef in the results.
Definition: ensightFile.C:124
static string subDir(const label)
Consistent zero-padded numbers for subdirectories.
Definition: ensightFile.C:49
virtual ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:159
virtual Ostream & writeKeyword(const string &key)
Write element keyword with trailing newline, optionally with undef.
Definition: ensightFile.C:275
~ensightFile()
Destructor.
Definition: ensightFile.C:104
static const floatScalar floatScalarVGreat
Definition: floatScalar.H:56
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
static string mask()
The &#39;*&#39; mask appropriate for subDir.
Definition: ensightFile.C:43
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
ensightFile(const fileName &pathname, IOstream::streamFormat format=IOstream::BINARY)
Construct from pathname.
Definition: ensightFile.C:85
virtual Ostream & write(const char *buf, std::streamsize count)
Binary write.
Definition: ensightFile.C:136
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
static bool allowUndef()
Return setting for whether &#39;undef&#39; values are allowed in results.
Definition: ensightFile.C:110
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
Ostream & writeBinaryHeader()
Write "C Binary" for binary files (eg, geometry/measured)
Definition: ensightFile.C:293
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:259
label n
A class for handling character strings derived from std::string.
Definition: string.H:76
static label subDirWidth()
Return current width of subDir and mask.
Definition: ensightFile.C:76
IOstream & scientific(IOstream &io)
Definition: IOstream.H:582