Developer documentation
Version 3.0.3-105-gd3941f44
gz.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __file_gz_h__
18#define __file_gz_h__
19
20#include <cassert>
21#include <cstring>
22#include <cstdio>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <zlib.h>
27
28#include "debug.h"
29#include "mrtrix.h"
30#include "types.h"
31#include "exception.h"
32#include "file/path.h"
33
34namespace MR
35{
36 namespace File
37 {
38
39 class GZ { NOMEMALIGN
40 public:
41 GZ () : gz (NULL) { }
42 GZ (const std::string& fname, const char* mode) : gz (NULL) {
43 open (fname, mode);
44 }
45 ~GZ () {
46 try {
47 close();
48 } catch (...) {
49 FAIL ("error closing GZ file \"" + filename + "\": " + error());
51 }
52 }
53
54 const std::string& name () const {
55 return filename;
56 }
57
58 void open (const std::string& fname, const char* mode) {
59 close();
60 filename = fname;
62 throw Exception ("cannot access file \"" + filename + "\": No such file or directory");
63
64 gz = gzopen (filename.c_str(), mode);
65 if (!gz)
66 throw Exception ("error opening file \"" + filename + "\": " + strerror(errno));
67 }
68
69 void close () {
70 if (gz) {
71 if (gzclose (gz))
72 throw Exception ("error closing GZ file \"" + filename + "\": " + error());
73 filename.clear();
74 gz = NULL;
75 }
76 }
77
78 bool is_open () const {
79 return gz;
80 }
81 bool eof () const {
82 assert (gz);
83 return gzeof (gz);
84 }
85 int64_t tell () const {
86 assert (gz);
87 return gztell (gz);
88 }
89 int64_t tellg () const {
90 return tell();
91 }
92
93 void seek (int64_t offset) {
94 assert (gz);
95 z_off_t pos = gzseek (gz, offset, SEEK_SET);
96 if (pos < 0)
97 throw Exception ("error seeking in GZ file \"" + filename + "\": " + error());
98 }
99
100 int read (char* s, size_t n) {
101 assert (gz);
102 int n_read = gzread (gz, s, n);
103 if (n_read < 0)
104 throw Exception ("error uncompressing GZ file \"" + filename + "\": " + error());
105 return n_read;
106 }
107
108 void write (const char* s, size_t n) {
109 assert (gz);
110 if (gzwrite (gz, s, n) <= 0)
111 throw Exception ("error writing to GZ file \"" + filename + "\": " + error());
112 }
113
114 void write (const std::string& s) {
115 assert (gz);
116 if (gzputs (gz, s.c_str()) < 0)
117 throw Exception ("error writing to GZ file \"" + filename + "\": " + error());
118 }
119
120 std::string getline () {
121 assert (gz);
122 std::string string;
123 int c = 0;
124 do {
125 c = gzgetc (gz);
126 if (c < 0) {
127 if (eof()) break;
128 throw Exception ("error uncompressing GZ file \"" + filename + "\": " + error());
129 }
130 string += char (c);
131 }
132 while (c != '\n');
133 if (string.size() && (string[string.size()-1] == 015 || string[string.size()-1] == '\n'))
134 string.resize (string.size()-1);
135 return string;
136 }
137
138 template <typename T> T get () {
139 T val;
140 if (read (&val, sizeof (T)) != sizeof (T))
141 throw Exception ("error uncompressing GZ file \"" + filename + "\": " + error());
142 return val;
143 }
144
145 template <typename T> T get (int64_t offset) {
146 seek (offset);
147 return get<T>();
148 }
149
150 template <typename T> T* get (T* buf, size_t n) {
151 if (read (buf, n*sizeof (T)) != n*sizeof (T))
152 throw Exception ("error uncompressing GZ file \"" + filename + "\": " + error());
153 return buf;
154 }
155
156 template <typename T> T* get (int64_t offset, T* buf, size_t n) {
157 seek (offset);
158 return get<T> (buf, n);
159 }
160
161 protected:
162 gzFile gz;
163 std::string filename;
164
165 const char* error () {
166 int error_number;
167 const char* s = gzerror (gz, &error_number);
168 if (error_number == Z_ERRNO) s = strerror (errno);
169 return s;
170 }
171 };
172
173 }
174}
175
176#endif
177
178
const char * error()
Definition: gz.h:165
int64_t tell() const
Definition: gz.h:85
int read(char *s, size_t n)
Definition: gz.h:100
std::string filename
Definition: gz.h:163
int64_t tellg() const
Definition: gz.h:89
T get(int64_t offset)
Definition: gz.h:145
std::string getline()
Definition: gz.h:120
GZ()
Definition: gz.h:41
GZ(const std::string &fname, const char *mode)
Definition: gz.h:42
void seek(int64_t offset)
Definition: gz.h:93
void open(const std::string &fname, const char *mode)
Definition: gz.h:58
void write(const char *s, size_t n)
Definition: gz.h:108
T get()
Definition: gz.h:138
bool is_open() const
Definition: gz.h:78
bool eof() const
Definition: gz.h:81
void write(const std::string &s)
Definition: gz.h:114
T * get(T *buf, size_t n)
Definition: gz.h:150
T * get(int64_t offset, T *buf, size_t n)
Definition: gz.h:156
gzFile gz
Definition: gz.h:162
void close()
Definition: gz.h:69
const std::string & name() const
Definition: gz.h:54
~GZ()
Definition: gz.h:45
#define FAIL(msg)
Definition: exception.h:72
index_type offset
Definition: loop.h:33
#define NOMEMALIGN
Definition: memory.h:22
int exit_error_code
Definition: exception.h:35
bool exists(const std::string &path)
Definition: path.h:88
Definition: base.h:24