ARTS 2.5.0 (git: 9ee3ac6c)
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 "arts.h"
35
36#ifdef ENABLE_ZLIB
37#include "gzstream.h"
38#endif
39
44};
45
48
50// XML parser classes
52
54
59 public:
62};
63
65
68class XMLTag {
69 public:
70 XMLTag(const Verbosity& rverbosity) : verbosity(rverbosity){};
71
72 String& get_name() { return name; }
73
74 void check_name(const String& expected_name);
75
76 void set_name(const String& new_name) { name = new_name; }
77
78 void add_attribute(const String& aname, const String& value);
79
80 void add_attribute(const String& aname, const Index& value);
81
87 void add_attribute(const String& aname, const Numeric& value);
88
89 void check_attribute(const String& aname, const String& value);
90
91 void get_attribute_value(const String& aname, String& value);
92
93 void get_attribute_value(const String& aname, Index& value);
94
104 void get_attribute_value(const String& aname, Numeric& value);
105
106 void read_from_stream(istream& is);
107
108 void write_to_stream(ostream& os);
109
115 bool has_attribute(const String& aname) const;
116
117 protected:
121};
122
124// General XML handling routines
126
127void xml_parse_error(const String& str_error);
128
129void xml_data_parse_error(XMLTag& tag, String str_error);
130
131void xml_set_stream_precision(ostream& os);
132
133void parse_xml_tag_content_as_string(std::istream& is_xml, String& content);
134
136 istream &, Vector &, bifstream *, XMLTag &, const Verbosity &verbosity);
137
139 istream &, ArrayOfString &, bifstream *, XMLTag &, const Verbosity &);
140
141FileType string2filetype(const String& file_format);
142
144// Generic IO routines for XML files
146
148
157void xml_read_header_from_stream(istream& is,
158 FileType& ftype,
159 NumericType& ntype,
160 EndianType& etype,
161 const Verbosity& verbosity);
162
164
169void xml_read_footer_from_stream(istream& is, const Verbosity& verbosity);
170
172
176void xml_write_header_to_stream(ostream& os,
177 FileType ftype,
178 const Verbosity& verbosity);
179
181
184void xml_write_footer_to_stream(ostream& os, const Verbosity& verbosity);
185
187
193void xml_open_input_file(ifstream& ifs,
194 const String& name,
195 const Verbosity& verbosity);
196
198
204void xml_open_output_file(ofstream& file, const String& name);
205
206#ifdef ENABLE_ZLIB
207
209
216 const String& name,
217 const Verbosity& verbosity);
218
220
226void xml_open_output_file(ogzstream& file, const String& name);
227
228#endif // ENABLE_ZLIB
229
231// Generic IO routines for XML files
233
235
242template <typename T>
243void xml_read_from_file_base(const String& filename,
244 T& type,
245 const Verbosity& verbosity) {
247
248 out2 << " Reading " + filename + '\n';
249
250 // Open input stream:
251 std::unique_ptr<istream> ifs;
252 if (filename.nelem() > 2 &&
253 filename.substr(filename.length() - 3, 3) == ".gz")
254#ifdef ENABLE_ZLIB
255 {
256 ifs = std::make_unique<igzstream>();
258 *static_cast<igzstream*>(ifs.get()), filename, verbosity);
259 }
260#else
261 {
262 throw runtime_error(
263 "This arts version was compiled without zlib support.\n"
264 "Thus zipped xml files cannot be read.");
265 }
266#endif /* ENABLE_ZLIB */
267 else {
268 ifs = std::make_unique<ifstream>();
270 *static_cast<ifstream*>(ifs.get()), filename, verbosity);
271 }
272
273 // No need to check for error, because xml_open_input_file throws a
274 // runtime_error with an appropriate error message.
275
276 // Read the matrix from the stream. Here we catch the exception,
277 // because then we can issue a nicer error message that includes the
278 // filename.
279 try {
280 FileType ftype;
281 NumericType ntype;
282 EndianType etype;
283
284 xml_read_header_from_stream(*ifs, ftype, ntype, etype, verbosity);
285 if (ftype == FILE_TYPE_ASCII) {
286 xml_read_from_stream(*ifs, type, NULL, verbosity);
287 } else {
288 String bfilename = filename + ".bin";
289 bifstream bifs(bfilename.c_str());
290 xml_read_from_stream(*ifs, type, &bifs, verbosity);
291 }
292 xml_read_footer_from_stream(*ifs, verbosity);
293 } catch (const std::runtime_error& e) {
294 ostringstream os;
295 os << "Error reading file: " << filename << '\n' << e.what();
296 throw runtime_error(os.str());
297 }
298}
299
301
310template <typename T>
311void xml_write_to_file_base(const String& filename,
312 const T& type,
313 const FileType ftype,
314 const Verbosity& verbosity) {
316
317 std::unique_ptr<ostream> ofs;
318
319 out2 << " Writing " << filename << '\n';
320 if (ftype == FILE_TYPE_ZIPPED_ASCII)
321#ifdef ENABLE_ZLIB
322 {
323 ofs = std::make_unique<ogzstream>();
324 xml_open_output_file(*static_cast<ogzstream*>(ofs.get()), filename);
325 }
326#else
327 {
328 throw runtime_error(
329 "This arts version was compiled without zlib support.\n"
330 "Thus zipped xml files cannot be written.");
331 }
332#endif /* ENABLE_ZLIB */
333 else {
334 ofs = std::make_unique<ofstream>();
335 xml_open_output_file(*static_cast<ofstream*>(ofs.get()), filename);
336 }
337
338 try {
339 xml_write_header_to_stream(*ofs, ftype, verbosity);
340 if (ftype == FILE_TYPE_ASCII || ftype == FILE_TYPE_ZIPPED_ASCII) {
341 xml_write_to_stream(*ofs, type, NULL, "", verbosity);
342 } else {
343 String bfilename = filename + ".bin";
344 bofstream bofs(bfilename.c_str());
345 xml_write_to_stream(*ofs, type, &bofs, "", verbosity);
346 }
347
348 xml_write_footer_to_stream(*ofs, verbosity);
349 } catch (const std::runtime_error& e) {
350 ostringstream os;
351 os << "Error writing file: " << filename << '\n' << e.what();
352 throw runtime_error(os.str());
353 }
354}
355
356#endif
The global header file for ARTS.
This can be used to make arrays out of anything.
Definition: array.h:107
The Vector class.
Definition: matpackI.h:876
XML attribute class.
Definition: xml_io_base.h:58
String name
Definition: xml_io_base.h:60
String value
Definition: xml_io_base.h:61
The ARTS XML tag class.
Definition: xml_io_base.h:68
String name
Definition: xml_io_base.h:118
void add_attribute(const String &aname, const String &value)
Adds a String attribute to tag.
Definition: xml_io_base.cc:60
bool has_attribute(const String &aname) const
Returns if the attribute exists or not.
Definition: xml_io_base.cc:111
void write_to_stream(ostream &os)
Write XML tag.
Definition: xml_io_base.cc:300
void check_name(const String &expected_name)
Check tag name.
Definition: xml_io_base.cc:48
XMLTag(const Verbosity &rverbosity)
Definition: xml_io_base.h:70
void read_from_stream(istream &is)
Reads next XML tag.
Definition: xml_io_base.cc:179
Array< XMLAttribute > attribs
Definition: xml_io_base.h:119
void get_attribute_value(const String &aname, String &value)
Returns value of attribute as String.
Definition: xml_io_base.cc:124
void set_name(const String &new_name)
Definition: xml_io_base.h:76
String & get_name()
Definition: xml_io_base.h:72
void check_attribute(const String &aname, const String &value)
Checks whether attribute has the expected value.
Definition: xml_io_base.cc:97
const Verbosity & verbosity
Definition: xml_io_base.h:120
Binary output file stream class.
Definition: bifstream.h:42
Binary output file stream class.
Definition: bofstream.h:42
Index nelem() const
Number of elements.
Definition: mystring.h:253
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
#define CREATE_OUT2
Definition: messages.h:206
char Type type
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:674
FileType
Definition: xml_io_base.h:40
@ FILE_TYPE_ZIPPED_ASCII
Definition: xml_io_base.h:42
@ FILE_TYPE_BINARY
Definition: xml_io_base.h:43
@ FILE_TYPE_ASCII
Definition: xml_io_base.h:41
NumericType
Definition: xml_io_base.h:46
@ NUMERIC_TYPE_FLOAT
Definition: xml_io_base.h:46
@ NUMERIC_TYPE_DOUBLE
Definition: xml_io_base.h:46
void xml_open_input_file(ifstream &ifs, const String &name, const Verbosity &verbosity)
Open file for XML input.
Definition: xml_io_base.cc:427
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:311
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:633
void xml_data_parse_error(XMLTag &tag, String str_error)
Throws XML parser runtime error.
Definition: xml_io_base.cc:532
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:701
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:552
void xml_write_header_to_stream(ostream &os, FileType ftype, const Verbosity &verbosity)
Writes XML header and root tag.
Definition: xml_io_base.cc:645
void xml_set_stream_precision(ostream &os)
Definition: xml_io_base.cc:683
FileType string2filetype(const String &file_format)
Definition: xml_io_base.cc:313
void xml_open_output_file(ofstream &file, const String &name)
Open file for XML output.
Definition: xml_io_base.cc:337
EndianType
Definition: xml_io_base.h:47
@ ENDIAN_TYPE_BIG
Definition: xml_io_base.h:47
@ ENDIAN_TYPE_LITTLE
Definition: xml_io_base.h:47
void xml_read_from_file_base(const String &filename, T &type, const Verbosity &verbosity)
Reads data from XML file.
Definition: xml_io_base.h:243
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.
Definition: xml_io_base.cc:517