IOstream.H
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef IOstream_H
00044 #define IOstream_H
00045
00046 #include "char.H"
00047 #include "bool.H"
00048 #include "label.H"
00049 #include "uLabel.H"
00050 #include "scalar.H"
00051 #include "fileName.H"
00052 #include "InfoProxy.H"
00053
00054 #include <iostream>
00055
00056 #if __GNUC__ < 3
00057 # define ios_base ios
00058 #endif
00059
00060 using std::ios_base;
00061 using std::istream;
00062 using std::ostream;
00063
00064 using std::cin;
00065 using std::cout;
00066 using std::cerr;
00067
00068
00069
00070 namespace Foam
00071 {
00072
00073
00074
00075
00076
00077 class IOstream
00078 {
00079
00080 public:
00081
00082
00083
00084
00085 enum streamAccess
00086 {
00087 OPENED,
00088 CLOSED
00089 };
00090
00091
00092 enum streamFormat
00093 {
00094 ASCII,
00095 BINARY
00096 };
00097
00098
00099 friend Ostream& operator<<(Ostream& os, const streamFormat& sf);
00100
00101
00102 class versionNumber
00103 {
00104
00105 scalar versionNumber_;
00106
00107
00108 int index_;
00109
00110
00111 public:
00112
00113
00114
00115
00116 versionNumber(const scalar num)
00117 :
00118 versionNumber_(num),
00119 index_(numberToIndex(num))
00120 {}
00121
00122
00123 versionNumber(Istream& is)
00124 :
00125 versionNumber_(readScalar(is)),
00126 index_(numberToIndex(versionNumber_))
00127 {}
00128
00129
00130
00131
00132
00133 int numberToIndex(const scalar num) const
00134 {
00135 return int(10*num + SMALL);
00136 }
00137
00138
00139 int majorVersion() const
00140 {
00141 return int(versionNumber_);
00142 }
00143
00144
00145 int minorVersion() const
00146 {
00147 return int(10.0*(versionNumber_ - majorVersion()));
00148 }
00149
00150
00151 string str() const;
00152
00153
00154
00155
00156
00157 bool operator==(const versionNumber& vn)
00158 {
00159 return index_ == vn.index_;
00160 }
00161
00162
00163 bool operator!=(const versionNumber& vn)
00164 {
00165 return index_ != vn.index_;
00166 }
00167
00168
00169 bool operator<(const versionNumber& vn)
00170 {
00171 return index_ < vn.index_;
00172 }
00173
00174
00175 bool operator<=(const versionNumber& vn)
00176 {
00177 return index_ <= vn.index_;
00178 }
00179
00180
00181 bool operator>(const versionNumber& vn)
00182 {
00183 return index_ > vn.index_;
00184 }
00185
00186
00187 bool operator>=(const versionNumber& vn)
00188 {
00189 return index_ >= vn.index_;
00190 }
00191
00192
00193
00194 friend Ostream& operator<<(Ostream& os, const versionNumber& vn);
00195 };
00196
00197
00198
00199 enum compressionType
00200 {
00201 UNCOMPRESSED,
00202 COMPRESSED
00203 };
00204
00205
00206
00207
00208
00209 static const versionNumber originalVersion;
00210
00211
00212 static const versionNumber currentVersion;
00213
00214
00215 static unsigned int precision_;
00216
00217
00218 private:
00219
00220
00221
00222
00223 static fileName name_;
00224
00225 streamFormat format_;
00226 versionNumber version_;
00227 compressionType compression_;
00228
00229 streamAccess openClosed_;
00230 ios_base::iostate ioState_;
00231
00232
00233 protected:
00234
00235
00236
00237 label lineNumber_;
00238
00239
00240
00241
00242
00243
00244
00245 void setOpened()
00246 {
00247 openClosed_ = OPENED;
00248 }
00249
00250
00251 void setClosed()
00252 {
00253 openClosed_ = CLOSED;
00254 }
00255
00256
00257 void setState(ios_base::iostate state)
00258 {
00259 ioState_ = state;
00260 }
00261
00262
00263 void setGood()
00264 {
00265 ioState_ = ios_base::iostate(0);
00266 }
00267
00268
00269 public:
00270
00271
00272
00273
00274 IOstream
00275 (
00276 streamFormat format,
00277 versionNumber version,
00278 compressionType compression=UNCOMPRESSED
00279 )
00280 :
00281 format_(format),
00282 version_(version),
00283 compression_(compression),
00284 openClosed_(CLOSED),
00285 ioState_(ios_base::iostate(0)),
00286 lineNumber_(0)
00287 {
00288 setBad();
00289 }
00290
00291
00292
00293
00294 virtual ~IOstream()
00295 {}
00296
00297
00298
00299
00300
00301
00302
00303
00304 virtual const fileName& name() const
00305 {
00306 return name_;
00307 }
00308
00309
00310
00311 virtual fileName& name()
00312 {
00313 return name_;
00314 }
00315
00316
00317
00318
00319
00320
00321 virtual bool check(const char* operation) const;
00322
00323
00324
00325 void fatalCheck(const char* operation) const;
00326
00327
00328 bool opened() const
00329 {
00330 return openClosed_ == OPENED;
00331 }
00332
00333
00334 bool closed() const
00335 {
00336 return openClosed_ == CLOSED;
00337 }
00338
00339
00340 bool good() const
00341 {
00342 return ioState_ == 0;
00343 }
00344
00345
00346 bool eof() const
00347 {
00348 return ioState_ & ios_base::eofbit;
00349 }
00350
00351
00352 bool fail() const
00353 {
00354 return ioState_ & (ios_base::badbit | ios_base::failbit);
00355 }
00356
00357
00358 bool bad() const
00359 {
00360 return ioState_ & ios_base::badbit;
00361 }
00362
00363
00364 operator void*() const
00365 {
00366 return fail()
00367 ? reinterpret_cast<void*>(0)
00368 : reinterpret_cast<void*>(-1);
00369 }
00370
00371
00372 bool operator!() const
00373 {
00374 return fail();
00375 }
00376
00377
00378
00379
00380
00381 static streamFormat formatEnum(const word&);
00382
00383
00384 streamFormat format() const
00385 {
00386 return format_;
00387 }
00388
00389
00390 streamFormat format(const streamFormat fmt)
00391 {
00392 streamFormat fmt0 = format_;
00393 format_ = fmt;
00394 return fmt0;
00395 }
00396
00397
00398 streamFormat format(const word& fmt)
00399 {
00400 streamFormat fmt0 = format_;
00401 format_ = formatEnum(fmt);
00402 return fmt0;
00403 }
00404
00405
00406 versionNumber version() const
00407 {
00408 return version_;
00409 }
00410
00411
00412 versionNumber version(const versionNumber ver)
00413 {
00414 versionNumber ver0 = version_;
00415 version_ = ver;
00416 return ver0;
00417 }
00418
00419
00420 static compressionType compressionEnum(const word&);
00421
00422
00423 compressionType compression() const
00424 {
00425 return compression_;
00426 }
00427
00428
00429 compressionType compression(const compressionType cmp)
00430 {
00431 compressionType cmp0 = compression_;
00432 compression_ = cmp;
00433 return cmp0;
00434 }
00435
00436
00437 compressionType compression(const word& cmp)
00438 {
00439 compressionType cmp0 = compression_;
00440 compression_ = compressionEnum(cmp);
00441 return cmp0;
00442 }
00443
00444
00445 label lineNumber() const
00446 {
00447 return lineNumber_;
00448 }
00449
00450
00451 label& lineNumber()
00452 {
00453 return lineNumber_;
00454 }
00455
00456
00457 label lineNumber(const label ln)
00458 {
00459 label ln0 = lineNumber_;
00460 lineNumber_ = ln;
00461 return ln0;
00462 }
00463
00464
00465 virtual ios_base::fmtflags flags() const = 0;
00466
00467
00468 static unsigned int defaultPrecision()
00469 {
00470 return precision_;
00471 }
00472
00473
00474 static unsigned int defaultPrecision(unsigned int p)
00475 {
00476 unsigned int precision0 = precision_;
00477 precision_ = p;
00478 return precision0;
00479 }
00480
00481
00482 void setEof()
00483 {
00484 ioState_ |= ios_base::eofbit;
00485 }
00486
00487
00488 void setFail()
00489 {
00490 ioState_ |= ios_base::failbit;
00491 }
00492
00493
00494 void setBad()
00495 {
00496 ioState_ |= ios_base::badbit;
00497 }
00498
00499
00500 virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
00501
00502
00503 ios_base::fmtflags setf(const ios_base::fmtflags f)
00504 {
00505 return flags(flags() | f);
00506 }
00507
00508
00509 ios_base::fmtflags setf
00510 (
00511 const ios_base::fmtflags f,
00512 const ios_base::fmtflags mask
00513 )
00514 {
00515 return flags((flags() & ~mask) | (f & mask));
00516 }
00517
00518
00519 void unsetf(const ios_base::fmtflags uf)
00520 {
00521 flags(flags()&~uf);
00522 }
00523
00524
00525
00526
00527
00528 virtual void print(Ostream&) const;
00529
00530
00531 void print(Ostream&, const int streamState) const;
00532
00533
00534
00535
00536
00537
00538 InfoProxy<IOstream> info() const
00539 {
00540 return *this;
00541 }
00542 };
00543
00544
00545 Ostream& operator<<(Ostream& os, const IOstream::streamFormat& sf);
00546 Ostream& operator<<(Ostream& os, const IOstream::versionNumber& vn);
00547
00548
00549
00550
00551
00552
00553 typedef IOstream& (*IOstreamManip)(IOstream&);
00554
00555
00556 inline IOstream& operator<<(IOstream& io, IOstreamManip f)
00557 {
00558 return f(io);
00559 }
00560
00561
00562 inline IOstream& dec(IOstream& io)
00563 {
00564 io.setf(ios_base::dec, ios_base::dec|ios_base::hex|ios_base::oct);
00565 return io;
00566 }
00567
00568 inline IOstream& hex(IOstream& io)
00569 {
00570 io.setf(ios_base::hex, ios_base::dec|ios_base::hex|ios_base::oct);
00571 return io;
00572 }
00573
00574 inline IOstream& oct(IOstream& io)
00575 {
00576 io.setf(ios_base::oct, ios_base::dec|ios_base::hex|ios_base::oct);
00577 return io;
00578 }
00579
00580 inline IOstream& fixed(IOstream& io)
00581 {
00582 io.setf(ios_base::fixed, ios_base::floatfield);
00583 return io;
00584 }
00585
00586 inline IOstream& scientific(IOstream& io)
00587 {
00588 io.setf(ios_base::scientific, ios_base::floatfield);
00589 return io;
00590 }
00591
00592
00593
00594
00595 }
00596
00597
00598
00599 #endif
00600
00601
Copyright © 2000-2009 OpenCFD Ltd