MRPT  1.9.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-2018, 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 "io-precomp.h" // Precompiled headers
11 
12 #include <mrpt/io/CMemoryStream.h>
15 #include <algorithm> // min,max
16 #include <cstring> // memcpy
17 
18 using namespace mrpt::io;
19 using std::max;
20 using std::min;
21 
22 CMemoryStream::CMemoryStream(const void* data, const uint64_t nBytesInData)
23 {
25  ASSERT_(data != nullptr);
26 
27  // Set data:
28  resize(nBytesInData);
29  memcpy(m_memory.get(), data, nBytesInData);
30 
31  m_bytesWritten = nBytesInData;
32 
33  MRPT_END
34 }
35 
37  const void* data, const uint64_t nBytesInData)
38 {
39  this->Clear();
40  m_memory.set(data);
41  m_size = nBytesInData;
42  m_position = 0;
43  m_bytesWritten = 0;
44  m_read_only = true;
45 }
46 
48 {
49  if (!m_read_only)
50  {
51  // Free memory buffer:
52  resize(0);
53  }
54 }
55 
57 {
58  if (m_read_only)
60  "[CMemoryStream::resize] Cannot change memory block size since it "
61  "was set with 'assign'")
62 
63  if (!newSize)
64  { // Free buffer:
65  if (m_memory.get()) free(m_memory.get());
66  m_memory = nullptr;
67  m_size = 0;
68  m_position = 0;
69  }
70  else
71  { // Resize:
72  m_memory.set(realloc(m_memory.get(), newSize));
73 
74  // Check for non-memory errors??
75  if (newSize) ASSERT_(m_memory.get());
76 
77  m_size = newSize;
78  }
79 
81 }
82 
83 size_t CMemoryStream::Read(void* Buffer, size_t Count)
84 {
85  // Enought bytes?
86  long maxAvail = (((long)m_size)) - ((long)m_position);
87  size_t nToRead = (size_t)min(((long)Count), maxAvail);
88 
89  // Copy the memory block:
90  if (nToRead > 0)
91  memcpy(Buffer, ((char*)m_memory.get()) + m_position, nToRead);
92 
93  // Update cursor position:
94  m_position += nToRead;
95  return nToRead;
96 }
97 
98 size_t CMemoryStream::Write(const void* Buffer, size_t Count)
99 {
100  // Enought space in current bufer?
101  size_t requiredSize = m_position + Count;
102 
103  if (requiredSize >= m_size)
104  {
105  // Incrent the size of reserved memory:
106  resize(requiredSize + m_alloc_block_size);
107  }
108 
109  // Copy the memory block:
110  memcpy(((char*)m_memory.get()) + m_position, Buffer, Count);
111 
112  // New cursor position:
113  m_position = requiredSize;
114 
116 
117  return Count;
118 }
119 
121 {
122  switch (Origin)
123  {
124  case sFromBeginning:
125  m_position = Offset;
126  break;
127  case sFromCurrent:
128  m_position += Offset;
129  break;
130  case sFromEnd:
131  m_position = m_bytesWritten - 1 + Origin;
132  break;
133  };
134 
135  if (m_position >= m_size) m_position = m_size - 1;
136 
137  return m_position;
138 }
139 
143 {
144  if (!m_read_only)
145  {
146  resize(0);
147  }
148  else
149  {
150  m_memory = 0;
151  m_size = 0;
152  m_position = 0;
153  m_bytesWritten = 0;
154  m_read_only = false;
155  }
156 }
157 
159 const void* CMemoryStream::getRawBufferData() const { return m_memory.get(); }
160 void CMemoryStream::changeSize(uint64_t newSize) { resize(newSize); }
162 {
163  try
164  {
165  CFileOutputStream fo(file_name);
167  return true;
168  }
169  catch (...)
170  {
171  return false;
172  }
173 }
174 
175 /*---------------------------------------------------------------
176  loadBufferFromFile
177  ---------------------------------------------------------------*/
179 {
180  try
181  {
182  CFileInputStream fi(file_name);
183  uint64_t N = fi.getTotalBytesCount();
184 
185  // Read into the buffer:
186  Clear();
187  resize(N + 100);
188  uint64_t N_read = fi.Read(m_memory.get(), N);
189 
190  m_position = N_read;
192 
193  return N_read == N;
194  }
195  catch (...)
196  {
197  return false;
198  }
199 }
200 
201 // Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct, to
202 // be freed here.
203 void mrpt::io::internal::free_fn_for_zmq(void* /* data*/, void* hint)
204 {
205  auto* fd = reinterpret_cast<mrpt::io::internal::TFreeFnDataForZMQ*>(hint);
206  if (fd->do_free) delete fd->buf;
207  delete fd;
208 }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
#define MRPT_START
Definition: exceptions.h:262
#define min(a, b)
void set(const T *p)
This method can change the pointer, since the change is made explicitly, not through copy operators t...
bool loadBufferFromFile(const std::string &file_name)
Loads the entire buffer from a file *.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
void free_fn_for_zmq(void *data, void *hint)
Used in mrpt_send_to_zmq().
uint64_t getTotalBytesCount() const override
Returns the total size of the internal buffer.
bool saveBufferToFile(const std::string &file_name)
Saves the entire buffer to a file.
This CStream derived class allow using a file as a read-only, binary stream.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
void Clear()
Clears the memory buffer.
__int64 int64_t
Definition: rptypes.h:49
void * getRawBufferData()
Method for getting a pointer to the raw stored data.
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...
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
This CStream derived class allow using a file as a write-only, binary stream.
GLsizei const GLchar ** string
Definition: glext.h:4101
unsigned __int64 uint64_t
Definition: rptypes.h:50
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
#define MRPT_END
Definition: exceptions.h:266
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
void changeSize(uint64_t newSize)
Change size.
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.
CMemoryStream()
Default constructor.
void_ptr_noncopy m_memory
Internal data.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
void resize(uint64_t newSize)
Resizes the internal buffer size.
bool m_read_only
If the memory block does not belong to the object.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:356
virtual ~CMemoryStream()
Destructor.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020