ensightFile.C 6.07 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2015 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
7
8
9
10
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
15
16
17
18
19
20
21

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    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 "ensightFile.H"
27
28
29
#include "error.H"

#include <cstring>
30
#include <sstream>
31
32
33
34
35
36
37

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

bool Foam::ensightFile::allowUndef_ = false;

Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;

38
// default is width 8
39
Foam::string Foam::ensightFile::mask_   = "********";
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Foam::string Foam::ensightFile::dirFmt_ = "%08d";


// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //

Foam::string Foam::ensightFile::mask()
{
    return mask_;
}


Foam::string Foam::ensightFile::subDir(const label n)
{
    char buf[32];

    sprintf(buf, dirFmt_.c_str(), n);
    return buf;
}


void Foam::ensightFile::subDirWidth(const label n)
{
    // enforce max limit to avoid buffer overflow in subDir()
    if (n < 1 || n > 31)
    {
        return;
    }

    // appropriate printf format
    std::ostringstream oss;
    oss << "%0" << n << "d";
    dirFmt_ = oss.str();

    // set mask accordingly
    mask_.resize(n, '*');
}


Foam::label Foam::ensightFile::subDirWidth()
{
    return mask_.size();
}


84
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
85

86
void Foam::ensightFile::initialize()
87
88
89
90
91
92
93
94
95
96
{
    // ascii formatting specs
    setf
    (
        ios_base::scientific,
        ios_base::floatfield
    );
    precision(5);
}

97

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::ensightFile::ensightFile
(
    const fileName& pathname,
    IOstream::streamFormat format
)
:
    OFstream(ensight::FileName(pathname), format)
{
    initialize();
}


Foam::ensightFile::ensightFile
(
    const fileName& path,
    const fileName& name,
    IOstream::streamFormat format
)
:
    OFstream(path/ensight::FileName(name), format)
{
    initialize();
}


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::ensightFile::~ensightFile()
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

bool Foam::ensightFile::allowUndef()
{
    return allowUndef_;
}


bool Foam::ensightFile::allowUndef(bool value)
{
    bool old = allowUndef_;
    allowUndef_ = value;
    return old;
}


147
Foam::scalar Foam::ensightFile::undefValue(const scalar value)
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{
    // enable its use too
    allowUndef_ = true;

    scalar old = undefValue_;
    undefValue_ = value;
    return old;
}


Foam::Ostream& Foam::ensightFile::write
(
    const char* buf,
    std::streamsize count
)
{
164
    stdStream().write(buf, count);
165
166
167
168
    return *this;
}


169
Foam::Ostream& Foam::ensightFile::write(const char* value)
170
171
{
    char buf[80];
172
    strncpy(buf, value, 80); // max 80 chars or padded with nul if smaller
173
174
175

    if (format() == IOstream::BINARY)
    {
176
        write(buf, sizeof(buf));
177
178
179
    }
    else
    {
180
        buf[79] = 0;  // max 79 in ASCII, ensure it is indeed nul-terminated
181
        stdStream() << buf;
182
183
184
    }

    return *this;
185
186
187
188
189
190
}


Foam::Ostream& Foam::ensightFile::write(const string& value)
{
    return write(value.c_str());
191
192
193
}


194
Foam::Ostream& Foam::ensightFile::write(const label value)
195
196
197
198
199
200
201
{
    if (format() == IOstream::BINARY)
    {
        unsigned int ivalue(value);

        write
        (
202
            reinterpret_cast<const char *>(&ivalue),
203
204
205
206
207
            sizeof(ivalue)
        );
    }
    else
    {
208
209
        stdStream().width(10);
        stdStream() << value;
210
211
212
213
214
215
216
217
    }

    return *this;
}


Foam::Ostream& Foam::ensightFile::write
(
218
    const label value,
219
220
221
222
223
224
225
226
227
    const label fieldWidth
)
{
    if (format() == IOstream::BINARY)
    {
        unsigned int ivalue(value);

        write
        (
228
            reinterpret_cast<const char *>(&ivalue),
229
230
231
232
233
            sizeof(ivalue)
        );
    }
    else
    {
234
235
        stdStream().width(fieldWidth);
        stdStream() << value;
236
237
238
239
240
241
    }

    return *this;
}


242
Foam::Ostream& Foam::ensightFile::write(const scalar value)
243
{
244
245
    float fvalue(value);

246
247
248
249
    if (format() == IOstream::BINARY)
    {
        write
        (
250
            reinterpret_cast<const char *>(&fvalue),
251
252
253
254
255
            sizeof(fvalue)
        );
    }
    else
    {
256
        stdStream().width(12);
257
        stdStream() << fvalue;
258
259
260
261
262
263
264
265
266
267
    }

    return *this;
}


void Foam::ensightFile::newline()
{
    if (format() == IOstream::ASCII)
    {
268
        stdStream() << nl;
269
270
271
272
273
274
275
276
277
278
279
    }
}


Foam::Ostream& Foam::ensightFile::writeUndef()
{
    write(undefValue_);
    return *this;
}


280
Foam::Ostream& Foam::ensightFile::writeKeyword(const keyType& key)
281
282
283
{
    if (allowUndef_)
    {
284
        write(string(static_cast<const string&>(key) + " undef"));
285
286
287
288
289
290
        newline();
        write(undefValue_);
        newline();
    }
    else
    {
291
292
        // ensure we get ensightFile::write(const string&)
        write(static_cast<const string&>(key));
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
        newline();
    }
    return *this;
}


Foam::Ostream& Foam::ensightFile::writeBinaryHeader()
{
    if (format() == IOstream::BINARY)
    {
        write("C Binary");
    }

    return *this;
}


// ************************************************************************* //