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



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018