DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
xml.hpp
Go to the documentation of this file.
1#ifndef OKS_XML_H
2#define OKS_XML_H
3
4#include <string.h>
5
6#include <memory>
7#include <stack>
8#include <fstream>
9#include <stdexcept>
10
11#include "oks/defs.hpp"
12#include "oks/exceptions.hpp"
13#include "oks/cstring.hpp"
14
15namespace dunedaq {
16namespace oks {
17
18 struct OksData;
19
20 namespace xml {
21
23 inline const char * bool2str(bool b) noexcept { return (b ? "yes" : "no"); }
24
26 inline bool str2bool(const char * s) noexcept { return oks::cmp_str3(s, "yes"); }
27
28 }
29
32 class BadFileData : public exception {
33
34 public:
35
36 BadFileData(const std::string& what, unsigned long line_no, unsigned long line_pos, int level_arg = 0) noexcept : exception (fill(what, line_no, line_pos), level_arg) {}
37
38 virtual ~BadFileData() noexcept { }
39
40
41 private:
42
43 static std::string fill(const std::string& what, unsigned long line_no, unsigned long line_pos) noexcept;
44 };
45
46
49 class FailedRead : public exception {
50
51 public:
52
54 FailedRead(const std::string& what, const exception& reason) noexcept : exception (fill(what, reason.what()), reason.level() + 1) { }
55
57 FailedRead(const std::string& what, const std::string& reason) noexcept : exception (fill(what, reason), 0) { }
58
59 virtual ~FailedRead() noexcept { }
60
61
62 private:
63
64 static std::string fill(const std::string& what, const std::string& reason) noexcept;
65
66 };
67
68
71 class FailedSave : public exception {
72
73 public:
74
76 FailedSave(const std::string& what, const std::string& name, const exception& reason) noexcept : exception (fill(what, name, reason.what()), reason.level() + 1) { }
77
79 FailedSave(const std::string& what, const std::string& name, const std::string& reason) noexcept : exception (fill(what, name, reason), 0) { }
80
81 virtual ~FailedSave() noexcept { }
82
83
84 private:
85
86 static std::string fill(const std::string& what, const std::string& name, const std::string& reason) noexcept;
87
88 };
89
90
91 class EndOfXmlStream : public exception {
92
93 public:
94
95 EndOfXmlStream(const std::string& tag) noexcept : exception (fill(tag), 0) { }
96
97 virtual ~EndOfXmlStream() noexcept { }
98
99
100 private:
101
102 static std::string fill(const std::string& tag);
103
104 };
105
106 struct ReadFileParams;
107
108
109
111{
112
113public:
114
115 OksXmlOutputStream(std::ostream &p) : f(p)
116 {
117 ;
118 }
119
120 std::ostream&
122 {
123 return f;
124 }
125
130 void
131 put(char c);
132
137 void
138 put(const char * s);
139
144 void
145 put_raw(char c)
146 {
147 if (__builtin_expect((f.rdbuf()->sputc(c) != c), 0))
149 }
150
151 void
152 put_raw(const char * s, long len)
153 {
154 if (__builtin_expect((f.rdbuf()->sputn(s, len) != len), 0))
156 }
157
158 template<class T>
159 void
160 put_value(T value)
161 {
162 f << value;
163 }
164
165 void
166 put_quoted(const char *);
167
168 void
169 put_start_tag(const char *, size_t len); // '<tag-name'
170
171 void
172 put_end_tag(); // '/>\n'
173
174 void
175 put_last_tag(const char *, size_t len); // '</tag-name>\n'
176
177 void
178 put_attribute(const char *, size_t len, const char *);
179
180 void
181 put_attribute(const char *, size_t len, uint32_t);
182
183 void
184 put_attribute(const char *, size_t len, const OksData&);
185
186 void
187 put_eol(); // '>\n'
188
189private:
190
191 std::ostream& f;
192
193 static void
195
196};
197
198
200
201 size_t m_len;
202 size_t m_len2; // (m_len - 2)
203 char * m_buf;
204
205 OksXmlToken(size_t len = 2048) : m_len(len), m_len2(len-2), m_buf(new char [len]) { ; }
206 ~OksXmlToken() {delete [] m_buf;}
207
208 void realloc(unsigned long pos) {
209 if( __builtin_expect((pos >= m_len2), 0) ) {
210 m_len += 2048;
211 m_len2 = m_len-2;
212 char *ptr = new char [m_len];
213 memcpy(ptr, m_buf, pos);
214 delete [] m_buf;
215 m_buf = ptr;
216 }
217 }
218
219};
220
221
222class OksXmlInputStream;
223class OksClass;
224class OksString;
225
227{
229 unsigned int m_len;
230 unsigned int m_line_no;
231 unsigned int m_line_pos;
232
233 OksXmlValue(OksXmlToken * token = nullptr, unsigned int len = 0, unsigned int line_no = 0, unsigned int line_pos = 0) :
235 {
236 ;
237 }
238
239 bool
241 {
242 return (m_token == nullptr);
243 }
244
246 get_token() const
247 {
248 return m_token;
249 }
250
251 char * buf() const
252 {
253 return m_token->m_buf;
254 }
255
256 unsigned int len() const
257 {
258 return m_len;
259 }
260
261 unsigned int line_no() const
262 {
263 return m_line_no;
264 }
265
266 unsigned int line_pos() const
267 {
268 return m_line_pos;
269 }
270};
271
273{
274 OksXmlRelValue(const oks::ReadFileParams& file_params) : m_file_params(file_params), m_class(nullptr), m_class_id(nullptr)
275 {
276 ;
277 }
278
279 bool is_empty()
280 {
281 return m_value.is_empty() || (m_class == nullptr && m_class_id == nullptr);
282 }
283
288};
289
295
299
301
302 char * name() const {return p_name.m_buf;}
303 char * value() const {return p_value.m_buf;}
304 size_t value_len() const {return p_value_len;}
305};
306
308
309 friend class OksXmlInputStream;
310
311 public:
312
314 while(!m_tokens.empty()) {
315 OksXmlToken * t = m_tokens.top();
316 m_tokens.pop();
317 delete t;
318 }
319 }
320
321
322 private:
323
330 if(!m_tokens.empty()) {
331 OksXmlToken * t = m_tokens.top();
332 m_tokens.pop();
333 return t;
334 }
335 else {
336 return new OksXmlToken();
337 }
338 }
339
340
346 void release(OksXmlToken * token) {
347 m_tokens.push(token);
348 }
349
350
351 private:
352
353 std::mutex m_mutex;
354 std::stack<OksXmlToken *> m_tokens;
355
356};
357
358
360
361friend struct OksXmlAttribute;
362friend struct OksData;
363friend class OksObject;
364
365public:
366
367 OksXmlInputStream(std::shared_ptr<std::istream> p) :
368 f(p), m_pbuf(p->rdbuf()), line_no(1), line_pos(0)
369 {
370 init();
371 };
372
380
381
382 // get token separated by whitespace or "last" symbol
383 // put result on m_cvt_char token
384
385 void get_num_token(char last);
386
387
388 // throw exception
389
390 static void __throw_strto(const char * f, const char * where, const char * what, unsigned long line_no, unsigned long line_pos);
391
392 size_t get_quoted();
393
394 const char * get_tag();
395 const char * get_tag_start();
396
397 bool good() const { return f->good(); }
398 bool eof() const { return f->eof(); }
399
402 long get_position() const {
403 return f->tellg();
404 }
405
406 void inline seek_position(std::streamoff off) { f->seekg(off, std::ios_base::cur); }
407
408 std::ostream& error_msg(const char *);
409
410 unsigned long get_line_no() const { return line_no; }
411 unsigned long get_line_pos() const { return line_pos; }
412
414
416 get_value(unsigned int len)
417 {
418 OksXmlValue value(m_v2, len, line_no, line_pos);
419 m_v2 = m_v3;
420 m_v3 = value.get_token();
421 return value;
422 }
423
424 void throw_unexpected_tag(const char * what, const char * expected = 0);
425 void throw_unexpected_attribute(const char * what);
426
427private:
428
429 std::shared_ptr<std::istream> f;
430 std::streambuf * m_pbuf;
431 unsigned long line_no;
432 unsigned long line_pos;
433
435
436 std::istream::pos_type pos;
437
438 unsigned long m_line_no_sav;
439 unsigned long m_line_pos_sav;
440
442
443 OksXmlToken * m_cvt_char; // the token to be used by cvt_char()
446 OksXmlToken * m_v3; // used for swap with m_v2 to keep value of "val" attribute
447
448
449 void init() {
450 f->setf(std::ios::showbase, std::ios::basefield);
451 f->exceptions ( std::istream::eofbit | std::istream::failbit | std::istream::badbit );
452
453 std::lock_guard lock(s_tokens_pool.m_mutex);
458 }
459
460 inline size_t get_token(const char s1, OksXmlToken& token);
461 inline size_t get_token2(char, const char s1, const char s2, OksXmlToken& token);
462 inline void get_token5(const char s1, const char s2, const char s3, const char s4, const char s5, OksXmlToken& token);
463 char cvt_char();
465 inline char get_first_non_empty();
466 inline char get();
467
468 inline unsigned long get_value_pre();
469 inline void get_value_post(unsigned long);
470
471 void __throw_eof() const;
472
473
474 // protect usage of copy constructor and assignment operator
475
478
479};
480
481
482 //
483 // Read new symbol from stream and change LINE and POS numbers.
484 // Throw exception in case of I/O error or EOF
485 //
486
487inline char
489{
490 char c(m_pbuf->sbumpc());
491
492 line_pos++;
493 if( __builtin_expect((c == EOF), 0) ) __throw_eof();
494 else if( __builtin_expect((c == '\n'), 0) ) { line_no++; line_pos = 0; }
495
496 return c;
497}
498
499
500 //
501 // Read first non-null xml symbol
502 // Return 0 if error
503 //
504
505inline char
507{
508 while(true) {
509 char c(get());
510 if(c == ' ' || c == '\n' || c == '\r' || c == '\t') continue;
511 else return c;
512 }
513}
514} // namespace oks
515} // namespace dunedaq
516#endif
BadFileData(const std::string &what, unsigned long line_no, unsigned long line_pos, int level_arg=0) noexcept
Definition xml.hpp:36
virtual ~BadFileData() noexcept
Definition xml.hpp:38
static std::string fill(const std::string &what, unsigned long line_no, unsigned long line_pos) noexcept
Definition xml.cpp:52
virtual ~EndOfXmlStream() noexcept
Definition xml.hpp:97
EndOfXmlStream(const std::string &tag) noexcept
Definition xml.hpp:95
static std::string fill(const std::string &tag)
Definition xml.cpp:72
FailedRead(const std::string &what, const std::string &reason) noexcept
Definition xml.hpp:57
FailedRead(const std::string &what, const exception &reason) noexcept
Definition xml.hpp:54
virtual ~FailedRead() noexcept
Definition xml.hpp:59
static std::string fill(const std::string &what, const std::string &reason) noexcept
Definition xml.cpp:60
FailedSave(const std::string &what, const std::string &name, const std::string &reason) noexcept
Definition xml.hpp:79
FailedSave(const std::string &what, const std::string &name, const exception &reason) noexcept
Definition xml.hpp:76
static std::string fill(const std::string &what, const std::string &name, const std::string &reason) noexcept
Definition xml.cpp:66
virtual ~FailedSave() noexcept
Definition xml.hpp:81
The OKS class.
Definition class.hpp:200
OksObject describes instance of OksClass.
Definition object.hpp:836
Class OKS string.
Definition object.hpp:369
void throw_unexpected_attribute(const char *what)
Definition xml.cpp:284
unsigned long get_line_no() const
Definition xml.hpp:410
std::ostream & error_msg(const char *)
Definition xml.cpp:607
unsigned long m_line_pos_sav
Definition xml.hpp:439
std::streambuf * m_pbuf
Definition xml.hpp:430
OksXmlInputStream(const OksXmlInputStream &)
size_t get_token(const char s1, OksXmlToken &token)
Definition xml.cpp:423
size_t get_token2(char, const char s1, const char s2, OksXmlToken &token)
Definition xml.cpp:443
void throw_unexpected_tag(const char *what, const char *expected=0)
Definition xml.cpp:261
unsigned long get_line_pos() const
Definition xml.hpp:411
OksXmlValue get_value(unsigned int len)
Definition xml.hpp:416
void get_token5(const char s1, const char s2, const char s3, const char s4, const char s5, OksXmlToken &token)
Definition xml.cpp:465
OksXmlInputStream(std::shared_ptr< std::istream > p)
Definition xml.hpp:367
OksXmlInputStream & operator=(const OksXmlInputStream &)
OksXmlToken & get_xml_token()
Definition xml.hpp:413
unsigned long m_line_no_sav
Definition xml.hpp:438
void seek_position(std::streamoff off)
Definition xml.hpp:406
static void __throw_strto(const char *f, const char *where, const char *what, unsigned long line_no, unsigned long line_pos)
Definition xml.cpp:620
std::shared_ptr< std::istream > f
Definition xml.hpp:429
const char * get_tag_start()
Definition xml.cpp:485
std::istream::pos_type pos
Definition xml.hpp:436
void get_value_post(unsigned long)
static OksXmlTokenPool s_tokens_pool
Definition xml.hpp:441
void put_last_tag(const char *, size_t len)
Definition xml.cpp:204
void put_attribute(const char *, size_t len, const char *)
Definition xml.cpp:220
void put_start_tag(const char *, size_t len)
Definition xml.cpp:171
void put_raw(const char *s, long len)
Definition xml.hpp:152
static void __throw_write_failed()
Definition xml.cpp:112
OksXmlOutputStream(std::ostream &p)
Definition xml.hpp:115
void put_quoted(const char *)
Definition xml.cpp:157
std::ostream & get_stream() const
Definition xml.hpp:121
virtual const char * what() const noexcept
caught dunedaq::conffwk::Exception exception
const char * bool2str(bool b) noexcept
Definition xml.hpp:23
bool str2bool(const char *s) noexcept
Definition xml.hpp:26
bool cmp_str3(const char *s1, const char s2[4])
Definition cstring.hpp:21
Including Qt Headers.
Definition __init__.py:1
the structure to pass common parameters to various read() methods of OksData and OksObject class
Definition object.hpp:449
size_t value_len() const
Definition xml.hpp:304
OksXmlAttribute(OksXmlInputStream &s)
Definition xml.cpp:531
OksXmlRelValue(const oks::ReadFileParams &file_params)
Definition xml.hpp:274
const oks::ReadFileParams & m_file_params
Definition xml.hpp:284
OksXmlToken * get()
Definition xml.hpp:329
std::stack< OksXmlToken * > m_tokens
Definition xml.hpp:354
void release(OksXmlToken *token)
Definition xml.hpp:346
OksXmlToken(size_t len=2048)
Definition xml.hpp:205
void realloc(unsigned long pos)
Definition xml.hpp:208
unsigned int line_pos() const
Definition xml.hpp:266
unsigned int len() const
Definition xml.hpp:256
unsigned int line_no() const
Definition xml.hpp:261
unsigned int m_line_no
Definition xml.hpp:230
OksXmlToken * m_token
Definition xml.hpp:228
char * buf() const
Definition xml.hpp:251
OksXmlToken * get_token() const
Definition xml.hpp:246
unsigned int m_len
Definition xml.hpp:229
OksXmlValue(OksXmlToken *token=nullptr, unsigned int len=0, unsigned int line_no=0, unsigned int line_pos=0)
Definition xml.hpp:233
unsigned int m_line_pos
Definition xml.hpp:231