16 #define WIN32_LEAN_AND_MEAN 36 void* inData,
size_t inDataSize, std::vector<unsigned char>& outData)
41 unsigned long resSize;
43 outData.resize(inDataSize + inDataSize / 1000 + 50);
44 resSize = (
unsigned long)outData.size();
46 &outData[0], &resSize, static_cast<unsigned char*>(inData),
47 (
unsigned long)inDataSize);
50 outData.resize(resSize);
59 const std::vector<unsigned char>& inData,
60 std::vector<unsigned char>& outData)
65 unsigned long resSize;
67 outData.resize(inData.size() + inData.size() / 1000 + 50);
68 resSize = (
unsigned long)outData.size();
70 &outData[0], &resSize, &inData[0], (
unsigned long)inData.size());
73 outData.resize(resSize);
86 unsigned long resSize;
87 std::vector<unsigned char> outData;
89 outData.resize(inDataSize + inDataSize / 1000 + 50);
90 resSize = (
unsigned long)outData.size();
93 &outData[0], &resSize, (
unsigned char*)inData,
94 (
unsigned long)inDataSize);
97 outData.resize(resSize);
100 out.
Write(&outData[0], resSize);
109 const std::vector<unsigned char>& inData,
CStream& out)
114 unsigned long resSize;
115 std::vector<unsigned char> outData;
116 outData.resize(inData.size() + inData.size() / 1000 + 50);
117 resSize = (
unsigned long)outData.size();
120 &outData[0], &resSize, &inData[0], (
unsigned long)inData.size());
124 outData.resize(resSize);
127 out.
Write(&outData[0], resSize);
136 void* inData,
size_t inDataSize, std::vector<unsigned char>& outData,
137 size_t outDataEstimatedSize)
142 outData.resize(outDataEstimatedSize);
143 unsigned long actualOutSize;
146 &outData[0], &actualOutSize, (
unsigned char*)inData,
147 (
unsigned long)inDataSize);
151 outData.resize(actualOutSize);
160 void* inData,
size_t inDataSize,
void* outData,
size_t outDataBufferSize,
161 size_t& outDataActualSize)
166 auto actualOutSize = (
unsigned long)outDataBufferSize;
169 (
unsigned char*)outData, &actualOutSize, (
unsigned char*)inData,
170 (
unsigned long)inDataSize);
174 outDataActualSize = actualOutSize;
183 CStream& inStream,
size_t inDataSize,
void* outData,
184 size_t outDataBufferSize,
size_t& outDataActualSize)
189 auto actualOutSize = (
unsigned long)outDataBufferSize;
190 std::vector<unsigned char> inData;
192 inData.resize(inDataSize);
193 inStream.
Read(&inData[0], inDataSize);
196 (
unsigned char*)outData, &actualOutSize, &inData[0],
197 (
unsigned long)inDataSize);
201 outDataActualSize = actualOutSize;
217 const int bytes2read = 1 << 20;
219 size_t total_bytes = 0;
226 act_read = iss.
Read(&
buffer[total_bytes], bytes2read);
227 total_bytes += act_read;
228 }
while (act_read == bytes2read);
230 buffer.resize(total_bytes);
236 const int compress_level)
239 oss.
open(file_path, compress_level);
261 const std::vector<uint8_t>& in_data, std::vector<uint8_t>& out_gz_data,
262 const int compress_level)
265 if (in_data.empty())
return true;
267 const unsigned int nPipeName =
275 format(
"\\\\.\\pipe\\mrpt_compress_gz_data_block_%u", nPipeName);
277 HANDLE hPipe = CreateNamedPipeA(
278 pipe_file_name.c_str(),
279 PIPE_ACCESS_DUPLEX | 0x00080000 ,
280 PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, in_data.size() + 1000,
281 in_data.size() + 1000, 0,
nullptr);
282 if (hPipe == INVALID_HANDLE_VALUE)
286 pipe_file_name =
format(
"/tmp/mrpt_compress_gz_data_block_%u", nPipeName);
295 pipe_file_name.c_str(),
format(
"wb%i", compress_level).c_str());
298 retVal = (int)in_data.size() ==
299 gzwrite(f, &in_data[0], in_data.size());
304 std::cerr <<
"[compress_gz_data_block] Error writing to pipe: " 305 << pipe_file_name << std::endl;
313 const size_t N = GetFileSize(hPipe,
nullptr);
316 out_gz_data.resize(N);
318 SetFilePointer(hPipe, 0,
nullptr, FILE_BEGIN);
321 ReadFile(hPipe, &out_gz_data[0], N, &nRead,
nullptr);
333 if (iss.
open(pipe_file_name))
336 out_gz_data.resize(M);
337 if (M) retVal = M == iss.
Read(&out_gz_data[0], M);
352 remove(pipe_file_name.c_str());
359 const std::vector<uint8_t>& in_gz_data, std::vector<uint8_t>& out_data)
362 if (in_gz_data.empty())
return true;
369 remove(tmp_file_name.c_str());
bool vectorToBinaryFile(const std::vector< uint8_t > &vec, const std::string &fileName)
Saves a vector directly as a binary dump to a file:
void decompress(void *inData, size_t inDataSize, std::vector< unsigned char > &outData, size_t outDataEstimatedSize)
Decompress an array of bytes into another one.
#define THROW_EXCEPTION(msg)
bool open(const std::string &fileName, int compress_level=1, mrpt::optional_ref< std::string > error_msg=std::nullopt)
Open a file for write, choosing the compression level.
virtual size_t Read(void *Buffer, size_t Count)=0
Introduces a pure virtual method responsible for reading from the stream.
bool decompress_gz_file(const std::string &file_path, std::vector< uint8_t > &buffer)
Decompress a gzip file (xxxx.gz) into a memory buffer.
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
#define MRPT_END_WITH_CLEAN_UP(stuff)
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Compression using the "zip" algorithm and from/to gzip (gz) files.
#define ASSERT_(f)
Defines an assertion mechanism.
std::string getTempFileName()
Returns the name of a proposed temporary file name.
GLsizei const GLchar ** string
bool compress_gz_data_block(const std::vector< uint8_t > &in_data, std::vector< uint8_t > &out_gz_data, const int compress_level=9)
Compress a memory buffer in gz-file format and return it as a block a memory.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
virtual size_t Write(const void *Buffer, size_t Count)=0
Introduces a pure virtual method responsible for writing to the stream.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
bool decompress_gz_data_block(const std::vector< uint8_t > &in_gz_data, std::vector< uint8_t > &out_data)
Decompress an array of bytes storing a gz-compressed stream of data into a memory buffer...
bool compress_gz_file(const std::string &file_path, const std::vector< uint8_t > &buffer, const int compress_level=9)
Compress a memory buffer into a gzip file (xxxx.gz).
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Saves data to a file and transparently compress the data using the given compression level...
void compress(void *inData, size_t inDataSize, std::vector< unsigned char > &outData)
Compress an array of bytes into another one.