initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / conversion / ensight / file / ensightFile.C
blob8cb28f8c5ef5faccd7889f76cb351eaf09b709f0
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "ensightFile.H"
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 bool Foam::ensightFile::allowUndef_ = false;
33 Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
35 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
37 Foam::ensightFile::ensightFile
39     const fileName& pathname,
40     IOstream::streamFormat format
43     OFstream(pathname, format)
45     // ascii formatting specs
46     setf
47     (
48         ios_base::scientific,
49         ios_base::floatfield
50     );
51     precision(5);
55 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
57 Foam::ensightFile::~ensightFile()
61 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
63 bool Foam::ensightFile::allowUndef()
65     return allowUndef_;
69 bool Foam::ensightFile::allowUndef(bool value)
71     bool old = allowUndef_;
72     allowUndef_ = value;
73     return old;
77 Foam::scalar Foam::ensightFile::undefValue(const scalar& value)
79     // enable its use too
80     allowUndef_ = true;
82     scalar old = undefValue_;
83     undefValue_ = value;
84     return old;
88 Foam::Ostream& Foam::ensightFile::write
90     const char* buf,
91     std::streamsize count
94     stream().write(buf, count);
95     return *this;
99 Foam::Ostream& Foam::ensightFile::write(const string& value)
101     char buf[80];
103     for (string::size_type i = 0; i < 80; ++i)
104     {
105         buf[i] = 0;
106     }
108     string::size_type n = value.size();
109     if (n >= 80)
110     {
111         n = 79;
112     }
114     for (string::size_type i = 0; i < n; ++i)
115     {
116         buf[i] = value[i];
117     }
119     if (format() == IOstream::BINARY)
120     {
121         write
122         (
123             reinterpret_cast<char const *>(buf),
124             sizeof(buf)
125         );
126     }
127     else
128     {
129         stream() << buf;
130     }
132     return *this;
136 Foam::Ostream& Foam::ensightFile::write(const label& value)
138     if (format() == IOstream::BINARY)
139     {
140         unsigned int ivalue(value);
142         write
143         (
144             reinterpret_cast<char const *>(&ivalue),
145             sizeof(ivalue)
146         );
147     }
148     else
149     {
150         stream().width(10);
151         stream() << value;
152     }
154     return *this;
158 Foam::Ostream& Foam::ensightFile::write
160     const label& value,
161     const label fieldWidth
164     if (format() == IOstream::BINARY)
165     {
166         unsigned int ivalue(value);
168         write
169         (
170             reinterpret_cast<char const *>(&ivalue),
171             sizeof(ivalue)
172         );
173     }
174     else
175     {
176         stream().width(fieldWidth);
177         stream() << value;
178     }
180     return *this;
184 Foam::Ostream& Foam::ensightFile::write(const scalar& value)
186     if (format() == IOstream::BINARY)
187     {
188         float fvalue(value);
190         write
191         (
192             reinterpret_cast<char const *>(&fvalue),
193             sizeof(fvalue)
194         );
195     }
196     else
197     {
198         stream().width(12);
199         stream() << value;
200     }
202     return *this;
206 void Foam::ensightFile::newline()
208     if (format() == IOstream::ASCII)
209     {
210         stream() << nl;
211     }
215 Foam::Ostream& Foam::ensightFile::writeUndef()
217     write(undefValue_);
218     return *this;
222 Foam::Ostream& Foam::ensightFile::writeKeyword(const string& key)
224     if (allowUndef_)
225     {
226         write(key + " undef");
227         newline();
228         write(undefValue_);
229         newline();
230     }
231     else
232     {
233         write(key);
234         newline();
235     }
236     return *this;
240 Foam::Ostream& Foam::ensightFile::writeBinaryHeader()
242     if (format() == IOstream::BINARY)
243     {
244         write("C Binary");
245     }
247     return *this;
251 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
253 Foam::string Foam::ensightFile::mask()
255     char buf[16] = "********";
256     return buf;
260 Foam::string Foam::ensightFile::subDir(const label n)
262     char buf[16];
264     sprintf(buf, "%08d", n);
265     return buf;
269 // ************************************************************************* //