BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
defaultparser.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/dataloader/defaultparser.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <stdexcept>
18 
19 namespace gui2 {
20 
22 {
26 }
27 
28 //! Parse data representing content of ASCII file.
29 
30 void DefaultParser::process(const std::vector<std::string>& raw_data)
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 }
45 
46 //! Returns total number of lines in raw data.
47 
49 {
50  return m_rawData.size();
51 }
52 
53 //! Returns a pair representing raw line and flag describing parsing results.
54 
55 std::string DefaultParser::getLine(size_t index) const
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 }
62 
63 std::vector<std::string> DefaultParser::parseResults(size_t index) const
64 {
65  auto it = m_parsedData.find(index);
66  return it == m_parsedData.end() ? std::vector<std::string>() : it->second;
67 }
68 
69 std::vector<std::vector<std::string>> DefaultParser::parsedData() const
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 }
79 
80 } // namespace gui2
accept_int_t m_isSkipLineNumber
Definition: defaultparser.h:47
accept_string_t m_isValidLineContent
Definition: defaultparser.h:48
std::vector< std::vector< std::string > > parsedData() const override
Returns 2D vector representing parsed text. Skipped lines are not present.
std::map< size_t, std::vector< std::string > > m_parsedData
Definition: defaultparser.h:52
DefaultParser(const ParserOptions &options)
std::string getLine(size_t index) const override
Returns a pair representing raw line and flag describing parsing results.
std::vector< std::string > m_rawData
correspondence of parsed data to original line index
Definition: defaultparser.h:50
line_splitter_t m_line_splitter
Definition: defaultparser.h:49
void process(const std::vector< std::string > &raw_data) override
Parse data representing content of ASCII file.
std::vector< std::string > parseResults(size_t index) const override
Returns parsed text for given line index.
size_t totalLineCount() const override
Returns total number of lines in raw data.
Defines class CLASS?
Defines class CLASS?
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.
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Parser options to process multi column ASCII files.
std::string m_skip_index_pattern
pattern denoting line to skip (i.e. '1,10-12,42')
std::string m_header_prefix
prefix denoting header line
std::string m_separator
column separator