OpendTect  6.3
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 namespace std
21 {
22 
35 mClass(Basic) winfilebuf : public filebuf
36 {
37 public:
38 winfilebuf( const char* fnm, ios_base::openmode om )
39  : realpos_(0)
40 {
41  isok_ = open( fnm, om );
42 }
43 
44 bool isOK() const { return isok_; }
45 od_int64 getRealPos() const { return realpos_; }
46 protected:
47 
48 #define defom (ios_base::openmode)(ios_base::in | ios_base::out)
49 
50 virtual pos_type seekoff( off_type _Off, ios_base::seekdir _Way,
51  ios_base::openmode = defom )
52 {
53  fpos_t _Fileposition;
54 
55  if ( _Mysb::gptr() == &_Mychar && _Way == ios_base::cur && _Pcvt == 0 )
56  _Off -= (off_type)sizeof (char);
57 
58  if ( _Myfile==0 || !_Endwrite() || (_Off!=0 || _Way!=ios_base::cur) &&
59  _fseeki64(_Myfile,_Off,_Way) != 0 ||
60  fgetpos(_Myfile,&_Fileposition) != 0 )
61  return (pos_type(_BADOFF));
62 
63  if ( _Mysb::gptr() == &_Mychar )
64  _Mysb::setg( &_Mychar, &_Mychar+1, &_Mychar+1 );
65 
66  realpos_ = (od_int64)_Fileposition;
67  return (_POS_TYPE_FROM_STATE(pos_type,_State,_Fileposition));
68 }
69 
70 
71 virtual pos_type seekpos( pos_type _Pos, ios_base::openmode = defom )
72 
73 {
74  fpos_t _Fileposition = _POS_TYPE_TO_FPOS_T(_Pos);
75  off_type _Off = (off_type)_Pos - _FPOSOFF(_Fileposition);
76 
77  if ( _Myfile == 0 || !_Endwrite()
78  || fsetpos(_Myfile, &_Fileposition) != 0
79  || _Off != 0 && _fseeki64(_Myfile, _Off, SEEK_CUR) != 0
80  || fgetpos(_Myfile, &_Fileposition) != 0)
81  return (pos_type(_BADOFF));
82 
83  _State = _POS_TYPE_TO_STATE(_Pos);
84 
85  if ( _Mysb::gptr() == &_Mychar)
86  _Mysb::setg( &_Mychar, &_Mychar+1, &_Mychar+1 );
87 
88  realpos_ = (od_int64)_Fileposition;
89  return (_POS_TYPE_FROM_STATE(pos_type,_State,_Fileposition));
90 }
91 
92 bool isok_;
94 };
95 
96 
101 mClass(Basic) winifstream : public istream
102 {
103 public:
104 
105 winifstream( const char* fnm, ios_base::openmode om )
106  : istream(0)
107 {
108  fb_ = new winfilebuf( fnm, om );
109  rdbuf( fb_ );
110 
111  if ( fb_->isOK() )
112  clear();
113  else
114  setstate( ios_base::failbit );
115 }
116 
118 {
119  if ( !fb_->close() )
120  setstate( ios_base::failbit );
121 }
122 
123 bool is_open()
124 { return fb_->is_open(); }
125 
127 };
128 
129 
134 mClass(Basic) winofstream : public ostream
135 {
136 public:
137 
138 winofstream( const char* fnm, ios_base::openmode om )
139  : ostream(0)
140 {
141  fb_ = new winfilebuf( fnm, om );
142  rdbuf( fb_ );
143 
144  if ( fb_->isOK() )
145  clear();
146  else
147  setstate( ios_base::failbit );
148 }
149 
151 {
152  if ( !fb_->close() )
153  setstate( ios_base::failbit );
154 }
155 
156 bool is_open()
157 { return fb_->is_open(); }
158 
160 };
161 
162 
163 } // namespace std
164 
165 
166 #endif
#define od_int64
Definition: plftypes.h:34
Adapter for output streams on win64.
Definition: winstreambuf.h:134
void clear(std::ios &)
~winofstream()
Definition: winstreambuf.h:150
Definition: fixedstreambuf.h:18
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:50
#define defom
Definition: winstreambuf.h:48
winifstream(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:105
winfilebuf * fb_
Definition: winstreambuf.h:126
Adapter for input streams on win64.
Definition: winstreambuf.h:101
od_int64 realpos_
Definition: winstreambuf.h:93
bool isok_
Definition: winstreambuf.h:92
Adapter to seek in filebuffers on win64.
Definition: winstreambuf.h:35
od_int64 getRealPos() const
Definition: winstreambuf.h:45
bool is_open()
Definition: winstreambuf.h:156
winofstream(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:138
winfilebuf * fb_
Definition: winstreambuf.h:159
bool is_open()
Definition: winstreambuf.h:123
winfilebuf(const char *fnm, ios_base::openmode om)
Definition: winstreambuf.h:38
virtual pos_type seekpos(pos_type _Pos, ios_base::openmode=(ios_base::openmode)(ios_base::in|ios_base::out))
Definition: winstreambuf.h:71
#define mClass(module)
Definition: commondefs.h:161
bool isOK() const
Definition: winstreambuf.h:44
~winifstream()
Definition: winstreambuf.h:117

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