gzip_reader.cc
Go to the documentation of this file.
1// LIC// ====================================================================
2// LIC// This file forms part of oomph-lib, the object-oriented,
3// LIC// multi-physics finite-element library, available
4// LIC// at http://www.oomph-lib.org.
5// LIC//
6// LIC// Copyright (C) 2006-2022 Matthias Heil and Andrew Hazel
7// LIC//
8// LIC// This library is free software; you can redistribute it and/or
9// LIC// modify it under the terms of the GNU Lesser General Public
10// LIC// License as published by the Free Software Foundation; either
11// LIC// version 2.1 of the License, or (at your option) any later version.
12// LIC//
13// LIC// This library is distributed in the hope that it will be useful,
14// LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// LIC// Lesser General Public License for more details.
17// LIC//
18// LIC// You should have received a copy of the GNU Lesser General Public
19// LIC// License along with this library; if not, write to the Free Software
20// LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21// LIC// 02110-1301 USA.
22// LIC//
23// LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24// LIC//
25// LIC//====================================================================
26
27#include "gzip_reader.h"
28#include "oomph_definitions.h"
29#include "oomph_zlib/zlib.h"
30
31#include <algorithm>
32#include <fstream>
33#include <vector>
34
35namespace oomph
36{
37 /************************************************************************************
38 * @brief Construct a new GZipReader object
39 *
40 ************************************************************************************/
41 GZipReader::GZipReader() : Gzip_file(nullptr), Chunk_size(256) {}
42
43 /************************************************************************************
44 * @brief Construct a new GZipReader object
45 *
46 * @param filename:
47 ************************************************************************************/
48 GZipReader::GZipReader(const std::string& filename,
49 const unsigned& chunk_size)
50 : Gzip_file(nullptr), Chunk_size(chunk_size)
51 {
52 this->open(filename);
53 if (!this->is_open())
54 {
55 throw OomphLibError("Unable to open file '" + filename + "'!",
58 }
59 }
60
61 /************************************************************************************
62 * @brief Destroy the GZipReader object
63 *
64 ************************************************************************************/
66 {
67 if (this->is_open())
68 {
69 this->close();
70 }
71 }
72
73 /************************************************************************************
74 * @brief
75 *
76 * @param filename:
77 ************************************************************************************/
78 void GZipReader::open(const std::string& filename)
79 {
80 Gzip_file = gzopen(filename.c_str(), "rb");
81 }
82
83 /************************************************************************************
84 * @brief
85 *
86 ************************************************************************************/
88 {
89 if (!this->is_open())
90 {
91 return;
92 }
94 Gzip_file = nullptr;
95 }
96
97 /************************************************************************************
98 * @brief
99 *
100 * @return true:
101 * @return false:
102 ************************************************************************************/
104 {
105 if (Gzip_file != nullptr)
106 {
107 return true;
108 }
109 return false;
110 }
111
112 /************************************************************************************
113 * @brief
114 *
115 * TODO: Update to using std::optional<std::string> if C++17 is allowed
116 *
117 * @return std::string:
118 ************************************************************************************/
120 {
121 // Storage for the loaded line and flag to indicate whether we were able to
122 // read something
123 std::string line{};
124 bool found{false};
125
126 // Early exit if the file is closed
127 if (!is_open())
128 {
129 return Packet{line, found};
130 }
131
132 // Allocate space for a buffer on the stack
133 char buffer_pt[Chunk_size];
134
135 // Pointer to the end of the buffer
137
138 // Flag to indicate whether we've reached the end of the line
139 bool reached_end_of_line{false};
140
141 // Repeatedly read chunks until we reach the end of the line
142 while (!reached_end_of_line)
143 {
144 // EOF condition or error
146 {
147 this->close();
148 break;
149 }
150
151 // No EOF or error
152 found = true;
153
154 // Find the extent of the loaded string
155 char* line_end_pt = std::find(buffer_pt, buffer_end_pt, '\0');
156
157 // Append the loaded string
159
160 // Check if the last loaded non-null character is a newline character
161 if ((line_end_pt != buffer_pt) && (*(line_end_pt - 1) == '\n'))
162 {
163 reached_end_of_line = true;
164 }
165 }
166
167 // Strip the newline character, if there is one
168 if (found && (!line.empty()) && (line[line.length() - 1] == '\n'))
169 {
170 line.erase(line.length() - 1);
171 }
172 return Packet{line, found};
173 }
174
175 /************************************************************************************
176 * @brief
177 *
178 * @return std::vector<std::string>:
179 ************************************************************************************/
180 std::vector<std::string> GZipReader::read_all()
181 {
182 // Early exit if the file is closed
183 if (!this->is_open())
184 {
185 return {};
186 }
187
188 // Storage for the loaded data
189 std::vector<std::string> file_contents;
190 while (this->is_open())
191 {
192 Packet packet = this->getline();
193 if (packet.found)
194 {
195 file_contents.emplace_back(packet.data);
196 }
197 }
198 return file_contents;
199 }
200
201} // namespace oomph
std::vector< std::string > read_all()
void open(const std::string &filename)
bool is_open() const
unsigned Chunk_size
Definition gzip_reader.h:68
An OomphLibError object which should be thrown when an run-time error is encountered....
TAdvectionDiffusionReactionElement<NREAGENT,DIM,NNODE_1D> elements are isoparametric triangular DIM-d...
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).