FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <conversion/ensightFile.H>
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 bool Foam::ensightFile::allowUndef_ = false;
31 
32 Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
36 Foam::ensightFile::ensightFile
37 (
38  const fileName& pathname,
40 )
41 :
42  OFstream(pathname, format)
43 {
44  // ascii formatting specs
45  setf
46  (
48  ios_base::floatfield
49  );
50  precision(5);
51 }
52 
53 
54 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
55 
57 {}
58 
59 
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
61 
63 {
64  return allowUndef_;
65 }
66 
67 
69 {
70  bool old = allowUndef_;
71  allowUndef_ = value;
72  return old;
73 }
74 
75 
76 Foam::scalar Foam::ensightFile::undefValue(const scalar& value)
77 {
78  // enable its use too
79  allowUndef_ = true;
80 
81  scalar old = undefValue_;
82  undefValue_ = value;
83  return old;
84 }
85 
86 
88 (
89  const char* buf,
90  std::streamsize count
91 )
92 {
93  stream().write(buf, count);
94  return *this;
95 }
96 
97 
99 {
100  char buf[80];
101 
102  for (string::size_type i = 0; i < 80; ++i)
103  {
104  buf[i] = 0;
105  }
106 
107  string::size_type n = value.size();
108  if (n >= 80)
109  {
110  n = 79;
111  }
112 
113  for (string::size_type i = 0; i < n; ++i)
114  {
115  buf[i] = value[i];
116  }
117 
118  if (format() == IOstream::BINARY)
119  {
120  write
121  (
122  reinterpret_cast<char const *>(buf),
123  sizeof(buf)
124  );
125  }
126  else
127  {
128  stream() << buf;
129  }
130 
131  return *this;
132 }
133 
134 
136 {
137  if (format() == IOstream::BINARY)
138  {
139  unsigned int ivalue(value);
140 
141  write
142  (
143  reinterpret_cast<char const *>(&ivalue),
144  sizeof(ivalue)
145  );
146  }
147  else
148  {
149  stream().width(10);
150  stream() << value;
151  }
152 
153  return *this;
154 }
155 
156 
158 (
159  const label& value,
160  const label fieldWidth
161 )
162 {
163  if (format() == IOstream::BINARY)
164  {
165  unsigned int ivalue(value);
166 
167  write
168  (
169  reinterpret_cast<char const *>(&ivalue),
170  sizeof(ivalue)
171  );
172  }
173  else
174  {
175  stream().width(fieldWidth);
176  stream() << value;
177  }
178 
179  return *this;
180 }
181 
182 
184 {
185  if (format() == IOstream::BINARY)
186  {
187  float fvalue(value);
188 
189  write
190  (
191  reinterpret_cast<char const *>(&fvalue),
192  sizeof(fvalue)
193  );
194  }
195  else
196  {
197  stream().width(12);
198  stream() << value;
199  }
200 
201  return *this;
202 }
203 
204 
206 {
207  if (format() == IOstream::ASCII)
208  {
209  stream() << nl;
210  }
211 }
212 
213 
215 {
216  write(undefValue_);
217  return *this;
218 }
219 
220 
222 {
223  if (allowUndef_)
224  {
225  write(key + " undef");
226  newline();
227  write(undefValue_);
228  newline();
229  }
230  else
231  {
232  write(key);
233  newline();
234  }
235  return *this;
236 }
237 
238 
240 {
241  if (format() == IOstream::BINARY)
242  {
243  write("C Binary");
244  }
245 
246  return *this;
247 }
248 
249 
250 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
251 
253 {
254  char buf[16] = "********";
255  return buf;
256 }
257 
258 
260 {
261  char buf[16];
262 
263  sprintf(buf, "%08d", n);
264  return buf;
265 }
266 
267 
268 // ************************ vim: set sw=4 sts=4 et: ************************ //