Merge commit 'd3b269b7c6ffa0cd68845adfecdfb849316dba71' into nextRelease
[foam-extend-3.2.git] / src / conversion / ensight / file / ensightFile.C
blobdf0b1bf9882e23a6f68b352ec6e782da764998f0
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     |
5     \\  /    A nd           | For copyright notice see file Copyright
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "ensightFile.H"
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 bool Foam::ensightFile::allowUndef_ = false;
32 Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
34 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
36 Foam::ensightFile::ensightFile
38     const fileName& pathname,
39     IOstream::streamFormat format
42     OFstream(pathname, ios_base::out|ios_base::trunc, format)
44     // ascii formatting specs
45     setf
46     (
47         ios_base::scientific,
48         ios_base::floatfield
49     );
50     precision(5);
54 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
56 Foam::ensightFile::~ensightFile()
60 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
62 bool Foam::ensightFile::allowUndef()
64     return allowUndef_;
68 bool Foam::ensightFile::allowUndef(bool value)
70     bool old = allowUndef_;
71     allowUndef_ = value;
72     return old;
76 Foam::scalar Foam::ensightFile::undefValue(const scalar& value)
78     // enable its use too
79     allowUndef_ = true;
81     scalar old = undefValue_;
82     undefValue_ = value;
83     return old;
87 Foam::Ostream& Foam::ensightFile::write
89     const char* buf,
90     std::streamsize count
93     stream().write(buf, count);
94     return *this;
98 Foam::Ostream& Foam::ensightFile::write(const string& value)
100     char buf[80];
102     for (string::size_type i = 0; i < 80; ++i)
103     {
104         buf[i] = 0;
105     }
107     string::size_type n = value.size();
108     if (n >= 80)
109     {
110         n = 79;
111     }
113     for (string::size_type i = 0; i < n; ++i)
114     {
115         buf[i] = value[i];
116     }
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     }
131     return *this;
135 Foam::Ostream& Foam::ensightFile::write(const label& value)
137     if (format() == IOstream::BINARY)
138     {
139         unsigned int ivalue(value);
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     }
153     return *this;
157 Foam::Ostream& Foam::ensightFile::write
159     const label& value,
160     const label fieldWidth
163     if (format() == IOstream::BINARY)
164     {
165         unsigned int ivalue(value);
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     }
179     return *this;
183 Foam::Ostream& Foam::ensightFile::write(const scalar& value)
185     if (format() == IOstream::BINARY)
186     {
187         float fvalue(value);
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     }
201     return *this;
205 void Foam::ensightFile::newline()
207     if (format() == IOstream::ASCII)
208     {
209         stream() << nl;
210     }
214 Foam::Ostream& Foam::ensightFile::writeUndef()
216     write(undefValue_);
217     return *this;
221 Foam::Ostream& Foam::ensightFile::writeKeyword(const string& key)
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;
239 Foam::Ostream& Foam::ensightFile::writeBinaryHeader()
241     if (format() == IOstream::BINARY)
242     {
243         write("C Binary");
244     }
246     return *this;
250 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
252 Foam::string Foam::ensightFile::mask()
254     char buf[16] = "********";
255     return buf;
259 Foam::string Foam::ensightFile::subDir(const label n)
261     char buf[16];
263     sprintf(buf, "%08d", n);
264     return buf;
268 // ************************************************************************* //