MRPT  1.9.9
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-2019, 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 = 0;
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 
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_size)) - ((long)m_position);
90  size_t nToRead = (size_t)min(((long)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 
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 
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 void CMemoryStream::changeSize(uint64_t newSize) { resize(newSize); }
168 {
169  try
170  {
171  CFileOutputStream fo(file_name);
173  return true;
174  }
175  catch (...)
176  {
177  return false;
178  }
179 }
180 
181 /*---------------------------------------------------------------
182  loadBufferFromFile
183  ---------------------------------------------------------------*/
185 {
186  try
187  {
188  CFileInputStream fi(file_name);
189  uint64_t N = fi.getTotalBytesCount();
190 
191  // Read into the buffer:
192  Clear();
193  resize(N + 100);
194  uint64_t N_read = fi.Read(m_memory.get(), N);
195 
196  m_position = N_read;
198 
199  return N_read == N;
200  }
201  catch (...)
202  {
203  return false;
204  }
205 }
206 
207 // Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct, to
208 // be freed here.
209 void mrpt::io::internal::free_fn_for_zmq(void* /* data*/, void* hint)
210 {
211  auto* fd = reinterpret_cast<mrpt::io::internal::TFreeFnDataForZMQ*>(hint);
212  if (fd->do_free) delete fd->buf;
213  delete fd;
214 }
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 Clear()
Clears the memory buffer.
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.
GLsizei const GLchar ** string
Definition: glext.h:4116
unsigned __int64 uint64_t
Definition: glext.h:3457
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.
void changeSize(uint64_t newSize)
Change size.
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.
void_ptr_noncopy m_memory
Internal data.
__int64 int64_t
Definition: glext.h:3456
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:3550
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:358



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c1796881b Sat Nov 16 19:04:34 2019 +0100 at sáb nov 16 19:15:10 CET 2019