ARTS 2.5.10 (git: 2f1c442c)
xml_io_base.h
Go to the documentation of this file.
1/* Copyright (C) 2002-2012 Oliver Lemke <olemke@core-dump.info>
2
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 USA. */
17
19// File description
21
30#ifndef xml_io_base_h
31#define xml_io_base_h
32
33#include <memory>
34
35#include "arts.h"
37
38#ifdef ENABLE_ZLIB
39#include "gzstream.h"
40#endif
41
46};
47
50
52// XML parser classes
54
56
61 public:
64};
65
67
70class XMLTag {
71 public:
72 XMLTag(const Verbosity& rverbosity) : verbosity(rverbosity){};
73
74 String& get_name() { return name; }
75
76 void check_name(const String& expected_name);
77
78 void set_name(const String& new_name) { name = new_name; }
79
80 void add_attribute(const String& aname, String value);
81
82 void add_attribute(const String& aname, const Index& value);
83
89 void add_attribute(const String& aname, const Numeric& value);
90
91 void check_attribute(const String& aname, const String& value);
92
93 void get_attribute_value(const String& aname, String& value);
94
95 void get_attribute_value(const String& aname, Index& value);
96
106 void get_attribute_value(const String& aname, Numeric& value);
107
108 void read_from_stream(istream& is);
109
110 void write_to_stream(ostream& os);
111
117 [[nodiscard]] bool has_attribute(const String& aname) const;
118
119 protected:
123};
124
126// General XML handling routines
128
129void xml_parse_error(const String& str_error);
130
131void xml_data_parse_error(XMLTag& tag, const String& str_error);
132
133void xml_set_stream_precision(ostream& os);
134
135void parse_xml_tag_content_as_string(std::istream& is_xml, String& content);
136
138 istream &, Vector &, bifstream *, XMLTag &, const Verbosity &verbosity);
139
141 istream &, ArrayOfString &, bifstream *, XMLTag &, const Verbosity &);
142
143FileType string2filetype(const String& file_format);
144
146// Generic IO routines for XML files
148
150
159void xml_read_header_from_stream(istream& is,
160 FileType& ftype,
161 NumericType& ntype,
162 EndianType& etype,
163 const Verbosity& verbosity);
164
166
171void xml_read_footer_from_stream(istream& is, const Verbosity& verbosity);
172
174
178void xml_write_header_to_stream(ostream& os,
179 FileType ftype,
180 const Verbosity& verbosity);
181
183
186void xml_write_footer_to_stream(ostream& os, const Verbosity& verbosity);
187
189
195void xml_open_input_file(ifstream& ifs,
196 const String& name,
197 const Verbosity& verbosity);
198
200
206void xml_open_output_file(ofstream& file, const String& name);
207
208#ifdef ENABLE_ZLIB
209
211
218 const String& name,
219 const Verbosity& verbosity);
220
222
228void xml_open_output_file(ogzstream& file, const String& name);
229
230#endif // ENABLE_ZLIB
231
233// Generic IO routines for XML files
235
237
244template <typename T>
245void xml_read_from_file_base(const String& filename,
246 T& type,
247 const Verbosity& verbosity) {
249
250 out2 << " Reading " + filename + '\n';
251
252 // Open input stream:
253 std::unique_ptr<istream> ifs;
254 if (filename.nelem() > 2 &&
255 filename.substr(filename.length() - 3, 3) == ".gz")
256#ifdef ENABLE_ZLIB
257 {
258 ifs = std::make_unique<igzstream>();
260 *static_cast<igzstream*>(ifs.get()), filename, verbosity);
261 }
262#else
263 {
264 throw runtime_error(
265 "This arts version was compiled without zlib support.\n"
266 "Thus zipped xml files cannot be read.");
267 }
268#endif /* ENABLE_ZLIB */
269 else {
270 ifs = std::make_unique<ifstream>();
272 *static_cast<ifstream*>(ifs.get()), filename, verbosity);
273 }
274
275 // No need to check for error, because xml_open_input_file throws a
276 // runtime_error with an appropriate error message.
277
278 // Read the matrix from the stream. Here we catch the exception,
279 // because then we can issue a nicer error message that includes the
280 // filename.
281 try {
282 FileType ftype;
283 NumericType ntype;
284 EndianType etype;
285
286 xml_read_header_from_stream(*ifs, ftype, ntype, etype, verbosity);
287 if (ftype == FILE_TYPE_ASCII) {
288 xml_read_from_stream(*ifs, type, NULL, verbosity);
289 } else {
290 String bfilename = filename + ".bin";
291 bifstream bifs(bfilename.c_str());
292 xml_read_from_stream(*ifs, type, &bifs, verbosity);
293 }
294 xml_read_footer_from_stream(*ifs, verbosity);
295 } catch (const std::runtime_error& e) {
296 ostringstream os;
297 os << "Error reading file: " << filename << '\n' << e.what();
298 throw runtime_error(os.str());
299 }
300}
301
303
312template <typename T>
313void xml_write_to_file_base(const String& filename,
314 const T& type,
315 const FileType ftype,
316 const Verbosity& verbosity) {
318
319 std::unique_ptr<ostream> ofs;
320
321 out2 << " Writing " << filename << '\n';
322 if (ftype == FILE_TYPE_ZIPPED_ASCII)
323#ifdef ENABLE_ZLIB
324 {
325 ofs = std::make_unique<ogzstream>();
326 xml_open_output_file(*static_cast<ogzstream*>(ofs.get()), filename);
327 }
328#else
329 {
330 throw runtime_error(
331 "This arts version was compiled without zlib support.\n"
332 "Thus zipped xml files cannot be written.");
333 }
334#endif /* ENABLE_ZLIB */
335 else {
336 ofs = std::make_unique<ofstream>();
337 xml_open_output_file(*static_cast<ofstream*>(ofs.get()), filename);
338 }
339
340 try {
341 xml_write_header_to_stream(*ofs, ftype, verbosity);
342 if (ftype == FILE_TYPE_ASCII || ftype == FILE_TYPE_ZIPPED_ASCII) {
343 xml_write_to_stream(*ofs, type, NULL, "", verbosity);
344 } else {
345 String bfilename = filename + ".bin";
346 bofstream bofs(bfilename.c_str());
347 xml_write_to_stream(*ofs, type, &bofs, "", verbosity);
348 }
349
350 xml_write_footer_to_stream(*ofs, verbosity);
351 } catch (const std::runtime_error& e) {
352 ostringstream os;
353 os << "Error writing file: " << filename << '\n' << e.what();
354 throw runtime_error(os.str());
355 }
356}
357
358#endif
The global header file for ARTS.
This can be used to make arrays out of anything.
Definition: array.h:48
The Vector class.
Definition: matpackI.h:910
XML attribute class.
Definition: xml_io_base.h:60
String name
Definition: xml_io_base.h:62
String value
Definition: xml_io_base.h:63
The ARTS XML tag class.
Definition: xml_io_base.h:70
String name
Definition: xml_io_base.h:120
bool has_attribute(const String &aname) const
Returns if the attribute exists or not.
Definition: xml_io_base.cc:125
void add_attribute(const String &aname, String value)
Definition: xml_io_base.cc:67
void write_to_stream(ostream &os)
Write XML tag.
Definition: xml_io_base.cc:322
void check_name(const String &expected_name)
Check tag name.
Definition: xml_io_base.cc:54
XMLTag(const Verbosity &rverbosity)
Definition: xml_io_base.h:72
void read_from_stream(istream &is)
Reads next XML tag.
Definition: xml_io_base.cc:201
Array< XMLAttribute > attribs
Definition: xml_io_base.h:121
void get_attribute_value(const String &aname, String &value)
Definition: xml_io_base.cc:140
void set_name(const String &new_name)
Definition: xml_io_base.h:78
String & get_name()
Definition: xml_io_base.h:74
void check_attribute(const String &aname, const String &value)
Checks whether attribute has the expected value.
Definition: xml_io_base.cc:111
const Verbosity & verbosity
Definition: xml_io_base.h:122
Binary output file stream class.
Definition: bifstream.h:43
Binary output file stream class.
Definition: bofstream.h:42
Index nelem() const
Definition: mystring.h:189
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
#define CREATE_OUT2
Definition: messages.h:205
void xml_write_to_stream(ostream &os_xml, const ArrayOfAgenda &aa, bofstream *pbofs, const String &name, const Verbosity &)
Writes ArrayOfAgenda to XML output stream.
void xml_read_from_stream(istream &is_xml, ArrayOfAgenda &aa, bifstream *pbifs, const Verbosity &)
Reads ArrayOfAgenda from XML input stream.
void xml_write_footer_to_stream(ostream &os, const Verbosity &verbosity)
Write closing root tag.
Definition: xml_io_base.cc:696
FileType
Definition: xml_io_base.h:42
@ FILE_TYPE_ZIPPED_ASCII
Definition: xml_io_base.h:44
@ FILE_TYPE_BINARY
Definition: xml_io_base.h:45
@ FILE_TYPE_ASCII
Definition: xml_io_base.h:43
NumericType
Definition: xml_io_base.h:48
@ NUMERIC_TYPE_FLOAT
Definition: xml_io_base.h:48
@ NUMERIC_TYPE_DOUBLE
Definition: xml_io_base.h:48
void xml_data_parse_error(XMLTag &tag, const String &str_error)
Throws XML parser runtime error.
Definition: xml_io_base.cc:554
void xml_open_input_file(ifstream &ifs, const String &name, const Verbosity &verbosity)
Open file for XML input.
Definition: xml_io_base.cc:449
void xml_write_to_file_base(const String &filename, const T &type, const FileType ftype, const Verbosity &verbosity)
Write data to XML file.
Definition: xml_io_base.h:313
void xml_parse_from_stream(istream &, Vector &, bifstream *, XMLTag &, const Verbosity &verbosity)
Parses Vector from XML input stream.
void xml_read_footer_from_stream(istream &is, const Verbosity &verbosity)
Reads closing root tag.
Definition: xml_io_base.cc:655
void parse_xml_tag_content_as_string(std::istream &is_xml, String &content)
Get the content of an xml tag as a string.
Definition: xml_io_base.cc:723
void xml_read_header_from_stream(istream &is, FileType &ftype, NumericType &ntype, EndianType &etype, const Verbosity &verbosity)
Reads XML header and root tag.
Definition: xml_io_base.cc:574
void xml_write_header_to_stream(ostream &os, FileType ftype, const Verbosity &verbosity)
Writes XML header and root tag.
Definition: xml_io_base.cc:667
void xml_set_stream_precision(ostream &os)
Definition: xml_io_base.cc:705
FileType string2filetype(const String &file_format)
Definition: xml_io_base.cc:335
void xml_open_output_file(ofstream &file, const String &name)
Open file for XML output.
Definition: xml_io_base.cc:359
EndianType
Definition: xml_io_base.h:49
@ ENDIAN_TYPE_BIG
Definition: xml_io_base.h:49
@ ENDIAN_TYPE_LITTLE
Definition: xml_io_base.h:49
void xml_read_from_file_base(const String &filename, T &type, const Verbosity &verbosity)
Reads data from XML file.
Definition: xml_io_base.h:245
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.
Definition: xml_io_base.cc:539