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