BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
gui2::DefaultParser Class Reference

Provides basic algorirthm for parsing multi-string data representing content of multi-column ASCII file. More...

Inheritance diagram for gui2::DefaultParser:
[legend]
Collaboration diagram for gui2::DefaultParser:
[legend]

Public Member Functions

 DefaultParser (const ParserOptions &options)
 
std::string getLine (size_t index) const override
 Returns a pair representing raw line and flag describing parsing results. More...
 
std::vector< std::vector< std::string > > parsedData () const override
 Returns 2D vector representing parsed text. Skipped lines are not present. More...
 
std::vector< std::string > parseResults (size_t index) const override
 Returns parsed text for given line index. More...
 
void process (const std::vector< std::string > &raw_data) override
 Parse data representing content of ASCII file. More...
 
size_t totalLineCount () const override
 Returns total number of lines in raw data. More...
 

Private Attributes

accept_int_t m_isSkipLineNumber
 
accept_string_t m_isValidLineContent
 
line_splitter_t m_line_splitter
 
std::map< size_t, std::vector< std::string > > m_parsedData
 
std::vector< std::string > m_rawData
 correspondence of parsed data to original line index More...
 

Detailed Description

Provides basic algorirthm for parsing multi-string data representing content of multi-column ASCII file.

  • Skips empty lines or lines matching the prefix.
  • Skips lines matching given line number pattern.
  • Parse data in columns of basing on given separator value.

Definition at line 32 of file defaultparser.h.

Constructor & Destructor Documentation

◆ DefaultParser()

gui2::DefaultParser::DefaultParser ( const ParserOptions options)

Definition at line 21 of file defaultparser.cpp.

22 {
23  m_isSkipLineNumber = Utils::CreateLineNumberPatternValidator(options.m_skip_index_pattern);
24  m_isValidLineContent = Utils::CreateLinePrefixValidator(options.m_header_prefix);
26 }
accept_int_t m_isSkipLineNumber
Definition: defaultparser.h:47
accept_string_t m_isValidLineContent
Definition: defaultparser.h:48
line_splitter_t m_line_splitter
Definition: defaultparser.h:49
DAREFLCORE_EXPORT accept_int_t CreateLineNumberPatternValidator(const std::string &pattern)
Creates a callback to define if given line number satisfies line number pattern.
DAREFLCORE_EXPORT line_splitter_t CreateSeparatorBasedSplitter(const std::string &separator)
Creates line splitter based on separator.
DAREFLCORE_EXPORT accept_string_t CreateLinePrefixValidator(const std::string &prefix_to_exclude)
Creates a callback to define if given line has a valid content for further parsing.

References gui2::Utils::CreateLineNumberPatternValidator(), gui2::Utils::CreateLinePrefixValidator(), gui2::Utils::CreateSeparatorBasedSplitter(), gui2::ParserOptions::m_header_prefix, m_isSkipLineNumber, m_isValidLineContent, m_line_splitter, gui2::ParserOptions::m_separator, and gui2::ParserOptions::m_skip_index_pattern.

Here is the call graph for this function:

Member Function Documentation

◆ getLine()

std::string gui2::DefaultParser::getLine ( size_t  index) const
overridevirtual

Returns a pair representing raw line and flag describing parsing results.

Implements gui2::ParserInterface.

Definition at line 55 of file defaultparser.cpp.

56 {
57  if (index >= m_rawData.size())
58  throw std::runtime_error("Error in DefaultParser: out of bounds.");
59 
60  return m_rawData[index];
61 }
std::vector< std::string > m_rawData
correspondence of parsed data to original line index
Definition: defaultparser.h:50

References m_rawData.

◆ parsedData()

std::vector< std::vector< std::string > > gui2::DefaultParser::parsedData ( ) const
overridevirtual

Returns 2D vector representing parsed text. Skipped lines are not present.

Implements gui2::ParserInterface.

Definition at line 69 of file defaultparser.cpp.

70 {
71  std::vector<std::vector<std::string>> result;
72  for (size_t index = 0; index < totalLineCount(); ++index) {
73  if (auto it = m_parsedData.find(index); it != m_parsedData.end())
74  result.push_back(it->second);
75  }
76 
77  return result;
78 }
std::map< size_t, std::vector< std::string > > m_parsedData
Definition: defaultparser.h:52
size_t totalLineCount() const override
Returns total number of lines in raw data.

References m_parsedData, and totalLineCount().

Here is the call graph for this function:

◆ parseResults()

std::vector< std::string > gui2::DefaultParser::parseResults ( size_t  index) const
overridevirtual

Returns parsed text for given line index.

If line was skipped during parsing, returns empty vector.

Implements gui2::ParserInterface.

Definition at line 63 of file defaultparser.cpp.

64 {
65  auto it = m_parsedData.find(index);
66  return it == m_parsedData.end() ? std::vector<std::string>() : it->second;
67 }

References m_parsedData.

◆ process()

void gui2::DefaultParser::process ( const std::vector< std::string > &  raw_data)
overridevirtual

Parse data representing content of ASCII file.

Implements gui2::ParserInterface.

Definition at line 30 of file defaultparser.cpp.

31 {
32  m_rawData = raw_data;
33  m_parsedData.clear();
34 
35  int index{0};
36  for (const auto& line : m_rawData) {
37  bool isValidLine = m_isValidLineContent(line) && !m_isSkipLineNumber(index + 1);
38 
39  if (isValidLine)
40  m_parsedData.emplace(index, m_line_splitter(line));
41 
42  ++index;
43  }
44 }

References m_isSkipLineNumber, m_isValidLineContent, m_line_splitter, m_parsedData, and m_rawData.

◆ totalLineCount()

size_t gui2::DefaultParser::totalLineCount ( ) const
overridevirtual

Returns total number of lines in raw data.

Implements gui2::ParserInterface.

Definition at line 48 of file defaultparser.cpp.

49 {
50  return m_rawData.size();
51 }

References m_rawData.

Referenced by parsedData().

Member Data Documentation

◆ m_isSkipLineNumber

accept_int_t gui2::DefaultParser::m_isSkipLineNumber
private

Definition at line 47 of file defaultparser.h.

Referenced by DefaultParser(), and process().

◆ m_isValidLineContent

accept_string_t gui2::DefaultParser::m_isValidLineContent
private

Definition at line 48 of file defaultparser.h.

Referenced by DefaultParser(), and process().

◆ m_line_splitter

line_splitter_t gui2::DefaultParser::m_line_splitter
private

Definition at line 49 of file defaultparser.h.

Referenced by DefaultParser(), and process().

◆ m_parsedData

std::map<size_t, std::vector<std::string> > gui2::DefaultParser::m_parsedData
private

Definition at line 52 of file defaultparser.h.

Referenced by parsedData(), parseResults(), and process().

◆ m_rawData

std::vector<std::string> gui2::DefaultParser::m_rawData
private

correspondence of parsed data to original line index

Definition at line 50 of file defaultparser.h.

Referenced by getLine(), process(), and totalLineCount().


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