Main MRPT website > C++ reference for MRPT 1.9.9
CFileGZOutputStream.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-2017, 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 "base-precomp.h" // Precompiled headers
11 
12 #ifdef _MSC_VER
13 #define _SCL_SECURE_NO_WARNINGS
14 #endif
15 
17 #include <mrpt/system/os.h>
18 
19 #if MRPT_HAS_GZ_STREAMS
20 
21 #include <zlib.h>
22 
23 #define THE_GZFILE reinterpret_cast<gzFile>(m_f)
24 
25 using namespace mrpt::utils;
26 using namespace std;
27 
28 /*---------------------------------------------------------------
29  Constructor
30  ---------------------------------------------------------------*/
31 CFileGZOutputStream::CFileGZOutputStream(const string& fileName) : m_f(nullptr)
32 {
34  if (!open(fileName))
36  "Error trying to open file: '%s'", fileName.c_str());
37  MRPT_END
38 }
39 
40 /*---------------------------------------------------------------
41  Constructor
42  ---------------------------------------------------------------*/
44 /*---------------------------------------------------------------
45  open
46  ---------------------------------------------------------------*/
47 bool CFileGZOutputStream::open(const string& fileName, int compress_level)
48 {
50 
51  if (m_f) gzclose(THE_GZFILE);
52 
53  // Open gz stream:
54  m_f = gzopen(fileName.c_str(), format("wb%i", compress_level).c_str());
55  return m_f != nullptr;
56 
57  MRPT_END
58 }
59 
60 /*---------------------------------------------------------------
61  Destructor
62  ---------------------------------------------------------------*/
63 CFileGZOutputStream::~CFileGZOutputStream() { close(); }
64 /*---------------------------------------------------------------
65  close
66  ---------------------------------------------------------------*/
67 void CFileGZOutputStream::close()
68 {
69  if (m_f)
70  {
71  gzclose(THE_GZFILE);
72  m_f = nullptr;
73  }
74 }
75 
76 /*---------------------------------------------------------------
77  Read
78  Reads bytes from the stream into Buffer
79  ---------------------------------------------------------------*/
80 size_t CFileGZOutputStream::Read(void* Buffer, size_t Count)
81 {
82  MRPT_UNUSED_PARAM(Buffer);
83  MRPT_UNUSED_PARAM(Count);
84  THROW_EXCEPTION("Trying to read from an output file stream.");
85 }
86 
87 /*---------------------------------------------------------------
88  Write
89  Writes a block of bytes to the stream.
90  ---------------------------------------------------------------*/
91 size_t CFileGZOutputStream::Write(const void* Buffer, size_t Count)
92 {
93  if (!m_f)
94  {
95  THROW_EXCEPTION("File is not open.");
96  }
97  return gzwrite(THE_GZFILE, const_cast<void*>(Buffer), Count);
98 }
99 
100 /*---------------------------------------------------------------
101  getPosition
102  ---------------------------------------------------------------*/
103 uint64_t CFileGZOutputStream::getPosition()
104 {
105  if (!m_f)
106  {
107  THROW_EXCEPTION("File is not open.");
108  }
109  return gztell(THE_GZFILE);
110 }
111 
112 /*---------------------------------------------------------------
113  fileOpenCorrectly
114  ---------------------------------------------------------------*/
115 bool CFileGZOutputStream::fileOpenCorrectly() { return m_f != nullptr; }
116 #endif // MRPT_HAS_GZ_STREAMS
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
bool open(const std::string &fileName)
Opens the file for read.
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
STL namespace.
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define THE_GZFILE
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:50
#define CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019