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

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