ARTS 2.5.11 (git: 725533f0)
xml_io_base.h
Go to the documentation of this file.
1
2// File description
4
13#ifndef xml_io_base_h
14#define xml_io_base_h
15
16#include <memory>
17
18#include "arts.h"
20
21#ifdef ENABLE_ZLIB
22#include "gzstream.h"
23#endif
24
25enum FileType : Index {
29};
30
33
35// XML parser classes
37
39
44 public:
47};
48
50
53class XMLTag {
54 public:
55 XMLTag(const Verbosity& rverbosity) : verbosity(rverbosity){};
56
57 String& get_name() { return name; }
58
59 void check_name(const String& expected_name);
60
61 void set_name(const String& new_name) { name = new_name; }
62
63 void add_attribute(const String& aname, String value);
64
65 void add_attribute(const String& aname, const Index& value);
66
72 void add_attribute(const String& aname, const Numeric& value);
73
74 void check_attribute(const String& aname, const String& value);
75
76 void get_attribute_value(const String& aname, String& value);
77
78 void get_attribute_value(const String& aname, Index& value);
79
89 void get_attribute_value(const String& aname, Numeric& value);
90
91 void read_from_stream(istream& is);
92
93 void write_to_stream(ostream& os);
94
100 [[nodiscard]] bool has_attribute(const String& aname) const;
101
102 protected:
106};
107
109// General XML handling routines
111
112void xml_parse_error(const String& str_error);
113
114void xml_data_parse_error(XMLTag& tag, const String& str_error);
115
116void xml_set_stream_precision(ostream& os);
117
118void parse_xml_tag_content_as_string(std::istream& is_xml, String& content);
119
121 istream &, Vector &, bifstream *, XMLTag &, const Verbosity &verbosity);
122
124 istream &, ArrayOfString &, bifstream *, XMLTag &, const Verbosity &);
125
126FileType string2filetype(const String& file_format);
127
129// Generic IO routines for XML files
131
133
142void xml_read_header_from_stream(istream& is,
143 FileType& ftype,
144 NumericType& ntype,
145 EndianType& etype,
146 const Verbosity& verbosity);
147
149
154void xml_read_footer_from_stream(istream& is, const Verbosity& verbosity);
155
157
161void xml_write_header_to_stream(ostream& os,
162 FileType ftype,
163 const Verbosity& verbosity);
164
166
169void xml_write_footer_to_stream(ostream& os, const Verbosity& verbosity);
170
172
178void xml_open_input_file(ifstream& ifs,
179 const String& name,
180 const Verbosity& verbosity);
181
183
189void xml_open_output_file(ofstream& file, const String& name);
190
191#ifdef ENABLE_ZLIB
192
194
201 const String& name,
202 const Verbosity& verbosity);
203
205
211void xml_open_output_file(ogzstream& file, const String& name);
212
213#endif // ENABLE_ZLIB
214
216// Generic IO routines for XML files
218
220
227template <typename T>
228void xml_read_from_file_base(const String& filename,
229 T& type,
230 const Verbosity& verbosity) {
232
233 out2 << " Reading " + filename + '\n';
234
235 // Open input stream:
236 std::unique_ptr<istream> ifs;
237 if (filename.nelem() > 2 &&
238 filename.substr(filename.length() - 3, 3) == ".gz")
239#ifdef ENABLE_ZLIB
240 {
241 ifs = std::make_unique<igzstream>();
243 *static_cast<igzstream*>(ifs.get()), filename, verbosity);
244 }
245#else
246 {
247 throw runtime_error(
248 "This arts version was compiled without zlib support.\n"
249 "Thus zipped xml files cannot be read.");
250 }
251#endif /* ENABLE_ZLIB */
252 else {
253 ifs = std::make_unique<ifstream>();
255 *static_cast<ifstream*>(ifs.get()), filename, verbosity);
256 }
257
258 // No need to check for error, because xml_open_input_file throws a
259 // runtime_error with an appropriate error message.
260
261 // Read the matrix from the stream. Here we catch the exception,
262 // because then we can issue a nicer error message that includes the
263 // filename.
264 try {
265 FileType ftype;
266 NumericType ntype;
267 EndianType etype;
268
269 xml_read_header_from_stream(*ifs, ftype, ntype, etype, verbosity);
270 if (ftype == FILE_TYPE_ASCII) {
271 xml_read_from_stream(*ifs, type, NULL, verbosity);
272 } else {
273 String bfilename = filename + ".bin";
274 bifstream bifs(bfilename.c_str());
275 xml_read_from_stream(*ifs, type, &bifs, verbosity);
276 }
277 xml_read_footer_from_stream(*ifs, verbosity);
278 } catch (const std::runtime_error& e) {
279 ostringstream os;
280 os << "Error reading file: " << filename << '\n' << e.what();
281 throw runtime_error(os.str());
282 }
283}
284
286
295template <typename T>
296void xml_write_to_file_base(const String& filename,
297 const T& type,
298 const FileType ftype,
299 const Verbosity& verbosity) {
301
302 std::unique_ptr<ostream> ofs;
303
304 out2 << " Writing " << filename << '\n';
305 if (ftype == FILE_TYPE_ZIPPED_ASCII)
306#ifdef ENABLE_ZLIB
307 {
308 ofs = std::make_unique<ogzstream>();
309 xml_open_output_file(*static_cast<ogzstream*>(ofs.get()), filename);
310 }
311#else
312 {
313 throw runtime_error(
314 "This arts version was compiled without zlib support.\n"
315 "Thus zipped xml files cannot be written.");
316 }
317#endif /* ENABLE_ZLIB */
318 else {
319 ofs = std::make_unique<ofstream>();
320 xml_open_output_file(*static_cast<ofstream*>(ofs.get()), filename);
321 }
322
323 try {
324 xml_write_header_to_stream(*ofs, ftype, verbosity);
325 if (ftype == FILE_TYPE_ASCII || ftype == FILE_TYPE_ZIPPED_ASCII) {
326 xml_write_to_stream(*ofs, type, NULL, "", verbosity);
327 } else {
328 String bfilename = filename + ".bin";
329 bofstream bofs(bfilename.c_str());
330 xml_write_to_stream(*ofs, type, &bofs, "", verbosity);
331 }
332
333 xml_write_footer_to_stream(*ofs, verbosity);
334 } catch (const std::runtime_error& e) {
335 ostringstream os;
336 os << "Error writing file: " << filename << '\n' << e.what();
337 throw runtime_error(os.str());
338 }
339}
340
341#endif
The global header file for ARTS.
This can be used to make arrays out of anything.
Definition array.h:31
XML attribute class.
Definition xml_io_base.h:43
The ARTS XML tag class.
Definition xml_io_base.h:53
String name
bool has_attribute(const String &aname) const
Returns if the attribute exists or not.
void add_attribute(const String &aname, String value)
void write_to_stream(ostream &os)
Write XML tag.
void check_name(const String &expected_name)
Check tag name.
XMLTag(const Verbosity &rverbosity)
Definition xml_io_base.h:55
void read_from_stream(istream &is)
Reads next XML tag.
Array< XMLAttribute > attribs
void get_attribute_value(const String &aname, String &value)
void set_name(const String &new_name)
Definition xml_io_base.h:61
String & get_name()
Definition xml_io_base.h:57
void check_attribute(const String &aname, const String &value)
Checks whether attribute has the expected value.
const Verbosity & verbosity
Binary output file stream class.
Definition bifstream.h:26
Binary output file stream class.
Definition bofstream.h:25
Index nelem() const
Definition mystring.h:172
#define CREATE_OUT2
Definition messages.h:188
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.
FileType
Definition xml_io_base.h:25
@ FILE_TYPE_ZIPPED_ASCII
Definition xml_io_base.h:27
@ FILE_TYPE_BINARY
Definition xml_io_base.h:28
@ FILE_TYPE_ASCII
Definition xml_io_base.h:26
NumericType
Definition xml_io_base.h:31
@ NUMERIC_TYPE_FLOAT
Definition xml_io_base.h:31
@ NUMERIC_TYPE_DOUBLE
Definition xml_io_base.h:31
void xml_data_parse_error(XMLTag &tag, const String &str_error)
Throws XML parser runtime error.
void xml_open_input_file(ifstream &ifs, const String &name, const Verbosity &verbosity)
Open file for XML input.
void xml_write_to_file_base(const String &filename, const T &type, const FileType ftype, const Verbosity &verbosity)
Write data to XML file.
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.
void parse_xml_tag_content_as_string(std::istream &is_xml, String &content)
Get the content of an xml tag as a string.
void xml_read_header_from_stream(istream &is, FileType &ftype, NumericType &ntype, EndianType &etype, const Verbosity &verbosity)
Reads XML header and root tag.
void xml_write_header_to_stream(ostream &os, FileType ftype, const Verbosity &verbosity)
Writes XML header and root tag.
void xml_set_stream_precision(ostream &os)
FileType string2filetype(const String &file_format)
void xml_open_output_file(ofstream &file, const String &name)
Open file for XML output.
EndianType
Definition xml_io_base.h:32
@ ENDIAN_TYPE_BIG
Definition xml_io_base.h:32
@ ENDIAN_TYPE_LITTLE
Definition xml_io_base.h:32
void xml_read_from_file_base(const String &filename, T &type, const Verbosity &verbosity)
Reads data from XML file.
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.