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 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  ASSERT_(Buffer != nullptr);
101  // Enought space in current bufer?
102  size_t requiredSize = m_position + Count;
103 
104  if (requiredSize >= m_size)
105  {
106  // Incrent the size of reserved memory:
107  resize(requiredSize + m_alloc_block_size);
108  }
109 
110  // Copy the memory block:
111  memcpy(((char*)m_memory.get()) + m_position, Buffer, Count);
112 
113  // New cursor position:
114  m_position = requiredSize;
115 
117 
118  return Count;
119 }
120 
122 {
123  switch (Origin)
124  {
125  case sFromBeginning:
126  m_position = Offset;
127  break;
128  case sFromCurrent:
129  m_position += Offset;
130  break;
131  case sFromEnd:
132  m_position = m_bytesWritten - 1 + Origin;
133  break;
134  };
135 
136  if (m_position >= m_size) m_position = m_size - 1;
137 
138  return m_position;
139 }
140 
144 {
145  if (!m_read_only)
146  {
147  resize(0);
148  }
149  else
150  {
151  m_memory = nullptr;
152  m_size = 0;
153  m_position = 0;
154  m_bytesWritten = 0;
155  m_read_only = false;
156  }
157 }
158 
160 const void* CMemoryStream::getRawBufferData() const { return m_memory.get(); }
161 void CMemoryStream::changeSize(uint64_t newSize) { resize(newSize); }
163 {
164  try
165  {
166  CFileOutputStream fo(file_name);
168  return true;
169  }
170  catch (...)
171  {
172  return false;
173  }
174 }
175 
176 /*---------------------------------------------------------------
177  loadBufferFromFile
178  ---------------------------------------------------------------*/
180 {
181  try
182  {
183  CFileInputStream fi(file_name);
184  uint64_t N = fi.getTotalBytesCount();
185 
186  // Read into the buffer:
187  Clear();
188  resize(N + 100);
189  uint64_t N_read = fi.Read(m_memory.get(), N);
190 
191  m_position = N_read;
193 
194  return N_read == N;
195  }
196  catch (...)
197  {
198  return false;
199  }
200 }
201 
202 // Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct, to
203 // be freed here.
204 void mrpt::io::internal::free_fn_for_zmq(void* /* data*/, void* hint)
205 {
206  auto* fd = reinterpret_cast<mrpt::io::internal::TFreeFnDataForZMQ*>(hint);
207  if (fd->do_free) delete fd->buf;
208  delete fd;
209 }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
#define MRPT_START
Definition: exceptions.h:241
#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: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.
__int64 int64_t
Definition: rptypes.h:52
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: rptypes.h:53
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.
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: ee555d257 Fri Aug 16 10:05:39 2019 +0200 at vie ago 16 10:10:14 CEST 2019