FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <conversion/ensightFile.H>
00027 
00028 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00029 
00030 bool Foam::ensightFile::allowUndef_ = false;
00031 
00032 Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 Foam::ensightFile::ensightFile
00037 (
00038     const fileName& pathname,
00039     IOstream::streamFormat format
00040 )
00041 :
00042     OFstream(pathname, format)
00043 {
00044     // ascii formatting specs
00045     setf
00046     (
00047         ios_base::scientific,
00048         ios_base::floatfield
00049     );
00050     precision(5);
00051 }
00052 
00053 
00054 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00055 
00056 Foam::ensightFile::~ensightFile()
00057 {}
00058 
00059 
00060 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00061 
00062 bool Foam::ensightFile::allowUndef()
00063 {
00064     return allowUndef_;
00065 }
00066 
00067 
00068 bool Foam::ensightFile::allowUndef(bool value)
00069 {
00070     bool old = allowUndef_;
00071     allowUndef_ = value;
00072     return old;
00073 }
00074 
00075 
00076 Foam::scalar Foam::ensightFile::undefValue(const scalar& value)
00077 {
00078     // enable its use too
00079     allowUndef_ = true;
00080 
00081     scalar old = undefValue_;
00082     undefValue_ = value;
00083     return old;
00084 }
00085 
00086 
00087 Foam::Ostream& Foam::ensightFile::write
00088 (
00089     const char* buf,
00090     std::streamsize count
00091 )
00092 {
00093     stream().write(buf, count);
00094     return *this;
00095 }
00096 
00097 
00098 Foam::Ostream& Foam::ensightFile::write(const string& value)
00099 {
00100     char buf[80];
00101 
00102     for (string::size_type i = 0; i < 80; ++i)
00103     {
00104         buf[i] = 0;
00105     }
00106 
00107     string::size_type n = value.size();
00108     if (n >= 80)
00109     {
00110         n = 79;
00111     }
00112 
00113     for (string::size_type i = 0; i < n; ++i)
00114     {
00115         buf[i] = value[i];
00116     }
00117 
00118     if (format() == IOstream::BINARY)
00119     {
00120         write
00121         (
00122             reinterpret_cast<char const *>(buf),
00123             sizeof(buf)
00124         );
00125     }
00126     else
00127     {
00128         stream() << buf;
00129     }
00130 
00131     return *this;
00132 }
00133 
00134 
00135 Foam::Ostream& Foam::ensightFile::write(const label& value)
00136 {
00137     if (format() == IOstream::BINARY)
00138     {
00139         unsigned int ivalue(value);
00140 
00141         write
00142         (
00143             reinterpret_cast<char const *>(&ivalue),
00144             sizeof(ivalue)
00145         );
00146     }
00147     else
00148     {
00149         stream().width(10);
00150         stream() << value;
00151     }
00152 
00153     return *this;
00154 }
00155 
00156 
00157 Foam::Ostream& Foam::ensightFile::write
00158 (
00159     const label& value,
00160     const label fieldWidth
00161 )
00162 {
00163     if (format() == IOstream::BINARY)
00164     {
00165         unsigned int ivalue(value);
00166 
00167         write
00168         (
00169             reinterpret_cast<char const *>(&ivalue),
00170             sizeof(ivalue)
00171         );
00172     }
00173     else
00174     {
00175         stream().width(fieldWidth);
00176         stream() << value;
00177     }
00178 
00179     return *this;
00180 }
00181 
00182 
00183 Foam::Ostream& Foam::ensightFile::write(const scalar& value)
00184 {
00185     if (format() == IOstream::BINARY)
00186     {
00187         float fvalue(value);
00188 
00189         write
00190         (
00191             reinterpret_cast<char const *>(&fvalue),
00192             sizeof(fvalue)
00193         );
00194     }
00195     else
00196     {
00197         stream().width(12);
00198         stream() << value;
00199     }
00200 
00201     return *this;
00202 }
00203 
00204 
00205 void Foam::ensightFile::newline()
00206 {
00207     if (format() == IOstream::ASCII)
00208     {
00209         stream() << nl;
00210     }
00211 }
00212 
00213 
00214 Foam::Ostream& Foam::ensightFile::writeUndef()
00215 {
00216     write(undefValue_);
00217     return *this;
00218 }
00219 
00220 
00221 Foam::Ostream& Foam::ensightFile::writeKeyword(const string& key)
00222 {
00223     if (allowUndef_)
00224     {
00225         write(key + " undef");
00226         newline();
00227         write(undefValue_);
00228         newline();
00229     }
00230     else
00231     {
00232         write(key);
00233         newline();
00234     }
00235     return *this;
00236 }
00237 
00238 
00239 Foam::Ostream& Foam::ensightFile::writeBinaryHeader()
00240 {
00241     if (format() == IOstream::BINARY)
00242     {
00243         write("C Binary");
00244     }
00245 
00246     return *this;
00247 }
00248 
00249 
00250 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
00251 
00252 Foam::string Foam::ensightFile::mask()
00253 {
00254     char buf[16] = "********";
00255     return buf;
00256 }
00257 
00258 
00259 Foam::string Foam::ensightFile::subDir(const label n)
00260 {
00261     char buf[16];
00262 
00263     sprintf(buf, "%08d", n);
00264     return buf;
00265 }
00266 
00267 
00268 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines