ensightFile.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 | Copyright (C) 2011-2012 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 
84 Foam::ensightFile::ensightFile
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  char buf[80];
149 
150  for (string::size_type i = 0; i < 80; ++i)
151  {
152  buf[i] = 0;
153  }
154 
155  string::size_type n = value.size();
156  if (n >= 80)
157  {
158  n = 79;
159  }
160 
161  for (string::size_type i = 0; i < n; ++i)
162  {
163  buf[i] = value[i];
164  }
165 
166  if (format() == IOstream::BINARY)
167  {
168  write
169  (
170  reinterpret_cast<char const *>(buf),
171  sizeof(buf)
172  );
173  }
174  else
175  {
176  stdStream() << buf;
177  }
178 
179  return *this;
180 }
181 
182 
184 {
185  if (format() == IOstream::BINARY)
186  {
187  unsigned int ivalue(value);
188 
189  write
190  (
191  reinterpret_cast<char const *>(&ivalue),
192  sizeof(ivalue)
193  );
194  }
195  else
196  {
197  stdStream().width(10);
198  stdStream() << value;
199  }
200 
201  return *this;
202 }
203 
204 
206 (
207  const label value,
208  const label fieldWidth
209 )
210 {
211  if (format() == IOstream::BINARY)
212  {
213  unsigned int ivalue(value);
214 
215  write
216  (
217  reinterpret_cast<char const *>(&ivalue),
218  sizeof(ivalue)
219  );
220  }
221  else
222  {
223  stdStream().width(fieldWidth);
224  stdStream() << value;
225  }
226 
227  return *this;
228 }
229 
230 
232 {
233  float fvalue(value);
234 
235  if (format() == IOstream::BINARY)
236  {
237  write
238  (
239  reinterpret_cast<char const *>(&fvalue),
240  sizeof(fvalue)
241  );
242  }
243  else
244  {
245  stdStream().width(12);
246  stdStream() << fvalue;
247  }
248 
249  return *this;
250 }
251 
252 
254 {
255  if (format() == IOstream::ASCII)
256  {
257  stdStream() << nl;
258  }
259 }
260 
261 
263 {
264  write(undefValue_);
265  return *this;
266 }
267 
268 
270 {
271  if (allowUndef_)
272  {
273  write(key + " undef");
274  newline();
275  write(undefValue_);
276  newline();
277  }
278  else
279  {
280  write(key);
281  newline();
282  }
283  return *this;
284 }
285 
286 
288 {
289  if (format() == IOstream::BINARY)
290  {
291  write("C Binary");
292  }
293 
294  return *this;
295 }
296 
297 
298 // ************************************************************************* //
static scalar undefValue(const scalar)
Assign the value to represent undef in the results.
Definition: ensightFile.C:124
virtual Ostream & write(const char *buf, std::streamsize count)
binary write
Definition: ensightFile.C:136
A class for handling file names.
Definition: fileName.H:69
virtual Ostream & write(const token &)=0
Write next token to stream.
static bool allowUndef()
Return setting for whether 'undef' values are allowed in results.
Definition: ensightFile.C:110
Output to file stream.
Definition: OFstream.H:81
virtual Ostream & writeKeyword(const string &key)
write element keyword with trailing newline, optionally with undef
Definition: ensightFile.C:269
A class for handling character strings derived from std::string.
Definition: string.H:74
static string subDir(const label)
Consistent zero-padded numbers for subdirectories.
Definition: ensightFile.C:49
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:90
static const floatScalar floatScalarVGREAT
Definition: floatScalar.H:55
static label subDirWidth()
Return current width of subDir and mask.
Definition: ensightFile.C:76
word format(conversionProperties.lookup("format"))
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:253
IOstream & scientific(IOstream &io)
Definition: IOstream.H:583
label n
runTime write()
static const char nl
Definition: Ostream.H:257
Smanip< ios::fmtflags > setf(const ios::fmtflags flags)
Definition: IOmanip.H:164
~ensightFile()
Destructor.
Definition: ensightFile.C:104
Ostream & writeUndef()
write undef value
Definition: ensightFile.C:262
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static string mask()
The '*' mask appropriate for subDir.
Definition: ensightFile.C:43
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
Ostream & writeBinaryHeader()
write "C Binary" for binary files (eg, geometry/measured)
Definition: ensightFile.C:287