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
00044 #ifndef IOstream_H
00045 #define IOstream_H
00046
00047 #include "char.H"
00048 #include "bool.H"
00049 #include "label.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 format(const word&);
00382
00383
00384 streamFormat format() const
00385 {
00386 return format_;
00387 }
00388
00389
00390 versionNumber version() const
00391 {
00392 return version_;
00393 }
00394
00395
00396 static compressionType compression(const word&);
00397
00398
00399 compressionType compression() const
00400 {
00401 return compression_;
00402 }
00403
00404
00405 label lineNumber() const
00406 {
00407 return lineNumber_;
00408 }
00409
00410
00411 virtual ios_base::fmtflags flags() const = 0;
00412
00413
00414
00415
00416
00417 label& lineNumber()
00418 {
00419 return lineNumber_;
00420 }
00421
00422
00423 static unsigned int defaultPrecision()
00424 {
00425 return precision_;
00426 }
00427
00428
00429 static unsigned int defaultPrecision(unsigned int p)
00430 {
00431 unsigned int precision0 = precision_;
00432 precision_ = p;
00433 return precision0;
00434 }
00435
00436
00437 void set(const streamFormat fmt)
00438 {
00439 format_ = fmt;
00440 }
00441
00442
00443 void setFormat(const word& fmt)
00444 {
00445 format_ = format(fmt);
00446 }
00447
00448
00449 void set(const versionNumber version)
00450 {
00451 version_ = version;
00452 }
00453
00454
00455 void setVersion(const versionNumber version)
00456 {
00457 version_ = version;
00458 }
00459
00460
00461 void setCompression(const compressionType compression)
00462 {
00463 compression_ = compression;
00464 }
00465
00466
00467 void setEof()
00468 {
00469 ioState_ |= ios_base::eofbit;
00470 }
00471
00472
00473 void setFail()
00474 {
00475 ioState_ |= ios_base::failbit;
00476 }
00477
00478
00479 void setBad()
00480 {
00481 ioState_ |= ios_base::badbit;
00482 }
00483
00484
00485 virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
00486
00487
00488 ios_base::fmtflags setf(const ios_base::fmtflags f)
00489 {
00490 return flags(flags() | f);
00491 }
00492
00493
00494 ios_base::fmtflags setf
00495 (
00496 const ios_base::fmtflags f,
00497 const ios_base::fmtflags mask
00498 )
00499 {
00500 return flags((flags() & ~mask) | (f & mask));
00501 }
00502
00503
00504 void unsetf(const ios_base::fmtflags uf)
00505 {
00506 flags(flags()&~uf);
00507 }
00508
00509
00510
00511
00512
00513 virtual void print(Ostream&) const;
00514
00515
00516 void print(Ostream&, const int streamState) const;
00517
00518
00519
00520
00521
00522
00523 InfoProxy<IOstream> info() const
00524 {
00525 return *this;
00526 }
00527 };
00528
00529
00530
00531
00532
00533
00534 typedef IOstream& (*IOstreamManip)(IOstream&);
00535
00536
00537 inline IOstream& operator<<(IOstream& io, IOstreamManip f)
00538 {
00539 return f(io);
00540 }
00541
00542
00543 inline IOstream& dec(IOstream& io)
00544 {
00545 io.setf(ios_base::dec, ios_base::dec|ios_base::hex|ios_base::oct);
00546 return io;
00547 }
00548
00549 inline IOstream& hex(IOstream& io)
00550 {
00551 io.setf(ios_base::hex, ios_base::dec|ios_base::hex|ios_base::oct);
00552 return io;
00553 }
00554
00555 inline IOstream& oct(IOstream& io)
00556 {
00557 io.setf(ios_base::oct, ios_base::dec|ios_base::hex|ios_base::oct);
00558 return io;
00559 }
00560
00561 inline IOstream& fixed(IOstream& io)
00562 {
00563 io.setf(ios_base::fixed, ios_base::floatfield);
00564 return io;
00565 }
00566
00567 inline IOstream& scientific(IOstream& io)
00568 {
00569 io.setf(ios_base::scientific, ios_base::floatfield);
00570 return io;
00571 }
00572
00573
00574
00575
00576 }
00577
00578
00579
00580 #endif
00581
00582