OpendTect  6.6
winstreambuf.h
Go to the documentation of this file.
1 #ifndef winstreambuf_h
2 #define winstreambuf_h
3 /*
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: Bert
8  Date: Feb 2009
9 ________________________________________________________________________
10 
11 */
12 
13 #define private protected
14 #include <fstream>
15 #undef private
16 #ifdef __win32__
17 # define fpos_t od_int64
18 #endif
19 
20 #ifndef _POS_TYPE_FROM_STATE
21 //TODO this is a hack for VS >= 15.8.7, try to get rid of this winstreambuf hack altogether
22 #define _POS_TYPE_FROM_STATE(postype, state, position) postype(state, position)
23 #define _POS_TYPE_TO_STATE(pos) pos.state()
24 #define _BADOFF -1
25 #endif
26 
27 namespace std
28 {
29 
42 mClass(Basic) winfilebuf : public filebuf
43 {
44 public:
45 winfilebuf( const char* fnm, ios_base::openmode om )
46  : realpos_(0)
47 {
48  std::filebuf* openstrm = open( fnm, om );
49  isok_ = openstrm;
50 }
51 
52 bool isOK() const { return isok_; }
53 od_int64 getRealPos() const { return realpos_; }
54 protected:
55 
56 #define defom (ios_base::openmode)(ios_base::in | ios_base::out)
57 
58 virtual pos_type seekoff( off_type _Off, ios_base::seekdir _Way,
59  ios_base::openmode = defom )
60 {
61  fpos_t _Fileposition;
62 
63  if ( _Mysb::gptr() == &_Mychar && _Way == ios_base::cur && _Pcvt == 0 )
64  _Off -= (off_type)sizeof (char);
65 
66  if ( _Myfile==0 || !_Endwrite() || (_Off!=0 || _Way!=ios_base::cur) &&
67  _fseeki64(_Myfile,_Off,_Way) != 0 ||
68  fgetpos(_Myfile,&_Fileposition) != 0 )
69  return (pos_type(_BADOFF));
70 
71  if ( _Mysb::gptr() == &_Mychar )
72  _Mysb::setg( &_Mychar, &_Mychar+1, &_Mychar+1 );
73 
74  realpos_ = (od_int64)_Fileposition;
75  return (_POS_TYPE_FROM_STATE(pos_type,_State,_Fileposition));
76 }
77 
78 bool isok_;
80 };
81 
82 
87 mClass(Basic) winifstream : public istream
88 {
89 public:
90 
91 winifstream( const char* fnm, ios_base::openmode om )
92  : istream(nullptr)
93 {
94  fb_ = new winfilebuf( fnm, om );
95  rdbuf( fb_ );
96 
97  if ( fb_->isOK() )
98  clear();
99  else
100  setstate( ios_base::failbit );
101 }
102 
104 {
105  if ( !fb_->close() )
106  setstate( ios_base::failbit );
107  delete fb_;
108 }
109 
110 bool is_open()
111 { return fb_->is_open(); }
112 
114 };
115 
116 
121 mClass(Basic) winofstream : public ostream
122 {
123 public:
124 
125 winofstream( const char* fnm, ios_base::openmode om )
126  : ostream(nullptr)
127 {
128  fb_ = new winfilebuf( fnm, om );
129  rdbuf( fb_ );
130 
131  if ( fb_->isOK() )
132  clear();
133  else
134  setstate( ios_base::failbit );
135 }
136 
138 {
139  if ( !fb_->close() )
140  setstate( ios_base::failbit );
141  delete fb_;
142 }
143 
144 bool is_open()
145 { return fb_->is_open(); }
146 
148 };
149 
150 
151 } // namespace std
152 
153 
154 #endif
std::winfilebuf::seekoff
virtual pos_type seekoff(off_type _Off, ios_base::seekdir _Way, ios_base::openmode=(ios_base::openmode)(ios_base::in|ios_base::out))
Definition: winstreambuf.h:58
std::winfilebuf::getRealPos
od_int64 getRealPos() const
Definition: winstreambuf.h:53
std::winifstream::~winifstream
~winifstream()
Definition: winstreambuf.h:103
od_int64
#define od_int64
Definition: plftypes.h:35
std::winifstream
Adapter for input streams on win64.
Definition: winstreambuf.h:88
std::winofstream::is_open
bool is_open()
Definition: winstreambuf.h:144
std::winofstream::winofstream
winofstream(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:125
std::winofstream::fb_
winfilebuf * fb_
Definition: winstreambuf.h:147
std::winifstream::is_open
bool is_open()
Definition: winstreambuf.h:110
std::winfilebuf::isok_
bool isok_
Definition: winstreambuf.h:78
mClass
#define mClass(module)
Definition: commondefs.h:181
std::winifstream::fb_
winfilebuf * fb_
Definition: winstreambuf.h:113
std::winfilebuf::isOK
bool isOK() const
Definition: winstreambuf.h:52
std::winfilebuf::winfilebuf
winfilebuf(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:45
defom
#define defom
Definition: winstreambuf.h:56
std::winfilebuf::realpos_
od_int64 realpos_
Definition: winstreambuf.h:79
std
Definition: fixedstreambuf.h:20
std::winofstream::~winofstream
~winofstream()
Definition: winstreambuf.h:137
std::winfilebuf
Adapter to seek in filebuffers on win64.
Definition: winstreambuf.h:43
_BADOFF
#define _BADOFF
Definition: winstreambuf.h:24
std::winofstream
Adapter for output streams on win64.
Definition: winstreambuf.h:122
StrmOper::clear
void clear(std::ios &)
std::winifstream::winifstream
winifstream(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:91
_POS_TYPE_FROM_STATE
#define _POS_TYPE_FROM_STATE(postype, state, position)
Definition: winstreambuf.h:22

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021