MRPT  2.0.2
CMemoryStream.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "io-precomp.h" // Precompiled headers
11 
14 #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 = nBytesInData;
44  m_read_only = true;
45 }
46 
48 {
49  if (!m_read_only)
50  {
51  // Free buffer:
52  if (m_memory.get()) free(m_memory.get());
53  m_memory = nullptr;
54  m_size = 0;
55  m_position = 0;
56  }
57 }
58 
59 void CMemoryStream::resize(uint64_t newSize)
60 {
61  if (m_read_only)
63  "[CMemoryStream::resize] Cannot change memory block size since it "
64  "was set with 'assign'");
65 
66  if (!newSize)
67  { // Free buffer:
68  if (m_memory.get()) free(m_memory.get());
69  m_memory = nullptr;
70  m_size = 0;
71  m_position = 0;
72  }
73  else
74  { // Resize:
75  m_memory.set(realloc(m_memory.get(), newSize));
76 
77  // Check for non-memory errors??
78  if (newSize) ASSERT_(m_memory.get());
79 
80  m_size = newSize;
81  }
82 
84 }
85 
86 size_t CMemoryStream::Read(void* Buffer, size_t Count)
87 {
88  // enough bytes?
89  long maxAvail = (((long)m_bytesWritten)) - ((long)m_position);
90  size_t nToRead = std::min<size_t>(Count, maxAvail);
91 
92  // Copy the memory block:
93  if (nToRead > 0)
94  memcpy(
95  Buffer, reinterpret_cast<char*>(m_memory.get()) + m_position,
96  nToRead);
97 
98  // Update cursor position:
99  m_position += nToRead;
100  return nToRead;
101 }
102 
103 size_t CMemoryStream::Write(const void* Buffer, size_t Count)
104 {
105  ASSERT_(Buffer != nullptr);
106  // enough space in current bufer?
107  size_t requiredSize = m_position + Count;
108 
109  if (requiredSize >= m_size)
110  {
111  // Incrent the size of reserved memory:
112  resize(requiredSize + m_alloc_block_size);
113  }
114 
115  // Copy the memory block:
116  memcpy(reinterpret_cast<char*>(m_memory.get()) + m_position, Buffer, Count);
117 
118  // New cursor position:
119  m_position = requiredSize;
120 
122 
123  return Count;
124 }
125 
126 uint64_t CMemoryStream::Seek(int64_t Offset, CStream::TSeekOrigin Origin)
127 {
128  switch (Origin)
129  {
130  case sFromBeginning:
131  m_position = Offset;
132  break;
133  case sFromCurrent:
134  m_position += Offset;
135  break;
136  case sFromEnd:
137  m_position = m_bytesWritten - 1 + Origin;
138  break;
139  };
140 
141  if (m_position >= m_size) m_position = m_size - 1;
142 
143  return m_position;
144 }
145 
147 uint64_t CMemoryStream::getPosition() const { return m_position; }
149 {
150  if (!m_read_only)
151  {
152  resize(0);
153  }
154  else
155  {
156  m_memory = nullptr;
157  m_size = 0;
158  m_position = 0;
159  m_bytesWritten = 0;
160  m_read_only = false;
161  }
162 }
163 
165 const void* CMemoryStream::getRawBufferData() const { return m_memory.get(); }
166 bool CMemoryStream::saveBufferToFile(const std::string& file_name)
167 {
168  try
169  {
170  CFileOutputStream fo(file_name);
172  return true;
173  }
174  catch (...)
175  {
176  return false;
177  }
178 }
179 
180 /*---------------------------------------------------------------
181  loadBufferFromFile
182  ---------------------------------------------------------------*/
183 bool CMemoryStream::loadBufferFromFile(const std::string& file_name)
184 {
185  try
186  {
187  CFileInputStream fi(file_name);
188  uint64_t N = fi.getTotalBytesCount();
189 
190  // Read into the buffer:
191  clear();
192  resize(N + 100);
193  uint64_t N_read = fi.Read(m_memory.get(), N);
194 
195  m_position = N_read;
197 
198  return N_read == N;
199  }
200  catch (...)
201  {
202  return false;
203  }
204 }
205 
206 // Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct, to
207 // be freed here.
208 void mrpt::io::internal::free_fn_for_zmq(void* /* data*/, void* hint)
209 {
210  auto* fd = reinterpret_cast<mrpt::io::internal::TFreeFnDataForZMQ*>(hint);
211  if (fd->do_free) delete fd->buf;
212  delete fd;
213 }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
#define MRPT_START
Definition: exceptions.h:241
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:67
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:120
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
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...
CMemoryStream()=default
Default constructor.
~CMemoryStream() override
Destructor.
This CStream derived class allow using a file as a write-only, binary stream.
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:245
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.
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.
void resize(uint64_t newSize)
Resizes the internal buffer size.
void clear()
Clears the memory buffer.
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".
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020