OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

ensightFile.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "ensightFile.H"
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 bool Foam::ensightFile::allowUndef_ = false;
00032 
00033 Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
00034 
00035 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00036 
00037 Foam::ensightFile::ensightFile
00038 (
00039     const fileName& pathname,
00040     IOstream::streamFormat format
00041 )
00042 :
00043     OFstream(pathname, format)
00044 {
00045     // ascii formatting specs
00046     setf
00047     (
00048         ios_base::scientific,
00049         ios_base::floatfield
00050     );
00051     precision(5);
00052 }
00053 
00054 
00055 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00056 
00057 Foam::ensightFile::~ensightFile()
00058 {}
00059 
00060 
00061 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00062 
00063 bool Foam::ensightFile::allowUndef()
00064 {
00065     return allowUndef_;
00066 }
00067 
00068 
00069 bool Foam::ensightFile::allowUndef(bool value)
00070 {
00071     bool old = allowUndef_;
00072     allowUndef_ = value;
00073     return old;
00074 }
00075 
00076 
00077 Foam::scalar Foam::ensightFile::undefValue(const scalar& value)
00078 {
00079     // enable its use too
00080     allowUndef_ = true;
00081 
00082     scalar old = undefValue_;
00083     undefValue_ = value;
00084     return old;
00085 }
00086 
00087 
00088 Foam::Ostream& Foam::ensightFile::write
00089 (
00090     const char* buf,
00091     std::streamsize count
00092 )
00093 {
00094     stream().write(buf, count);
00095     return *this;
00096 }
00097 
00098 
00099 Foam::Ostream& Foam::ensightFile::write(const string& value)
00100 {
00101     char buf[80];
00102 
00103     for (string::size_type i = 0; i < 80; ++i)
00104     {
00105         buf[i] = 0;
00106     }
00107 
00108     string::size_type n = value.size();
00109     if (n >= 80)
00110     {
00111         n = 79;
00112     }
00113 
00114     for (string::size_type i = 0; i < n; ++i)
00115     {
00116         buf[i] = value[i];
00117     }
00118 
00119     if (format() == IOstream::BINARY)
00120     {
00121         write
00122         (
00123             reinterpret_cast<char const *>(buf),
00124             sizeof(buf)
00125         );
00126     }
00127     else
00128     {
00129         stream() << buf;
00130     }
00131 
00132     return *this;
00133 }
00134 
00135 
00136 Foam::Ostream& Foam::ensightFile::write(const label& value)
00137 {
00138     if (format() == IOstream::BINARY)
00139     {
00140         unsigned int ivalue(value);
00141 
00142         write
00143         (
00144             reinterpret_cast<char const *>(&ivalue),
00145             sizeof(ivalue)
00146         );
00147     }
00148     else
00149     {
00150         stream().width(10);
00151         stream() << value;
00152     }
00153 
00154     return *this;
00155 }
00156 
00157 
00158 Foam::Ostream& Foam::ensightFile::write
00159 (
00160     const label& value,
00161     const label fieldWidth
00162 )
00163 {
00164     if (format() == IOstream::BINARY)
00165     {
00166         unsigned int ivalue(value);
00167 
00168         write
00169         (
00170             reinterpret_cast<char const *>(&ivalue),
00171             sizeof(ivalue)
00172         );
00173     }
00174     else
00175     {
00176         stream().width(fieldWidth);
00177         stream() << value;
00178     }
00179 
00180     return *this;
00181 }
00182 
00183 
00184 Foam::Ostream& Foam::ensightFile::write(const scalar& value)
00185 {
00186     if (format() == IOstream::BINARY)
00187     {
00188         float fvalue(value);
00189 
00190         write
00191         (
00192             reinterpret_cast<char const *>(&fvalue),
00193             sizeof(fvalue)
00194         );
00195     }
00196     else
00197     {
00198         stream().width(12);
00199         stream() << value;
00200     }
00201 
00202     return *this;
00203 }
00204 
00205 
00206 void Foam::ensightFile::newline()
00207 {
00208     if (format() == IOstream::ASCII)
00209     {
00210         stream() << nl;
00211     }
00212 }
00213 
00214 
00215 Foam::Ostream& Foam::ensightFile::writeUndef()
00216 {
00217     write(undefValue_);
00218     return *this;
00219 }
00220 
00221 
00222 Foam::Ostream& Foam::ensightFile::writeKeyword(const string& key)
00223 {
00224     if (allowUndef_)
00225     {
00226         write(key + " undef");
00227         newline();
00228         write(undefValue_);
00229         newline();
00230     }
00231     else
00232     {
00233         write(key);
00234         newline();
00235     }
00236     return *this;
00237 }
00238 
00239 
00240 Foam::Ostream& Foam::ensightFile::writeBinaryHeader()
00241 {
00242     if (format() == IOstream::BINARY)
00243     {
00244         write("C Binary");
00245     }
00246 
00247     return *this;
00248 }
00249 
00250 
00251 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
00252 
00253 Foam::string Foam::ensightFile::mask()
00254 {
00255     char buf[16] = "********";
00256     return buf;
00257 }
00258 
00259 
00260 Foam::string Foam::ensightFile::subDir(const label n)
00261 {
00262     char buf[16];
00263 
00264     sprintf(buf, "%08d", n);
00265     return buf;
00266 }
00267 
00268 
00269 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd