OpendTect-6_4  6.4
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  isok_ = open( fnm, om );
49 }
50 
51 bool isOK() const { return isok_; }
52 od_int64 getRealPos() const { return realpos_; }
53 protected:
54 
55 #define defom (ios_base::openmode)(ios_base::in | ios_base::out)
56 
57 virtual pos_type seekoff( off_type _Off, ios_base::seekdir _Way,
58  ios_base::openmode = defom )
59 {
60  fpos_t _Fileposition;
61 
62  if ( _Mysb::gptr() == &_Mychar && _Way == ios_base::cur && _Pcvt == 0 )
63  _Off -= (off_type)sizeof (char);
64 
65  if ( _Myfile==0 || !_Endwrite() || (_Off!=0 || _Way!=ios_base::cur) &&
66  _fseeki64(_Myfile,_Off,_Way) != 0 ||
67  fgetpos(_Myfile,&_Fileposition) != 0 )
68  return (pos_type(_BADOFF));
69 
70  if ( _Mysb::gptr() == &_Mychar )
71  _Mysb::setg( &_Mychar, &_Mychar+1, &_Mychar+1 );
72 
73  realpos_ = (od_int64)_Fileposition;
74  return (_POS_TYPE_FROM_STATE(pos_type,_State,_Fileposition));
75 }
76 
77 bool isok_;
79 };
80 
81 
86 mClass(Basic) winifstream : public istream
87 {
88 public:
89 
90 winifstream( const char* fnm, ios_base::openmode om )
91  : istream(0)
92 {
93  fb_ = new winfilebuf( fnm, om );
94  rdbuf( fb_ );
95 
96  if ( fb_->isOK() )
97  clear();
98  else
99  setstate( ios_base::failbit );
100 }
101 
103 {
104  if ( !fb_->close() )
105  setstate( ios_base::failbit );
106 }
107 
108 bool is_open()
109 { return fb_->is_open(); }
110 
112 };
113 
114 
119 mClass(Basic) winofstream : public ostream
120 {
121 public:
122 
123 winofstream( const char* fnm, ios_base::openmode om )
124  : ostream(0)
125 {
126  fb_ = new winfilebuf( fnm, om );
127  rdbuf( fb_ );
128 
129  if ( fb_->isOK() )
130  clear();
131  else
132  setstate( ios_base::failbit );
133 }
134 
136 {
137  if ( !fb_->close() )
138  setstate( ios_base::failbit );
139 }
140 
141 bool is_open()
142 { return fb_->is_open(); }
143 
145 };
146 
147 
148 } // namespace std
149 
150 
151 #endif
#define od_int64
Definition: plftypes.h:36
Adapter for output streams on win64.
Definition: winstreambuf.h:119
void clear(std::ios &)
~winofstream()
Definition: winstreambuf.h:135
Definition: fixedstreambuf.h:17
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:57
#define defom
Definition: winstreambuf.h:55
winifstream(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:90
winfilebuf * fb_
Definition: winstreambuf.h:111
Adapter for input streams on win64.
Definition: winstreambuf.h:86
od_int64 realpos_
Definition: winstreambuf.h:78
bool isok_
Definition: winstreambuf.h:77
Adapter to seek in filebuffers on win64.
Definition: winstreambuf.h:42
#define _POS_TYPE_FROM_STATE(postype, state, position)
Definition: winstreambuf.h:22
od_int64 getRealPos() const
Definition: winstreambuf.h:52
#define _BADOFF
Definition: winstreambuf.h:24
bool is_open()
Definition: winstreambuf.h:141
winofstream(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:123
winfilebuf * fb_
Definition: winstreambuf.h:144
bool is_open()
Definition: winstreambuf.h:108
winfilebuf(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:45
#define mClass(module)
Definition: commondefs.h:164
bool isOK() const
Definition: winstreambuf.h:51
~winifstream()
Definition: winstreambuf.h:102

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