OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimByteStreamBuffer.cpp
Go to the documentation of this file.
2 #include <cstring> /* for memcpy */
3 
5  :m_buffer(0),
6  m_bufferSize(0),
7  m_sharedBuffer(false)
8 {
10 }
11 
13  ossim_int64 bufSize,
14  bool shared)
15  :m_buffer(0),
16  m_bufferSize(0),
17  m_sharedBuffer(false)
18 {
19  setBuf(buf, bufSize, shared);
20 }
21 
23  :m_buffer(0),
24  m_bufferSize(0),
25  m_sharedBuffer(false)
26 {
28 
29  if(src.m_buffer&&src.m_bufferSize)
30  {
31  // now align pointers to the same offset locations
32  //
33  ossim_int64 goff = src.gptr()-src.eback();
34  ossim_int64 poff = src.pptr()-src.pbase();
37  pbump(poff);
38  }
39 }
40 
42 {
43  deleteBuffer();
44 }
45 std::streambuf* ossimByteStreamBuffer::setbuf ( char_type * s, std::streamsize n )
46 {
47  return setBuf(s, n, false);
48 }
49 
51 {
52  setbuf(0,0);
53 }
54 // added so we can set a buffer and make it shared
55 std::streambuf* ossimByteStreamBuffer::setBuf(char* buf, std::streamsize bufSize, bool shared)
56 {
57 
58  deleteBuffer();
59  setp(0,0);
60  setg(0,0,0);
61  char_type* tempBuf = buf;
62  if(!shared&&bufSize&&buf)
63  {
64  tempBuf = new char_type[bufSize];
65  memcpy(tempBuf, buf, bufSize);
66  }
67  m_buffer = tempBuf;
68  m_sharedBuffer = shared;
69  m_bufferSize = bufSize;
70  setp(m_buffer, m_buffer+bufSize);
71  if(m_buffer)
72  {
73  setg(m_buffer, m_buffer, m_buffer+bufSize);
74  }
75 
76  return this;
77 }
78 
80 {
81  if(m_sharedBuffer)
82  {
83  return EOF;
84  }
85  else
86  {
87  ossim_uint32 oldSize = m_bufferSize;
88  extendBuffer(1);
89  pbump(1);
90  m_buffer[oldSize] = (char_type)c;
91  }
92 
93  return c;
94 }
95 
96 ossimByteStreamBuffer::char_type* ossimByteStreamBuffer::buffer(){return m_buffer;}
97 const ossimByteStreamBuffer::char_type* ossimByteStreamBuffer::buffer()const{return m_buffer;}
98 
102 ossimByteStreamBuffer::char_type* ossimByteStreamBuffer::takeBuffer()
103 {
104  char_type* result = m_buffer;
105  setp(0,0);
106  setg(0,0,0);
107  m_bufferSize = 0;
108  m_buffer = 0;
109 
110  return result;
111 }
112 
114 
115 
116 ossimByteStreamBuffer::int_type ossimByteStreamBuffer::pbackfail(int_type __c )
117 {
118  int_type result = __c;
119  ossim_int64 delta = gptr()-eback();
120  if(delta!=0)
121  {
122  setg(m_buffer, m_buffer+(delta-1), m_buffer+m_bufferSize);
123  if(__c != traits_type::eof())
124  {
125  *gptr() = static_cast<char_type>(__c);
126  }
127  }
128  else
129  {
130  result = traits_type::eof();
131  }
132  return result;
133 }
134 ossimByteStreamBuffer::pos_type ossimByteStreamBuffer::seekoff(off_type offset, std::ios_base::seekdir dir,
135  std::ios_base::openmode __mode)
136 {
137  pos_type result = pos_type(off_type(-1));
138  if((__mode & std::ios_base::in)&&
139  (__mode & std::ios_base::out))
140  {
141  // we currently will not support both input and output stream at the same time
142  //
143  return result;
144  }
145  switch(dir)
146  {
147  case std::ios_base::beg:
148  {
149  // if we are determing an absolute position from the beginning then
150  // just make sure the offset is within range of the current buffer size
151  //
152  if((offset < m_bufferSize)&&
153  (offset >=0))
154  {
155  result = pos_type(offset);
156  }
157  if(__mode & std::ios_base::in)
158  {
159  gbump(offset - (gptr() - eback()));
160  }
161  else if(__mode & std::ios_base::out)
162  {
163  pbump(offset - (pptr() - pbase()));
164  }
165  break;
166  }
167  case std::ios_base::cur:
168  {
169  // if we are determing an absolute position from the current pointer then
170  // add the offset as a relative displacment
171  //
172  pos_type newPosition = result;
173  ossim_int64 delta = 0;
174  if(__mode & std::ios_base::in)
175  {
176  delta = gptr()-eback();
177  }
178  else if(__mode & std::ios_base::out)
179  {
180  delta = pptr()-pbase();
181  }
182  newPosition = pos_type(delta + offset);
183  if((newPosition >= 0)&&(newPosition < m_bufferSize))
184  {
185  result = newPosition;
186  if(__mode & std::ios_base::in)
187  {
188  gbump(offset);
189  }
190  else if(__mode & std::ios_base::out)
191  {
192  pbump(offset);
193  }
194  }
195  break;
196  }
197  case std::ios_base::end:
198  {
199  pos_type newPosition = result;
200  ossim_int64 delta = 0;
201  if(__mode & std::ios_base::in)
202  {
203  delta = egptr()-eback();
204  }
205  else if(__mode & std::ios_base::out)
206  {
207  delta = epptr()-pbase();
208  }
209  newPosition = pos_type(delta + offset);
210  if((newPosition >= 0)&&(newPosition < m_bufferSize))
211  {
212  result = newPosition;
213  if(__mode & std::ios_base::in)
214  {
215  gbump(offset - (gptr() - eback()));
216  }
217  else if(__mode & std::ios_base::out)
218  {
219  pbump(offset - (epptr() - pptr()));
220  }
221  }
222  break;
223  }
224  default:
225  {
226  break;
227  }
228  }
229  return result;
230 }
231 
232 ossimByteStreamBuffer::pos_type ossimByteStreamBuffer::seekpos(pos_type pos, std::ios_base::openmode __mode)
233 {
234  pos_type result = pos_type(off_type(-1));
235 
236  if(__mode & std::ios_base::in)
237  {
238  if(pos >= 0)
239  {
240  if(pos < m_bufferSize)
241  {
242  setg(m_buffer, m_buffer + pos, m_buffer + m_bufferSize);
243  result = pos;
244  }
245  }
246  }
247  else if(__mode & std::ios_base::out)
248  {
249  if(pos >=0)
250  {
252  if(pos < m_bufferSize)
253  {
254  pbump(pos);
255  result = pos;
256  }
257  else if(!m_sharedBuffer)
258  {
259  ossim_int64 delta = ossim_int64(pos) - m_bufferSize;
260  if(delta > 0)
261  {
262  extendBuffer(delta+1);
263  pbump(pos);
264  result = pos;
265  }
266  }
267  }
268  }
269 
270  return result;
271 }
272 
273 std::streamsize ossimByteStreamBuffer::xsgetn(char_type* __s, std::streamsize __n)
274 {
275  ossim_uint64 bytesLeftToRead = egptr()-gptr();
276  ossim_uint64 bytesToRead = __n;
277 
278  if(bytesToRead > bytesLeftToRead)
279  {
280  bytesToRead = bytesLeftToRead;
281  }
282  if(bytesToRead)
283  {
284  std::memcpy(__s, gptr(), bytesToRead);
285  gbump(bytesToRead); // bump the current get pointer
286  }
287  return std::streamsize(bytesToRead);
288 }
289 
290 std::streamsize ossimByteStreamBuffer::xsputn(const char_type* __s, std::streamsize __n)
291 {
292  ossim_int64 bytesLeftToWrite = epptr()-pptr();
293  ossim_int64 bytesToWrite = __n;
294  if(__n > bytesLeftToWrite)
295  {
296  if(!m_sharedBuffer)
297  {
298  extendBuffer(__n-bytesLeftToWrite);
299  }
300  else
301  {
302  bytesToWrite = bytesLeftToWrite;
303  }
304  }
305  if(bytesToWrite)
306  {
307  std::memcpy(pptr(), __s, bytesToWrite);
308  pbump(bytesToWrite);
309  }
310 
311  return bytesToWrite;
312 }
313 
315 {
317  {
318  delete [] m_buffer;
319  }
320  m_buffer = 0;
321  m_bufferSize=0;
322 }
324 {
325  // ossim_uint32 oldSize = m_bufferSize;
326  char* inBegin = eback();
327  char* inCur = gptr();
328  ossim_uint64 pbumpOffset = pptr()-pbase();
329 
330  ossim_int64 relativeInCur = inCur-inBegin;
331 
332 
333  if(!m_buffer)
334  {
335  if(bytes>0)
336  {
337  m_buffer = new char[m_bufferSize + bytes];
338  }
339  }
340  else
341  {
342  if(bytes>0)
343  {
344  char* newBuf = new char[m_bufferSize + bytes];
345  std::memcpy(newBuf, m_buffer, m_bufferSize);
346  delete [] m_buffer;
347  m_buffer = newBuf;
348  }
349  }
350  m_bufferSize += bytes;
351 
353  setg(m_buffer, m_buffer+relativeInCur, m_buffer + m_bufferSize);
354  pbump(pbumpOffset); // reallign to the current location
355 }
virtual std::streambuf * setbuf(char_type *s, std::streamsize n)
virtual std::streamsize xsputn(const char_type *__s, std::streamsize __n)
virtual std::streamsize xsgetn(char_type *__s, std::streamsize __n)
ossim_uint64 bufferSize() const
char_type * takeBuffer()
The buffer is no longer managed by this stream buffer and is removed.
virtual int overflow(int c=EOF)
char_type * buffer()
Returns a pointer to the buffer.
virtual pos_type seekoff(off_type offset, std::ios_base::seekdir dir, std::ios_base::openmode __mode=std::ios_base::in|std::ios_base::out)
os2<< "> n<< " > nendobj n
unsigned long long ossim_uint64
unsigned int ossim_uint32
virtual pos_type seekpos(pos_type pos, std::ios_base::openmode __mode=std::ios_base::in|std::ios_base::out)
std::streambuf * setBuf(char *buf, std::streamsize bufSize, bool shared)
long long ossim_int64
void extendBuffer(ossim_uint64 bytes)
virtual int_type pbackfail(int_type __c=traits_type::eof())