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
OksXmlTokenget_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 &)
OksXmlInputStreamoperator= (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
OksXmlTokenm_cvt_char
OksXmlTokenm_v1
OksXmlTokenm_v2
OksXmlTokenm_v3

Static Private Attributes

static OksXmlTokenPool s_tokens_pool

Friends

struct OksXmlAttribute
struct OksData
class OksObject

Detailed Description

Definition at line 364 of file xml.hpp.

Constructor & Destructor Documentation

◆ OksXmlInputStream() [1/2]

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

Definition at line 372 of file xml.hpp.

372 :
373 f(p), m_pbuf(p->rdbuf()), line_no(1), line_pos(0)
374 {
375 init();
376 };
std::streambuf * m_pbuf
Definition xml.hpp:435
std::shared_ptr< std::istream > f
Definition xml.hpp:434

◆ ~OksXmlInputStream()

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

Definition at line 378 of file xml.hpp.

378 {
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 }
static OksXmlTokenPool s_tokens_pool
Definition xml.hpp:446

◆ 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 619 of file xml.cpp.

620{
621 throw std::runtime_error("unexpected end of file");
622}

◆ __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 625 of file xml.cpp.

626{
627 std::ostringstream text;
628 text << "function " << f << "(\'" << what << "\') has failed";
629 if(*where) text << " on unrecognized characters \'" << where << "\'";
630 if(errno) text << " with code " << errno << ", reason = \'" << oks::strerror(errno) << '\'';
631 throw oks::BadFileData(text.str(), line_no, line_pos);
632}
const std::string strerror(int error)
Convert C error number to string.
Definition kernel.cpp:119

◆ cvt_char()

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

Read encoded symbol (&xxx;)

Definition at line 590 of file xml.cpp.

591{
592 get_token(';', *m_cvt_char);
593
594 if(oks::cmp_str2n(m_cvt_char->m_buf, oks::xml::left_angle_bracket + 1)) return '<';
595 else if(oks::cmp_str2n(m_cvt_char->m_buf, oks::xml::right_angle_bracket + 1)) return '>';
596 else if(oks::cmp_str3n(m_cvt_char->m_buf, oks::xml::ampersand + 1)) return '&';
597 else if(oks::cmp_str3n(m_cvt_char->m_buf, oks::xml::carriage_return + 1)) return '\r';
598 else if(oks::cmp_str3n(m_cvt_char->m_buf, oks::xml::new_line + 1)) return '\n';
599 else if(oks::cmp_str3n(m_cvt_char->m_buf, oks::xml::tabulation + 1)) return '\t';
600 else if(oks::cmp_str4n(m_cvt_char->m_buf, oks::xml::single_quote + 1)) return '\'';
601 else if(oks::cmp_str4n(m_cvt_char->m_buf, oks::xml::double_quote + 1)) return '\"';
602 else {
603 std::string text("bad symbol \'&");
604 text += m_cvt_char->m_buf;
605 text += '\'';
606 throw std::runtime_error(text.c_str());
607 }
608}
size_t get_token(const char s1, OksXmlToken &token)
Definition xml.cpp:428
const char left_angle_bracket[]
Definition xml.cpp:31
const char new_line[]
Definition xml.cpp:35
const char tabulation[]
Definition xml.cpp:36
const char single_quote[]
Definition xml.cpp:37
const char ampersand[]
Definition xml.cpp:33
const char right_angle_bracket[]
Definition xml.cpp:32
const char carriage_return[]
Definition xml.cpp:34
const char double_quote[]
Definition xml.cpp:38
bool cmp_str2n(const char *s1, const char s2[2])
Definition cstring.hpp:22
bool cmp_str3n(const char *s1, const char s2[3])
Definition cstring.hpp:30
bool cmp_str4n(const char *s1, const char s2[4])
Definition cstring.hpp:38

◆ eof()

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

Definition at line 403 of file xml.hpp.

403{ return f->eof(); }

◆ error_msg()

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

Definition at line 612 of file xml.cpp.

613{
614 return Oks::error_msg(msg)
615 << "(line " << line_no << ", char " << line_pos << ")\n";
616}
static std::ostream & error_msg(const char *)
Definition kernel.cpp:561

◆ get()

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

Definition at line 493 of file xml.hpp.

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}

◆ get_first_non_empty()

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

Definition at line 511 of file xml.hpp.

512{
513 while(true) {
514 char c(get());
515 if(c == ' ' || c == '\n' || c == '\r' || c == '\t') continue;
516 else return c;
517 }
518}

◆ get_line_no()

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

Definition at line 415 of file xml.hpp.

415{ return line_no; }

◆ get_line_pos()

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

Definition at line 416 of file xml.hpp.

416{ 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 407 of file xml.hpp.

407 {
408 return f->tellg();
409 }

◆ get_quoted()

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

Definition at line 295 of file xml.cpp.

296{
297 size_t pos(0);
298
299 try {
300 char c = get_first_non_empty();
301
302 if( __builtin_expect((c != '\"'), 0) ) {
304 }
305
306 while(true) {
307 c = get();
308
309 if( __builtin_expect((c == '\"'), 0) ) {
310 m_v1->m_buf[pos] = 0;
311 return pos;
312 }
313 else if( __builtin_expect((c == '&'), 0) ) {
314 c = cvt_char();
315 }
316
317 m_v1->realloc(pos);
318 m_v1->m_buf[pos++] = c;
319 }
320
321 throw std::runtime_error("cannot find closing quote character");
322 }
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:441
static void __throw_runtime_error_unexpected_symbol(const char expected, const char read)
Definition xml.cpp:105

◆ 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 == '<') count++;
372 else if(c2 == '>') count--;
373
374 m_v1->realloc(pos);
375 m_v1->m_buf[pos++] = c2;
376
377 if(pos > 3) {
378 if(__is_comment(m_v1->m_buf + pos - 4)) {
379 char buf[3] = {0, 0, 0};
380 unsigned long start_comment_line_no = line_no;
381 try {
382 while(true) {
383 buf[0] = buf[1];
384 buf[1] = buf[2];
385 buf[2] = get();
386
387 if(oks::cmp_str3n(buf, "-->")) {
388 break; // end of comment = '-->'
389 }
390 }
391 }
392 catch(std::exception& failure) {
393 std::ostringstream what;
394 what << "Cannot find end of comment started on line " << start_comment_line_no << ": " << failure.what();
395 throw oks::BadFileData(what.str(), line_no, line_pos);
396 }
397
398 pos -= 4;
399 count--;
400 }
401 }
402
403 if(count == 0) {
404 m_v1->m_buf[pos] = '\0';
405 break;
406 }
407 }
408
409 if(pos > 0) {
410 if(count != 0) { throw std::runtime_error("unbalanced tags"); }
411 else { return m_v1->m_buf; }
412 }
413 }
414 }
415 catch(std::exception& ex) {
416 m_v1->m_buf[pos] = '\0';
417 throw oks::BadFileData(std::string(ex.what()) + " when read xml tag started at", start_line_no, start_line_pos);
418 }
419}
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:111
bool __is_comment(const char *s)
Definition xml.cpp:332

◆ get_tag_start()

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

Definition at line 490 of file xml.cpp.

491{
492 try {
493 while(true) {
494 char c = get_first_non_empty();
495
496 if( __builtin_expect((c != '<'), 0) ) {
498 }
499
500 get_token5(' ', '>', '\t', '\n', '\r', *m_v1);
501 TLOG_DEBUG(8) << "read tag \'" << m_v1->m_buf << '\'';
502
503 if( __builtin_expect((oks::cmp_str3n(m_v1->m_buf, "!--")), 0) ) {
504 char buf[3] = {0, 0, 0};
505 unsigned long start_comment_line_no = line_no;
506 auto tag_len = strlen(m_v1->m_buf);
507 if(tag_len < 5 || !oks::cmp_str2n(m_v1->m_buf + tag_len- 2, "--")) // special case for comments without empty symbols like <!--foo-->
508 try {
509 while(true) {
510 buf[0] = buf[1];
511 buf[1] = buf[2];
512 c = buf[2] = get();
513 if(oks::cmp_str3n(buf, "-->")) {
514 break; // end of comment = '-->'
515 }
516 }
517 }
518 catch(std::exception& failure) {
519 std::ostringstream what;
520 what << "Cannot find end of comment started on line " << start_comment_line_no << ": " << failure.what();
521 throw oks::BadFileData(what.str(), line_no, line_pos);
522 }
523 }
524
525 if( __builtin_expect((c != '>'), 1) ) return m_v1->m_buf;
526 }
527 }
528 catch(std::exception& ex) {
529 throw oks::BadFileData(std::string("Failed to read start-of-tag: ") + ex.what(), line_no, line_pos);
530 }
531
532 return 0; // never reach this line
533}
void get_token5(const char s1, const char s2, const char s3, const char s4, const char s5, OksXmlToken &token)
Definition xml.cpp:470
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112

◆ get_token()

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

Read token (one separator)

Definition at line 428 of file xml.cpp.

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

◆ 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 448 of file xml.cpp.

449{
450 size_t pos(1);
451 token.m_buf[0] = __fs;
452 if(__fs == __s1 || __fs == __s2) { token.m_buf[1] = '\0'; return 1; }
453
454 while(true) {
455 last_read_c = get();
456
457 if(last_read_c == __s1 || last_read_c == __s2) { token.m_buf[pos] = '\0'; return pos; }
458 else if( __builtin_expect((last_read_c == '&'), 0) ) last_read_c = cvt_char();
459
460 token.realloc(pos);
461 token.m_buf[pos++] = last_read_c;
462 }
463
464 token.m_buf[pos] = 0;
465
466 throw std::runtime_error("cannot find closing separator while read token");
467}

◆ 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 470 of file xml.cpp.

471{
472 size_t pos(0);
473
474 while(true) {
475 last_read_c = get();
476
477 if(last_read_c == __s1 || last_read_c == __s2 || last_read_c == __s3 || last_read_c == __s4 || last_read_c == __s5) { token.m_buf[pos] = '\0'; return; }
478 else if( __builtin_expect((last_read_c == '&'), 0) ) last_read_c = cvt_char();
479
480 token.realloc(pos);
481 token.m_buf[pos++] = last_read_c;
482 }
483
484 token.m_buf[pos] = 0;
485
486 throw std::runtime_error("cannot find closing separator while read token");
487}

◆ get_value()

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

Definition at line 421 of file xml.hpp.

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 }

◆ 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 418 of file xml.hpp.

418{return *m_v1;}

◆ good()

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

Definition at line 402 of file xml.hpp.

402{ return f->good(); }

◆ init()

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

Definition at line 454 of file xml.hpp.

454 {
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 }

◆ operator=()

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

◆ restore_position()

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

Definition at line 406 of file xml.hpp.

unsigned long m_line_pos_sav
Definition xml.hpp:444
unsigned long m_line_no_sav
Definition xml.hpp:443

◆ seek_position()

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

Definition at line 411 of file xml.hpp.

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

◆ store_position()

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

Definition at line 405 of file xml.hpp.

405{ pos = f->tellg(); m_line_no_sav = line_no; m_line_pos_sav = line_pos; }

◆ throw_unexpected_attribute()

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

Definition at line 289 of file xml.cpp.

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

◆ throw_unexpected_tag()

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

Definition at line 266 of file xml.cpp.

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

◆ OksData

friend struct OksData
friend

Definition at line 367 of file xml.hpp.

◆ OksObject

friend class OksObject
friend

Definition at line 368 of file xml.hpp.

◆ OksXmlAttribute

friend struct OksXmlAttribute
friend

Definition at line 366 of file xml.hpp.

Member Data Documentation

◆ f

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

Definition at line 434 of file xml.hpp.

◆ last_read_c

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

Definition at line 439 of file xml.hpp.

◆ line_no

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

Definition at line 436 of file xml.hpp.

◆ line_pos

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

Definition at line 437 of file xml.hpp.

◆ m_cvt_char

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

Definition at line 448 of file xml.hpp.

◆ m_line_no_sav

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

Definition at line 443 of file xml.hpp.

◆ m_line_pos_sav

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

Definition at line 444 of file xml.hpp.

◆ m_pbuf

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

Definition at line 435 of file xml.hpp.

◆ m_v1

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

Definition at line 449 of file xml.hpp.

◆ m_v2

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

Definition at line 450 of file xml.hpp.

◆ m_v3

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

Definition at line 451 of file xml.hpp.

◆ pos

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

Definition at line 441 of file xml.hpp.

◆ s_tokens_pool

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

Definition at line 446 of file xml.hpp.


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