IOstream.H 15.5 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
Mark Olesen's avatar
Mark Olesen committed
5
    \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    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 2 of the License, or (at your
    option) any later version.

    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
    along with OpenFOAM; if not, write to the Free Software Foundation,
    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Class
    Foam::IOstream

Description
Mark Olesen's avatar
Mark Olesen committed
29
30
31
32
33
34
35
36
    An IOstream is an abstract base class for all input/output systems; be
    they streams, files, token lists etc.

    The basic operations are construct, close, read token, read primitive
    and read binary block.  In addition version control and line number
    counting is incorporated.  Usually one would use the read primitive
    member functions, but if one were reading a stream on unknown data
    sequence one can read token by token, and then analyse.
37
38

SourceFiles
Mark Olesen's avatar
Mark Olesen committed
39
    IOstream.C
40
41
42
43
44
45
46
47
48

\*---------------------------------------------------------------------------*/

#ifndef IOstream_H
#define IOstream_H

#include "char.H"
#include "bool.H"
#include "label.H"
Mark Olesen's avatar
Mark Olesen committed
49
#include "uLabel.H"
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
84
85
86
87
88
89
90
91
92
93
94
95
96
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "scalar.H"
#include "fileName.H"
#include "InfoProxy.H"

#include <iostream>

#if __GNUC__ < 3
#   define ios_base ios
#endif

using std::ios_base;
using std::istream;
using std::ostream;

using std::cin;
using std::cout;
using std::cerr;

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

namespace Foam
{

/*---------------------------------------------------------------------------*\
                           Class IOstream Declaration
\*---------------------------------------------------------------------------*/

class IOstream
{

public:

    // Public data types

        //- Enumeration for whether the stream open or closed
        enum streamAccess
        {
            OPENED,
            CLOSED
        };

        //- Enumeration for the format of data in the stream
        enum streamFormat
        {
            ASCII,
            BINARY
        };

        //- Ostream operator
        friend Ostream& operator<<(Ostream& os, const streamFormat& sf);

        //- Version number type
        class versionNumber
        {
            //- The version number
            scalar versionNumber_;

            //- The version number as an integer
            int index_;


        public:

            // Constructors

                //- Construct from number
                versionNumber(const scalar num)
                :
                    versionNumber_(num),
                    index_(numberToIndex(num))
                {}

                //- Construct from Istream
                versionNumber(Istream& is)
                :
                    versionNumber_(readScalar(is)),
                    index_(numberToIndex(versionNumber_))
                {}


            // Member functions

                //- Convert a version number into an index
                int numberToIndex(const scalar num) const
                {
                    return int(10*num + SMALL);
                }

                //- Return major version
                int majorVersion() const
                {
                    return int(versionNumber_);
                }

                //- Return minor version
                int minorVersion() const
                {
                    return int(10.0*(versionNumber_ - majorVersion()));
                }

                //- Return the versionNumber as a character string
                string str() const;


            // Member operators

                //- Are these versionNumbers the same?
                bool operator==(const versionNumber& vn)
                {
                    return index_ == vn.index_;
                }

                //- Are these versionNumbers different?
                bool operator!=(const versionNumber& vn)
                {
                    return index_ != vn.index_;
                }

                //- Is this version older than the one given
                bool operator<(const versionNumber& vn)
                {
                    return index_ < vn.index_;
                }

                //- Is this version the same as or older than the one given
                bool operator<=(const versionNumber& vn)
                {
                    return index_ <= vn.index_;
                }

                //- Is this version newer than the one given
                bool operator>(const versionNumber& vn)
                {
                    return index_ > vn.index_;
                }

                //- this version the same as or newer than the one given
                bool operator>=(const versionNumber& vn)
                {
                    return index_ >= vn.index_;
                }


            //- Ostream operator
            friend Ostream& operator<<(Ostream& os, const versionNumber& vn);
        };


        //- Enumeration for the format of data in the stream
        enum compressionType
        {
            UNCOMPRESSED,
            COMPRESSED
        };


    // Public static data

        //- Original version number
        static const versionNumber originalVersion;

        //- Current version number
        static const versionNumber currentVersion;

        //- Default precision
        static unsigned int precision_;


private:

    // Private data

        //- Name of the stream
        static fileName name_;

        streamFormat format_;
        versionNumber version_;
        compressionType compression_;

        streamAccess openClosed_;
        ios_base::iostate ioState_;


protected:

    // Protected data

        label lineNumber_;


    // Protected member functions

        // Access

            //- Set stream opened
            void setOpened()
            {
                openClosed_ = OPENED;
            }

            //- Set stream closed
            void setClosed()
            {
                openClosed_ = CLOSED;
            }

            //- Set stream state
            void setState(ios_base::iostate state)
            {
                ioState_ = state;
            }

            //- Set stream to be good
            void setGood()
            {
                ioState_ = ios_base::iostate(0);
            }


public:

    // Constructors

        //- Construct setting format and version
        IOstream
        (
            streamFormat format,
            versionNumber version,
            compressionType compression=UNCOMPRESSED
        )
        :
            format_(format),
            version_(version),
            compression_(compression),
            openClosed_(CLOSED),
            ioState_(ios_base::iostate(0)),
            lineNumber_(0)
        {
            setBad();
        }


    // Destructor

        virtual ~IOstream()
        {}


    // Member functions

        // Access

            //- Return the name of the stream
            //  Useful for Fstream to return the filename
            virtual const fileName& name() const
            {
                return name_;
            }

            //- Return non-const access to the name of the stream
            //  Useful to alter the stream name
            virtual fileName& name()
            {
                return name_;
            }


        // Check

            //- Check IOstream status for given operation
            //  print IOstream state if error has occured
            virtual bool check(const char* operation) const;

            //- Check IOstream status for given operation
            //  print IOstream state if error has occured and exit
            void fatalCheck(const char* operation) const;

            //- Return true if stream has been opened
            bool opened() const
            {
                return openClosed_ == OPENED;
            }

            //- Return true if stream is closed
            bool closed() const
            {
                return openClosed_ == CLOSED;
            }

            //- Return true if next operation might succeed
            bool good() const
            {
                return ioState_ == 0;
            }

            //- Return true if end of input seen
            bool eof() const
            {
                return ioState_ & ios_base::eofbit;
            }

            //- Return true if next operation will fail
            bool fail() const
            {
                return ioState_ & (ios_base::badbit | ios_base::failbit);
            }

            //- Return true if stream is corrupted
            bool bad() const
            {
                return ioState_ & ios_base::badbit;
            }

            //- Return non-zero if the stream has not failed
            operator void*() const
            {
                return fail()
                    ? reinterpret_cast<void*>(0)
                    : reinterpret_cast<void*>(-1);
            }

            //- Return true if the stream has failed
            bool operator!() const
            {
                return fail();
            }


        // Stream state functions

            //- Return stream format of given format name
            static streamFormat formatEnum(const word&);

            //- Return current stream format
            streamFormat format() const
            {
                return format_;
            }

            //- Set the stream format
            streamFormat format(const streamFormat fmt)
            {
                streamFormat fmt0 = format_;
                format_ = fmt;
                return fmt0;
            }

            //- Set the stream format from word
            streamFormat format(const word& fmt)
            {
                streamFormat fmt0 = format_;
                format_ = formatEnum(fmt);
                return fmt0;
            }

            //- Return the stream version
            versionNumber version() const
            {
                return version_;
            }

            //- Set the stream version
            versionNumber version(const versionNumber ver)
            {
                versionNumber ver0 = version_;
                version_ = ver;
                return ver0;
            }

            //- Return compression of given compression name
Henry Weller's avatar
Henry Weller committed
420
            static compressionType compressionEnum(const word&);
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601

            //- Return the stream compression
            compressionType compression() const
            {
                return compression_;
            }

            //- Set the stream compression
            compressionType compression(const compressionType cmp)
            {
                compressionType cmp0 = compression_;
                compression_ = cmp;
                return cmp0;
            }

            //- Set the stream compression from word
            compressionType compression(const word& cmp)
            {
                compressionType cmp0 = compression_;
                compression_ = compressionEnum(cmp);
                return cmp0;
            }

            //- Return current stream line number
            label lineNumber() const
            {
                return lineNumber_;
            }

            //- Return current stream line number
            label& lineNumber()
            {
                return lineNumber_;
            }

            //- Set the stream line number
            label lineNumber(const label ln)
            {
                label ln0 = lineNumber_;
                lineNumber_ = ln;
                return ln0;
            }

            //- Return flags of stream
            virtual ios_base::fmtflags flags() const = 0;

            //- Return the default precision
            static unsigned int defaultPrecision()
            {
                return precision_;
            }

            //- Reset the default precision (and return old precision)
            static unsigned int defaultPrecision(unsigned int p)
            {
                unsigned int precision0 = precision_;
                precision_ = p;
                return precision0;
            }

            //- Set stream to have reached eof
            void setEof()
            {
                ioState_ |= ios_base::eofbit;
            }

            //- Set stream to have failed
            void setFail()
            {
                ioState_ |= ios_base::failbit;
            }

            //- Set stream to be bad
            void setBad()
            {
                ioState_ |= ios_base::badbit;
            }

            //- Set flags of stream
            virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;

            //- Set flags of stream
            ios_base::fmtflags setf(const ios_base::fmtflags f)
            {
                return flags(flags() | f);
            }

            //- Set flags of given field of stream
            ios_base::fmtflags setf
            (
                const ios_base::fmtflags f,
                const ios_base::fmtflags mask
            )
            {
                return flags((flags() & ~mask) | (f & mask));
            }

            //- Unset flags of stream
            void unsetf(const ios_base::fmtflags uf)
            {
                flags(flags()&~uf);
            }


        // Print

            //- Print description of IOstream to Ostream
            virtual void print(Ostream&) const;

            //- Check given stream state bits
            void print(Ostream&, const int streamState) const;


        // Info

            //- Return info proxy.
            //  Used to print IOstream information to a stream
            InfoProxy<IOstream> info() const
            {
                return *this;
            }
};


Ostream& operator<<(Ostream& os, const IOstream::streamFormat& sf);
Ostream& operator<<(Ostream& os, const IOstream::versionNumber& vn);


// --------------------------------------------------------------------
// ------ Manipulators (not taking arguments)
// --------------------------------------------------------------------

typedef IOstream& (*IOstreamManip)(IOstream&);

//- operator<< handling for manipulators without arguments
inline IOstream& operator<<(IOstream& io, IOstreamManip f)
{
    return f(io);
}


inline IOstream& dec(IOstream& io)
{
    io.setf(ios_base::dec, ios_base::dec|ios_base::hex|ios_base::oct);
    return io;
}

inline IOstream& hex(IOstream& io)
{
    io.setf(ios_base::hex, ios_base::dec|ios_base::hex|ios_base::oct);
    return io;
}

inline IOstream& oct(IOstream& io)
{
    io.setf(ios_base::oct, ios_base::dec|ios_base::hex|ios_base::oct);
    return io;
}

inline IOstream& fixed(IOstream& io)
{
    io.setf(ios_base::fixed, ios_base::floatfield);
    return io;
}

inline IOstream& scientific(IOstream& io)
{
    io.setf(ios_base::scientific, ios_base::floatfield);
    return io;
}


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

} // End namespace Foam

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

#endif

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