Main MRPT website > C++ reference for MRPT 1.5.9
CMemoryStream.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 
16 #include <mrpt/system/os.h>
17 #include <cstring>
18 
19 using namespace mrpt::utils;
20 using namespace std;
21 
22 /*---------------------------------------------------------------
23  Constructor
24  ---------------------------------------------------------------*/
26  m_memory (NULL),
27  m_size (0),
28  m_position (0),
29  m_bytesWritten(0),
30  m_alloc_block_size(0x1000),
31  m_read_only (false)
32 {
33 }
34 
35 /*---------------------------------------------------------------
36  Constructor with data
37  ---------------------------------------------------------------*/
39  const void *data,
40  const uint64_t nBytesInData ) :
41  m_memory (NULL),
42  m_size (0),
43  m_position (0),
44  m_bytesWritten(0),
45  m_alloc_block_size(0x1000),
46  m_read_only (false)
47 {
49  ASSERT_(data!=NULL);
50 
51  // Set data:
52  resize( nBytesInData );
53  memcpy( m_memory.get(),data,nBytesInData );
54 
55  m_bytesWritten=nBytesInData;
56 
57  MRPT_END
58 }
59 
60 /*---------------------------------------------------------------
61  assignMemoryNotOwn
62  ---------------------------------------------------------------*/
63 void CMemoryStream::assignMemoryNotOwn( const void *data, const uint64_t nBytesInData )
64 {
65  this->Clear();
66  m_memory.set(data);
67  m_size = nBytesInData;
68  m_position = 0;
69  m_bytesWritten = 0;
70  m_read_only = true;
71 }
72 
73 /*---------------------------------------------------------------
74  Destructor
75  ---------------------------------------------------------------*/
77 {
78  if (!m_read_only)
79  {
80  // Free memory buffer:
81  resize(0);
82  }
83 }
84 
85 /*---------------------------------------------------------------
86  resize
87  ---------------------------------------------------------------*/
89 {
90  if (m_read_only)
91  THROW_EXCEPTION("[CMemoryStream::resize] Cannot change memory block size since it was set with 'assign'")
92 
93  if (!newSize)
94  { // Free buffer:
95  if (m_memory.get())
96  free(m_memory.get());
97  m_memory=NULL;
98  m_size=0;
99  m_position=0;
100  }
101  else
102  { // Resize:
103  m_memory.set( realloc( m_memory.get(), newSize ));
104 
105  // Check for non-memory errors??
106  if (newSize) ASSERT_(m_memory.get());
107 
108  m_size = newSize;
109  }
110 
112 }
113 
114 /*---------------------------------------------------------------
115  Read
116  Reads bytes from the stream into Buffer
117  ---------------------------------------------------------------*/
118 size_t CMemoryStream::Read(void *Buffer, size_t Count)
119 {
120  // Enought bytes?
121  long maxAvail = (((long)m_size)) - ((long)m_position);
122  size_t nToRead = (size_t) min(((long)Count),maxAvail);
123 
124  // Copy the memory block:
125  if (nToRead>0)
126  memcpy(Buffer, ((char*)m_memory.get()) + m_position, nToRead );
127 
128  // Update cursor position:
129  m_position+=nToRead;
130  return nToRead;
131 }
132 
133 /*---------------------------------------------------------------
134  Write
135  Writes a block of bytes to the stream.
136  ---------------------------------------------------------------*/
137 size_t CMemoryStream::Write(const void *Buffer, size_t Count)
138 {
139  // Enought space in current bufer?
140  size_t requiredSize = m_position + Count;
141 
142  if (requiredSize>=m_size)
143  {
144  // Incrent the size of reserved memory:
145  resize( requiredSize + m_alloc_block_size );
146  }
147 
148  // Copy the memory block:
149  memcpy(((char*)m_memory.get()) + m_position, Buffer, Count );
150 
151  // New cursor position:
152  m_position = requiredSize;
153 
155 
156  return Count;
157 }
158 
159 /*---------------------------------------------------------------
160  Seek
161  Method for moving to a specified position in the streamed resource.
162  See documentation of CStream::Seek
163  ---------------------------------------------------------------*/
165 {
166  switch (Origin)
167  {
168  case sFromBeginning:
169  m_position = Offset;
170  break;
171  case sFromCurrent:
172  m_position += Offset;
173  break;
174  case sFromEnd:
175  m_position = m_bytesWritten - 1 + Origin;
176  break;
177  };
178 
180 
181  return m_position;
182 }
183 
184 /*---------------------------------------------------------------
185  getTotalBytesCount
186  ---------------------------------------------------------------*/
188 {
189  return m_bytesWritten;
190 }
191 
192 /*---------------------------------------------------------------
193  getPosition
194  ---------------------------------------------------------------*/
196 {
197  return m_position;
198 }
199 
200 /*---------------------------------------------------------------
201  Clear
202  ---------------------------------------------------------------*/
204 {
205  if (!m_read_only)
206  {
207  resize(0);
208  }
209  else
210  {
211  m_memory=0;
212  m_size = 0;
213  m_position=0;
214  m_bytesWritten=0;
215  m_read_only = false;
216  }
217 }
218 
219 /*---------------------------------------------------------------
220  getRawBufferData
221  Method for getting a pointer to the raw stored data. The
222  lenght in bytes is given by getTotalBytesCount
223  ---------------------------------------------------------------*/
225 {
226  return m_memory.get();
227 }
228 
229 /*---------------------------------------------------------------
230  changeSize
231 Change size. This would be rarely used
232  Use ">>" operators for writing to stream.
233  ---------------------------------------------------------------*/
235 {
236  resize(newSize);
237 }
238 
239 /*---------------------------------------------------------------
240  saveBufferToFile
241  ---------------------------------------------------------------*/
243 {
244  try
245  {
246  CFileOutputStream fo(file_name);
248  return true;
249  }
250  catch(...)
251  {
252  return false;
253  }
254 }
255 
256 /*---------------------------------------------------------------
257  loadBufferFromFile
258  ---------------------------------------------------------------*/
260 {
261  try
262  {
263  CFileInputStream fi(file_name);
264  uint64_t N = fi.getTotalBytesCount();
265 
266  // Read into the buffer:
267  Clear();
268  resize(N+100);
269  uint64_t N_read = fi.ReadBuffer( m_memory.get(), N );
270 
271  m_position = N_read;
273 
274  return N_read==N;
275  }
276  catch(...)
277  {
278  return false;
279  }
280 }
281 
282 
283 // Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct, to be freed here.
284 void mrpt::utils::internal::free_fn_for_zmq(void * /* data*/, void *hint)
285 {
287  if (fd->do_free) delete fd->buf;
288  delete fd;
289 }
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
Definition: os.cpp:358
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
Definition: CStream.cpp:45
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#define min(a, b)
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
#define THROW_EXCEPTION(msg)
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:67
STL namespace.
void resize(uint64_t newSize)
Resizes the internal buffer size.
#define MRPT_END
virtual ~CMemoryStream()
Destructor.
void Clear()
Clears the memory buffer.
This CStream derived class allow using a file as a write-only, binary stream.
__int64 int64_t
Definition: rptypes.h:51
bool saveBufferToFile(const std::string &file_name)
Saves the entire buffer to a file.
uint64_t getPosition() MRPT_OVERRIDE
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
GLsizei const GLchar ** string
Definition: glext.h:3919
size_t m_size
Number of elements accessed with write access so far.
Definition: ts_hash_map.h:103
void BASE_IMPEXP free_fn_for_zmq(void *data, void *hint)
Used in mrpt_send_to_zmq(). hint points to a TFreeFnDataForZMQ struct, to be freed here...
bool m_read_only
If the memory block does not belong to the object.
Definition: CMemoryStream.h:36
void * getRawBufferData()
Method for getting a pointer to the raw stored data.
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:52
void assignMemoryNotOwn(const void *data, const uint64_t nBytesInData)
Initilize the data in the stream from a block of memory which is NEITHER OWNED NOR COPIED by the obje...
size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for writing to the stream.
CMemoryStream()
Default constructor.
void changeSize(uint64_t newSize)
Change size. This would be rarely used. Use ">>" operators for writing to stream. ...
#define ASSERT_(f)
uint64_t getTotalBytesCount() MRPT_OVERRIDE
Returns the total size of the internal buffer.
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) MRPT_OVERRIDE
Introduces a pure virtual method for moving to a specified position in the streamed resource...
This CStream derived class allow using a file as a read-only, binary stream.
void set(const T *p)
This method can change the pointer, since the change is made explicitly, not through copy operators t...
uint64_t getTotalBytesCount() MRPT_OVERRIDE
Method for getting the total number of bytes in the buffer.
void_ptr_noncopy m_memory
Internal data.
Definition: CMemoryStream.h:33
size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for reading from the stream.
bool loadBufferFromFile(const std::string &file_name)
Loads the entire buffer from a file *.
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020