OpendTect-6_4  6.4
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  RCS: $Id$
10 ________________________________________________________________________
11 
12 */
13 
14 #include <streambuf>
15 
16 
17 namespace std
18 {
19 
31 mClass(Basic) fixedstreambuf : public streambuf
32 {
33 public:
34 
35 fixedstreambuf( char_type* b, off_type sz, bool manbuf=false )
36  : mine_(manbuf)
37  , newpos_(0)
38 {
39  setbuf( b, sz );
40 }
41 
43 {
44  if ( mine_ )
45  delete [] buf_;
46 }
47 
48 virtual fixedstreambuf* setbuf( char_type* b, streamsize n )
49 {
50  buf_ = b; sz_ = n;
51  setg( buf_, buf_, buf_ + sz_ );
52  setp( buf_, buf_ + sz_ );
53 
54  return this;
55 }
56 
57 virtual pos_type seekoff( off_type offs, ios_base::seekdir sd,
58  ios_base::openmode which )
59 {
60  newpos_ = offs;
61  if ( sd == ios_base::cur )
62  newpos_ += which == ios_base::in ? gptr() - buf_ : pptr() - buf_;
63  else if ( sd == ios_base::end )
64  newpos_ = sz_ + newpos_;
65 
66  return seekpos( newpos_, which );
67 }
68 
69 virtual pos_type seekpos( pos_type newpos, ios_base::openmode which )
70 {
71  if ( newpos_ < 0 || newpos_ >= sz_ )
72  newpos_ = -1;
73  else if ( which == ios_base::in )
74  setg( buf_, buf_+newpos_, buf_ + sz_ );
75  else
76  setp( buf_+newpos_, buf_ + sz_ );
77 
78  return newpos_;
79 }
80 
81 virtual streamsize xsgetn( char_type* s, streamsize n )
82 {
83  streamsize toget = n;
84  const od_int64 memsz = epptr() - pptr();
85  if ( toget > memsz && memsz >= 0 )
86  toget = memsz;
87 
88  OD::memCopy( s, gptr(), (size_t)toget );
89  gbump( (int) toget );
90 
91  return toget;
92 }
93 
94 virtual streamsize xsputn( const char_type* s, streamsize n )
95 {
96  streamsize toput = n;
97  const od_int64 memsz = epptr() - pptr();
98  if ( toput > memsz && memsz >= 0 )
99  toput = memsz;
100 
101  OD::memCopy( pptr(), s, (size_t)toput );
102  pbump( (int)toput );
103 
104  return toput;
105 }
106 
107  char_type* buf_;
108  off_type sz_;
109  bool mine_;
110  off_type newpos_;
111 };
112 
113 } // namespace std
114 
115 
116 #endif
~fixedstreambuf()
Definition: fixedstreambuf.h:42
virtual fixedstreambuf * setbuf(char_type *b, streamsize n)
Definition: fixedstreambuf.h:48
off_type sz_
Definition: fixedstreambuf.h:108
#define od_int64
Definition: plftypes.h:36
char_type * buf_
Definition: fixedstreambuf.h:107
virtual pos_type seekoff(off_type offs, ios_base::seekdir sd, ios_base::openmode which)
Definition: fixedstreambuf.h:57
Definition: fixedstreambuf.h:17
virtual streamsize xsgetn(char_type *s, streamsize n)
Definition: fixedstreambuf.h:81
fixedstreambuf(char_type *b, off_type sz, bool manbuf=false)
Definition: fixedstreambuf.h:35
bool mine_
Definition: fixedstreambuf.h:109
Adapter to use a fixed buffer as a stream.
Definition: fixedstreambuf.h:31
#define mClass(module)
Definition: commondefs.h:164
virtual streamsize xsputn(const char_type *s, streamsize n)
Definition: fixedstreambuf.h:94
off_type newpos_
Definition: fixedstreambuf.h:110
virtual pos_type seekpos(pos_type newpos, ios_base::openmode which)
Definition: fixedstreambuf.h:69

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