DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::oks::OksXmlInputStream Class Reference

#include <xml.hpp>

Collaboration diagram for dunedaq::oks::OksXmlInputStream:
[legend]

Public Member Functions

 OksXmlInputStream (std::shared_ptr< std::istream > p)
 ~OksXmlInputStream ()
void get_num_token (char last)
size_t get_quoted ()
const char * get_tag ()
const char * get_tag_start ()
bool good () const
bool eof () const
void store_position ()
void restore_position ()
long get_position () const
void seek_position (std::streamoff off)
std::ostream & error_msg (const char *)
unsigned long get_line_no () const
unsigned long get_line_pos () const
OksXmlToken & get_xml_token ()
OksXmlValue get_value (unsigned int len)
void throw_unexpected_tag (const char *what, const char *expected=0)
void throw_unexpected_attribute (const char *what)

Static Public Member Functions

static void __throw_strto (const char *f, const char *where, const char *what, unsigned long line_no, unsigned long line_pos)

Private Member Functions

void init ()
size_t get_token (const char s1, OksXmlToken &token)
size_t get_token2 (char, const char s1, const char s2, OksXmlToken &token)
void get_token5 (const char s1, const char s2, const char s3, const char s4, const char s5, OksXmlToken &token)
char cvt_char ()
char get_first_non_empty ()
char get ()
unsigned long get_value_pre ()
void get_value_post (unsigned long)
void __throw_eof () const
 OksXmlInputStream (const OksXmlInputStream &)
OksXmlInputStream & operator= (const OksXmlInputStream &)

Private Attributes

std::shared_ptr< std::istream > f
std::streambuf * m_pbuf
unsigned long line_no
unsigned long line_pos
char last_read_c
std::istream::pos_type pos
unsigned long m_line_no_sav
unsigned long m_line_pos_sav
OksXmlToken * m_cvt_char
OksXmlToken * m_v1
OksXmlToken * m_v2
OksXmlToken * m_v3

Static Private Attributes

static OksXmlTokenPool s_tokens_pool

Friends

struct OksXmlAttribute
struct OksData
class OksObject

Detailed Description

Definition at line 335 of file xml.hpp.

Constructor & Destructor Documentation

◆ OksXmlInputStream() [1/2]

dunedaq::oks::OksXmlInputStream::OksXmlInputStream ( std::shared_ptr< std::istream > p)
inline

Definition at line 343 of file xml.hpp.

344 : f(p)
345 , m_pbuf(p->rdbuf())
346 , line_no(1)
347 , line_pos(0)
348 {
349 init();
350 };
std::streambuf * m_pbuf
Definition xml.hpp:418
std::shared_ptr< std::istream > f
Definition xml.hpp:417

◆ ~OksXmlInputStream()

dunedaq::oks::OksXmlInputStream::~OksXmlInputStream ( )
inline

Definition at line 352 of file xml.hpp.

353 {
354 std::lock_guard lock(s_tokens_pool.m_mutex);
359 }
static OksXmlTokenPool s_tokens_pool
Definition xml.hpp:429
void release(OksXmlToken *token)
Definition xml.hpp:328

◆ OksXmlInputStream() [2/2]

dunedaq::oks::OksXmlInputStream::OksXmlInputStream ( const OksXmlInputStream & )
private

Member Function Documentation

◆ __throw_eof()

void dunedaq::oks::OksXmlInputStream::__throw_eof ( ) const
private

Definition at line 645 of file xml.cpp.

646{
647 throw std::runtime_error("unexpected end of file");
648}

◆ __throw_strto()

void dunedaq::oks::OksXmlInputStream::__throw_strto ( const char * f,
const char * where,
const char * what,
unsigned long line_no,
unsigned long line_pos )
static

Definition at line 651 of file xml.cpp.

656{
657 std::ostringstream text;
658 text << "function " << f << "(\'" << what << "\') has failed";
659 if (*where)
660 text << " on unrecognized characters \'" << where << "\'";
661 if (errno)
662 text << " with code " << errno << ", reason = \'" << oks::strerror(errno) << '\'';
663 throw oks::BadFileData(text.str(), line_no, line_pos);
664}
const std::string strerror(int error)
Convert C error number to string.
Definition kernel.cpp:111

◆ cvt_char()

char dunedaq::oks::OksXmlInputStream::cvt_char ( )
private

Read encoded symbol (&xxx;)

Definition at line 610 of file xml.cpp.

611{
612 get_token(';', *m_cvt_char);
613
615 return '<';
617 return '>';
619 return '&';
621 return '\r';
623 return '\n';
625 return '\t';
627 return '\'';
629 return '\"';
630 else {
631 std::string text("bad symbol \'&");
632 text += m_cvt_char->m_buf;
633 text += '\'';
634 throw std::runtime_error(text.c_str());
635 }
636}
size_t get_token(const char s1, OksXmlToken &token)
Definition xml.cpp:430
const char left_angle_bracket[]
Definition xml.cpp:30
const char new_line[]
Definition xml.cpp:34
const char tabulation[]
Definition xml.cpp:35
const char single_quote[]
Definition xml.cpp:36
const char ampersand[]
Definition xml.cpp:32
const char right_angle_bracket[]
Definition xml.cpp:31
const char carriage_return[]
Definition xml.cpp:33
const char double_quote[]
Definition xml.cpp:37
bool cmp_str2n(const char *s1, const char s2[2])
Definition cstring.hpp:27
bool cmp_str3n(const char *s1, const char s2[3])
Definition cstring.hpp:39
bool cmp_str4n(const char *s1, const char s2[4])
Definition cstring.hpp:51

◆ eof()

bool dunedaq::oks::OksXmlInputStream::eof ( ) const
inline

Definition at line 380 of file xml.hpp.

380{ return f->eof(); }

◆ error_msg()

std::ostream & dunedaq::oks::OksXmlInputStream::error_msg ( const char * msg)

Definition at line 639 of file xml.cpp.

640{
641 return Oks::error_msg(msg) << "(line " << line_no << ", char " << line_pos << ")\n";
642}
static std::ostream & error_msg(const char *)
Definition kernel.cpp:511

◆ get()

char dunedaq::oks::OksXmlInputStream::get ( )
inlineprivate

Definition at line 478 of file xml.hpp.

479{
480 char c(m_pbuf->sbumpc());
481
482 line_pos++;
483 if (__builtin_expect((c == EOF), 0))
484 __throw_eof();
485 else if (__builtin_expect((c == '\n'), 0)) {
486 line_no++;
487 line_pos = 0;
488 }
489
490 return c;
491}

◆ get_first_non_empty()

char dunedaq::oks::OksXmlInputStream::get_first_non_empty ( )
inlineprivate

Definition at line 499 of file xml.hpp.

500{
501 while (true) {
502 char c(get());
503 if (c == ' ' || c == '\n' || c == '\r' || c == '\t')
504 continue;
505 else
506 return c;
507 }
508}

◆ get_line_no()

unsigned long dunedaq::oks::OksXmlInputStream::get_line_no ( ) const
inline

Definition at line 400 of file xml.hpp.

400{ return line_no; }

◆ get_line_pos()

unsigned long dunedaq::oks::OksXmlInputStream::get_line_pos ( ) const
inline

Definition at line 401 of file xml.hpp.

401{ return line_pos; }

◆ get_num_token()

void dunedaq::oks::OksXmlInputStream::get_num_token ( char last)

◆ get_position()

long dunedaq::oks::OksXmlInputStream::get_position ( ) const
inline

Definition at line 394 of file xml.hpp.

394{ return f->tellg(); }

◆ get_quoted()

size_t dunedaq::oks::OksXmlInputStream::get_quoted ( )

Definition at line 297 of file xml.cpp.

298{
299 size_t pos(0);
300
301 try {
302 char c = get_first_non_empty();
303
304 if (__builtin_expect((c != '\"'), 0)) {
306 }
307
308 while (true) {
309 c = get();
310
311 if (__builtin_expect((c == '\"'), 0)) {
312 m_v1->m_buf[pos] = 0;
313 return pos;
314 } else if (__builtin_expect((c == '&'), 0)) {
315 c = cvt_char();
316 }
317
318 m_v1->realloc(pos);
319 m_v1->m_buf[pos++] = c;
320 }
321
322 throw std::runtime_error("cannot find closing quote character");
323 } catch (std::exception& ex) {
324 m_v1->m_buf[pos] = 0;
325 throw oks::BadFileData(std::string("Failed to read quoted string: ") + ex.what(), line_no, line_pos);
326 }
327}
std::istream::pos_type pos
Definition xml.hpp:424
static void __throw_runtime_error_unexpected_symbol(const char expected, const char read)
Definition xml.cpp:104
void realloc(unsigned long pos)
Definition xml.hpp:209

◆ get_tag()

const char * dunedaq::oks::OksXmlInputStream::get_tag ( )

Definition at line 343 of file xml.cpp.

344{
345 m_v1->m_buf[0] = 0;
346
347 unsigned long start_line_no = line_no;
348 unsigned long start_line_pos = line_pos;
349
350 size_t pos(0);
351
352 try {
353 while (true) {
354 char c(get_first_non_empty());
355
356 if (__builtin_expect((c != '<'), 0)) {
358 }
359
360 start_line_no = line_no;
361 start_line_pos = line_pos;
362
363 m_v1->m_buf[0] = c;
364 pos = 1;
365
366 int count(1);
367
368 while (true) {
369 char c2 = get();
370
371 if (c2 == '<')
372 count++;
373 else if (c2 == '>')
374 count--;
375
376 m_v1->realloc(pos);
377 m_v1->m_buf[pos++] = c2;
378
379 if (pos > 3) {
380 if (__is_comment(m_v1->m_buf + pos - 4)) {
381 char buf[3] = { 0, 0, 0 };
382 unsigned long start_comment_line_no = line_no;
383 try {
384 while (true) {
385 buf[0] = buf[1];
386 buf[1] = buf[2];
387 buf[2] = get();
388
389 if (oks::cmp_str3n(buf, "-->")) {
390 break; // end of comment = '-->'
391 }
392 }
393 } catch (std::exception& failure) {
394 std::ostringstream what;
395 what << "Cannot find end of comment started on line " << start_comment_line_no << ": " << failure.what();
396 throw oks::BadFileData(what.str(), line_no, line_pos);
397 }
398
399 pos -= 4;
400 count--;
401 }
402 }
403
404 if (count == 0) {
405 m_v1->m_buf[pos] = '\0';
406 break;
407 }
408 }
409
410 if (pos > 0) {
411 if (count != 0) {
412 throw std::runtime_error("unbalanced tags");
413 } else {
414 return m_v1->m_buf;
415 }
416 }
417 }
418 } catch (std::exception& ex) {
419 m_v1->m_buf[pos] = '\0';
420 throw oks::BadFileData(std::string(ex.what()) + " when read xml tag started at", start_line_no, start_line_pos);
421 }
422}
static void __throw_bad_file_data_unexpected_symbol(const char *what, const char expected, const char read, unsigned long l, unsigned long p)
Definition xml.cpp:110
bool __is_comment(const char *s)
Definition xml.cpp:332

◆ get_tag_start()

const char * dunedaq::oks::OksXmlInputStream::get_tag_start ( )

Definition at line 510 of file xml.cpp.

511{
512 try {
513 while (true) {
514 char c = get_first_non_empty();
515
516 if (__builtin_expect((c != '<'), 0)) {
518 }
519
520 get_token5(' ', '>', '\t', '\n', '\r', *m_v1);
521 TLOG_DEBUG(8) << "read tag \'" << m_v1->m_buf << '\'';
522
523 if (__builtin_expect((oks::cmp_str3n(m_v1->m_buf, "!--")), 0)) {
524 char buf[3] = { 0, 0, 0 };
525 unsigned long start_comment_line_no = line_no;
526 auto tag_len = strlen(m_v1->m_buf);
527 if (tag_len < 5 || !oks::cmp_str2n(m_v1->m_buf + tag_len - 2,
528 "--")) // special case for comments without empty symbols like <!--foo-->
529 try {
530 while (true) {
531 buf[0] = buf[1];
532 buf[1] = buf[2];
533 c = buf[2] = get();
534 if (oks::cmp_str3n(buf, "-->")) {
535 break; // end of comment = '-->'
536 }
537 }
538 } catch (std::exception& failure) {
539 std::ostringstream what;
540 what << "Cannot find end of comment started on line " << start_comment_line_no << ": " << failure.what();
541 throw oks::BadFileData(what.str(), line_no, line_pos);
542 }
543 }
544
545 if (__builtin_expect((c != '>'), 1))
546 return m_v1->m_buf;
547 }
548 } catch (std::exception& ex) {
549 throw oks::BadFileData(std::string("Failed to read start-of-tag: ") + ex.what(), line_no, line_pos);
550 }
551
552 return 0; // never reach this line
553}
void get_token5(const char s1, const char s2, const char s3, const char s4, const char s5, OksXmlToken &token)
Definition xml.cpp:481
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:116

◆ get_token()

size_t dunedaq::oks::OksXmlInputStream::get_token ( const char s1,
OksXmlToken & token )
inlineprivate

Read token (one separator)

Definition at line 430 of file xml.cpp.

431{
432 size_t pos(0);
433
434 while (true) {
435 last_read_c = get();
436
437 if (last_read_c == __s1) {
438 token.m_buf[pos] = '\0';
439 return pos;
440 } else if (__builtin_expect((last_read_c == '&'), 0))
442
443 token.realloc(pos);
444 token.m_buf[pos++] = last_read_c;
445 }
446
447 token.m_buf[pos] = 0;
448
449 throw std::runtime_error("cannot find closing separator while read token");
450}

◆ get_token2()

size_t dunedaq::oks::OksXmlInputStream::get_token2 ( char __fs,
const char s1,
const char s2,
OksXmlToken & token )
inlineprivate

Read token (two separators)

Definition at line 453 of file xml.cpp.

454{
455 size_t pos(1);
456 token.m_buf[0] = __fs;
457 if (__fs == __s1 || __fs == __s2) {
458 token.m_buf[1] = '\0';
459 return 1;
460 }
461
462 while (true) {
463 last_read_c = get();
464
465 if (last_read_c == __s1 || last_read_c == __s2) {
466 token.m_buf[pos] = '\0';
467 return pos;
468 } else if (__builtin_expect((last_read_c == '&'), 0))
470
471 token.realloc(pos);
472 token.m_buf[pos++] = last_read_c;
473 }
474
475 token.m_buf[pos] = 0;
476
477 throw std::runtime_error("cannot find closing separator while read token");
478}

◆ get_token5()

void dunedaq::oks::OksXmlInputStream::get_token5 ( const char s1,
const char s2,
const char s3,
const char s4,
const char s5,
OksXmlToken & token )
inlineprivate

Read token (five separators)

Definition at line 481 of file xml.cpp.

487{
488 size_t pos(0);
489
490 while (true) {
491 last_read_c = get();
492
493 if (last_read_c == __s1 || last_read_c == __s2 || last_read_c == __s3 || last_read_c == __s4 ||
494 last_read_c == __s5) {
495 token.m_buf[pos] = '\0';
496 return;
497 } else if (__builtin_expect((last_read_c == '&'), 0))
499
500 token.realloc(pos);
501 token.m_buf[pos++] = last_read_c;
502 }
503
504 token.m_buf[pos] = 0;
505
506 throw std::runtime_error("cannot find closing separator while read token");
507}

◆ get_value()

OksXmlValue dunedaq::oks::OksXmlInputStream::get_value ( unsigned int len)
inline

Definition at line 405 of file xml.hpp.

406 {
407 OksXmlValue value(m_v2, len, line_no, line_pos);
408 m_v2 = m_v3;
409 m_v3 = value.get_token();
410 return value;
411 }

◆ get_value_post()

void dunedaq::oks::OksXmlInputStream::get_value_post ( unsigned long )
inlineprivate

◆ get_value_pre()

unsigned long dunedaq::oks::OksXmlInputStream::get_value_pre ( )
inlineprivate

◆ get_xml_token()

OksXmlToken & dunedaq::oks::OksXmlInputStream::get_xml_token ( )
inline

Definition at line 403 of file xml.hpp.

403{ return *m_v1; }

◆ good()

bool dunedaq::oks::OksXmlInputStream::good ( ) const
inline

Definition at line 379 of file xml.hpp.

379{ return f->good(); }

◆ init()

void dunedaq::oks::OksXmlInputStream::init ( )
inlineprivate

Definition at line 436 of file xml.hpp.

437 {
438 f->setf(std::ios::showbase, std::ios::basefield);
439 f->exceptions(std::istream::eofbit | std::istream::failbit | std::istream::badbit);
440
441 std::lock_guard lock(s_tokens_pool.m_mutex);
446 }
OksXmlToken * get()
Definition xml.hpp:312

◆ operator=()

OksXmlInputStream & dunedaq::oks::OksXmlInputStream::operator= ( const OksXmlInputStream & )
private

◆ restore_position()

void dunedaq::oks::OksXmlInputStream::restore_position ( )
inline

Definition at line 388 of file xml.hpp.

389 {
390 f->seekg(pos);
393 }
unsigned long m_line_pos_sav
Definition xml.hpp:427
unsigned long m_line_no_sav
Definition xml.hpp:426

◆ seek_position()

void dunedaq::oks::OksXmlInputStream::seek_position ( std::streamoff off)
inline

Definition at line 396 of file xml.hpp.

396{ f->seekg(off, std::ios_base::cur); }

◆ store_position()

void dunedaq::oks::OksXmlInputStream::store_position ( )
inline

Definition at line 382 of file xml.hpp.

383 {
384 pos = f->tellg();
387 }

◆ throw_unexpected_attribute()

void dunedaq::oks::OksXmlInputStream::throw_unexpected_attribute ( const char * what)

Definition at line 291 of file xml.cpp.

292{
293 throw oks::BadFileData(std::string("Value \'") + what + "\' is not a valid attribute name", line_no, line_pos);
294}

◆ throw_unexpected_tag()

void dunedaq::oks::OksXmlInputStream::throw_unexpected_tag ( const char * what,
const char * expected = 0 )

Definition at line 270 of file xml.cpp.

271{
272 std::ostringstream s;
273
274 if (!*what) {
275 if (expected) {
276 s << "expected tag \'" << expected << '\'';
277 } else {
278 s << "empty tag";
279 }
280 } else {
281 s << "unexpected tag \'" << what << '\'';
282 if (expected) {
283 s << " instead of \'" << expected << '\'';
284 }
285 }
286
287 throw oks::BadFileData(s.str(), line_no, line_pos);
288}

◆ OksData

friend struct OksData
friend

Definition at line 339 of file xml.hpp.

◆ OksObject

friend class OksObject
friend

Definition at line 340 of file xml.hpp.

◆ OksXmlAttribute

friend struct OksXmlAttribute
friend

Definition at line 338 of file xml.hpp.

Member Data Documentation

◆ f

std::shared_ptr<std::istream> dunedaq::oks::OksXmlInputStream::f
private

Definition at line 417 of file xml.hpp.

◆ last_read_c

char dunedaq::oks::OksXmlInputStream::last_read_c
private

Definition at line 422 of file xml.hpp.

◆ line_no

unsigned long dunedaq::oks::OksXmlInputStream::line_no
private

Definition at line 419 of file xml.hpp.

◆ line_pos

unsigned long dunedaq::oks::OksXmlInputStream::line_pos
private

Definition at line 420 of file xml.hpp.

◆ m_cvt_char

OksXmlToken* dunedaq::oks::OksXmlInputStream::m_cvt_char
private

Definition at line 431 of file xml.hpp.

◆ m_line_no_sav

unsigned long dunedaq::oks::OksXmlInputStream::m_line_no_sav
private

Definition at line 426 of file xml.hpp.

◆ m_line_pos_sav

unsigned long dunedaq::oks::OksXmlInputStream::m_line_pos_sav
private

Definition at line 427 of file xml.hpp.

◆ m_pbuf

std::streambuf* dunedaq::oks::OksXmlInputStream::m_pbuf
private

Definition at line 418 of file xml.hpp.

◆ m_v1

OksXmlToken* dunedaq::oks::OksXmlInputStream::m_v1
private

Definition at line 432 of file xml.hpp.

◆ m_v2

OksXmlToken* dunedaq::oks::OksXmlInputStream::m_v2
private

Definition at line 433 of file xml.hpp.

◆ m_v3

OksXmlToken* dunedaq::oks::OksXmlInputStream::m_v3
private

Definition at line 434 of file xml.hpp.

◆ pos

std::istream::pos_type dunedaq::oks::OksXmlInputStream::pos
private

Definition at line 424 of file xml.hpp.

◆ s_tokens_pool

OksXmlTokenPool dunedaq::oks::OksXmlInputStream::s_tokens_pool
staticprivate

Definition at line 429 of file xml.hpp.


The documentation for this class was generated from the following files: