File: gz.h

package info (click to toggle)
mrtrix3 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,712 kB
  • sloc: cpp: 129,776; python: 9,494; sh: 593; makefile: 234; xml: 47
file content (178 lines) | stat: -rw-r--r-- 5,012 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Covered Software is provided under this License on an "as is"
 * basis, without warranty of any kind, either expressed, implied, or
 * statutory, including, without limitation, warranties that the
 * Covered Software is free of defects, merchantable, fit for a
 * particular purpose or non-infringing.
 * See the Mozilla Public License v. 2.0 for more details.
 *
 * For more details, see http://www.mrtrix.org/.
 */

#ifndef __file_gz_h__
#define __file_gz_h__

#include <cassert>
#include <cstring>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <zlib.h>

#include "debug.h"
#include "mrtrix.h"
#include "types.h"
#include "exception.h"
#include "file/path.h"

namespace MR
{
  namespace File
  {

    class GZ { NOMEMALIGN
      public:
        GZ () : gz (NULL) { }
        GZ (const std::string& fname, const char* mode) : gz (NULL) {
          open (fname, mode);
        }
        ~GZ () {
          try {
            close();
          } catch (...) {
            FAIL ("error closing GZ file \"" + filename + "\": " + error());
            App::exit_error_code = 1;
          }
        }

        const std::string&  name () const  {
          return filename;
        }

        void open (const std::string& fname, const char* mode) {
          close();
          filename = fname;
          if (!MR::Path::exists (filename))
            throw Exception ("cannot access file \"" + filename + "\": No such file or directory");

          gz = gzopen (filename.c_str(), mode);
          if (!gz)
            throw Exception ("error opening file \"" + filename + "\": " + strerror(errno));
        }

        void close () {
          if (gz) {
            if (gzclose (gz))
              throw Exception ("error closing GZ file \"" + filename + "\": " + error());
            filename.clear();
            gz = NULL;
          }
        }

        bool is_open () const {
          return gz;
        }
        bool eof () const {
          assert (gz);
          return gzeof (gz);
        }
        int64_t tell () const {
          assert (gz);
          return gztell (gz);
        }
        int64_t tellg () const {
          return tell();
        }

        void seek (int64_t offset) {
          assert (gz);
          z_off_t pos = gzseek (gz, offset, SEEK_SET);
          if (pos < 0)
            throw Exception ("error seeking in GZ file \"" + filename + "\": " + error());
        }

        int read (char* s, size_t n) {
          assert (gz);
          int n_read = gzread (gz, s, n);
          if (n_read < 0)
            throw Exception ("error uncompressing GZ file \"" + filename + "\": " + error());
          return n_read;
        }

        void write (const char* s, size_t n) {
          assert (gz);
          if (gzwrite (gz, s, n) <= 0)
            throw Exception ("error writing to GZ file \"" + filename + "\": " + error());
        }

        void write (const std::string& s) {
          assert (gz);
          if (gzputs (gz, s.c_str()) < 0)
            throw Exception ("error writing to GZ file \"" + filename + "\": " + error());
        }

        std::string getline () {
          assert (gz);
          std::string string;
          int c = 0;
          do {
            c = gzgetc (gz);
            if (c < 0) {
              if (eof()) break;
              throw Exception ("error uncompressing GZ file \"" + filename + "\": " + error());
            }
            string += char (c);
          }
          while (c != '\n');
          if (string.size() && (string[string.size()-1] == 015 || string[string.size()-1] == '\n'))
            string.resize (string.size()-1);
          return string;
        }

        template <typename T> T get () {
          T val;
          if (read (&val, sizeof (T)) != sizeof (T))
            throw Exception ("error uncompressing GZ file \"" + filename + "\": " + error());
          return val;
        }

        template <typename T> T get (int64_t offset) {
          seek (offset);
          return get<T>();
        }

        template <typename T> T* get (T* buf, size_t n) {
          if (read (buf, n*sizeof (T)) != n*sizeof (T))
            throw Exception ("error uncompressing GZ file \"" + filename + "\": " + error());
          return buf;
        }

        template <typename T> T* get (int64_t offset, T* buf, size_t n) {
          seek (offset);
          return get<T> (buf, n);
        }

      protected:
        gzFile       gz;
        std::string  filename;

        const char* error () {
          int error_number;
          const char* s = gzerror (gz, &error_number);
          if (error_number == Z_ERRNO) s = strerror (errno);
          return s;
        }
    };

  }
}

#endif