OpendTect  6.3
fixedstreambuf.h
Go to the documentation of this file.
1 #ifndef fixedstreambuf_h
2 #define fixedstreambuf_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 #include <streambuf>
14 #include <istream>
15 #include <ostream>
16 
17 
18 namespace std
19 {
20 
32 mClass(Basic) fixedstreambuf : public streambuf
33 {
34 public:
35 
36 fixedstreambuf( char_type* b, off_type sz, bool manbuf=false )
37  : mine_(manbuf)
38  , newpos_(0)
39 {
40  setbuf( b, sz );
41 }
42 
44 {
45  if ( mine_ )
46  delete [] buf_;
47 }
48 
49 virtual fixedstreambuf* setbuf( char_type* b, streamsize n )
50 {
51  buf_ = b; sz_ = n;
52  setg( buf_, buf_, buf_ + sz_ );
53  setp( buf_, buf_ + sz_ );
54 
55  return this;
56 }
57 
58 virtual pos_type seekoff( off_type offs, ios_base::seekdir sd,
59  ios_base::openmode which )
60 {
61  newpos_ = offs;
62  if ( sd == ios_base::cur )
63  newpos_ += which == ios_base::in ? gptr() - buf_ : pptr() - buf_;
64  else if ( sd == ios_base::end )
65  newpos_ = sz_ + newpos_;
66 
67  return seekpos( newpos_, which );
68 }
69 
70 virtual pos_type seekpos( pos_type newpos, ios_base::openmode which )
71 {
72  if ( newpos_ < 0 || newpos_ >= sz_ )
73  newpos_ = -1;
74  else if ( which == ios_base::in )
75  setg( buf_, buf_+newpos_, buf_ + sz_ );
76  else
77  setp( buf_+newpos_, buf_ + sz_ );
78 
79  return newpos_;
80 }
81 
82 virtual streamsize xsgetn( char_type* s, streamsize n )
83 {
84  streamsize toget = n;
85  const od_int64 memsz = epptr() - pptr();
86  if ( toget > memsz && memsz >= 0 )
87  toget = memsz;
88 
89  OD::memCopy( s, gptr(), (size_t)toget );
90  gbump( (int) toget );
91 
92  return toget;
93 }
94 
95 virtual streamsize xsputn( const char_type* s, streamsize n )
96 {
97  streamsize toput = n;
98  const od_int64 memsz = epptr() - pptr();
99  if ( toput > memsz && memsz >= 0 )
100  toput = memsz;
101 
102  OD::memCopy( pptr(), s, (size_t)toput );
103  pbump( (int)toput );
104 
105  return toput;
106 }
107 
108  char_type* buf_;
109  off_type sz_;
110  bool mine_;
111  off_type newpos_;
112 };
113 
114 class fixedistream : public istream
115 {
116 public:
117 
119  : istream(sb)
120 {}
121 
123 { delete rdbuf(); }
124 
125 };
126 
127 class fixedostream : public ostream
128 {
129 public:
130 
132  : ostream(sb)
133 {}
134 
136 { delete rdbuf(); }
137 
138 };
139 
140 } // namespace std
141 
142 
143 #endif
~fixedstreambuf()
Definition: fixedstreambuf.h:43
virtual fixedstreambuf * setbuf(char_type *b, streamsize n)
Definition: fixedstreambuf.h:49
Definition: fixedstreambuf.h:114
off_type sz_
Definition: fixedstreambuf.h:109
#define od_int64
Definition: plftypes.h:34
char_type * buf_
Definition: fixedstreambuf.h:108
virtual pos_type seekoff(off_type offs, ios_base::seekdir sd, ios_base::openmode which)
Definition: fixedstreambuf.h:58
Definition: fixedstreambuf.h:18
~fixedistream()
Definition: fixedstreambuf.h:122
~fixedostream()
Definition: fixedstreambuf.h:135
virtual streamsize xsgetn(char_type *s, streamsize n)
Definition: fixedstreambuf.h:82
fixedostream(fixedstreambuf *sb)
Definition: fixedstreambuf.h:131
fixedstreambuf(char_type *b, off_type sz, bool manbuf=false)
Definition: fixedstreambuf.h:36
fixedistream(fixedstreambuf *sb)
Definition: fixedstreambuf.h:118
bool mine_
Definition: fixedstreambuf.h:110
Adapter to use a fixed buffer as a stream.
Definition: fixedstreambuf.h:32
Definition: fixedstreambuf.h:127
#define mClass(module)
Definition: commondefs.h:161
virtual streamsize xsputn(const char_type *s, streamsize n)
Definition: fixedstreambuf.h:95
off_type newpos_
Definition: fixedstreambuf.h:111
virtual pos_type seekpos(pos_type newpos, ios_base::openmode which)
Definition: fixedstreambuf.h:70

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