EMODnet Quantized Mesh Generator for Cesium
gzip_file_writer.h
1 // Copyright (c) 2018 Coronis Computing S.L. (Spain)
2 // All rights reserved.
3 //
4 // This file is part of EMODnet Quantized Mesh Generator for Cesium.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 //
19 // Author: Ricard Campos (ricardcd@gmail.com)
20 
21 #ifndef EMODNET_QMGC_GZIP_FILE_WRITER_H
22 #define EMODNET_QMGC_GZIP_FILE_WRITER_H
23 
24 #include <zlib.h>
25 #include <string>
26 #include <cstring>
27 
33 public:
34  // Typedefs
35  typedef unsigned char Byte ;
36 
38  GZipFileWriter( const std::string &filePath ) ;
39 
41  bool isFileOpen() { return m_file != NULL ; }
42 
44  int writeByte( const Byte &b ) ;
45 
47  int writeDouble( const double &d ) ;
48 
50  int writeFloat( const float &f ) ;
51 
53  int writeInt( const int &i ) ;
54 
56  int writeUInt( const unsigned int &u ) ;
57 
59  int writeShort( const short &s ) ;
60 
62  int writeUShort( const unsigned short &u ) ;
63 
65  int writeChar( const char &c ) ;
66 
68  int writeUChar( const unsigned char &c ) ;
69 
75  template <typename T>
76  int write( T val ) {
77  // Size of the type used
78  int sz = sizeof(T) ;
79 
80  // Write to the GZip file
81 // Byte buffer[sz];
82 // memcpy( &buffer[0], &val, sz ) ;
83  int bytesWritten = gzwrite( m_file, reinterpret_cast<char *>(&val), sz ) ;
84 
85  // Update the position of the pointer in the file
86  m_pos += sz ;
87 
88  return bytesWritten ;
89  }
90 
92  int getPos() { return m_pos ; }
93 
95  bool close() { return gzclose(m_file) ; }
96 
97 private:
98  gzFile m_file ;
99  int m_pos ;
100 };
101 
102 
103 #endif //EMODNET_QMGC_GZIP_FILE_WRITER_H
int writeUShort(const unsigned short &u)
Writes an unsigned short.
Definition: gzip_file_writer.cpp:74
int writeUChar(const unsigned char &c)
Writes an unsigned char.
Definition: gzip_file_writer.cpp:88
int writeInt(const int &i)
Writes an int.
Definition: gzip_file_writer.cpp:53
bool isFileOpen()
Check if the file is open.
Definition: gzip_file_writer.h:41
GZipFileWriter(const std::string &filePath)
Constructor.
Definition: gzip_file_writer.cpp:24
int write(T val)
Generic templated write function.
Definition: gzip_file_writer.h:76
Helper class to write a GZip file.
Definition: gzip_file_writer.h:32
int writeByte(const Byte &b)
Writes a byte.
Definition: gzip_file_writer.cpp:32
int writeUInt(const unsigned int &u)
Writes an unsigned int.
Definition: gzip_file_writer.cpp:60
int writeChar(const char &c)
Writes a char.
Definition: gzip_file_writer.cpp:81
bool close()
Closes the file.
Definition: gzip_file_writer.h:95
int writeShort(const short &s)
Writes a short.
Definition: gzip_file_writer.cpp:67
int writeDouble(const double &d)
Writes a double.
Definition: gzip_file_writer.cpp:39
int getPos()
Returns the position on the file (i.e., read byte counter)
Definition: gzip_file_writer.h:92
int writeFloat(const float &f)
Writes a float.
Definition: gzip_file_writer.cpp:46