parser_configuration.cpp
Go to the documentation of this file.
1 #include "parser_configuration.h"
2 #include <boost/spirit/include/qi.hpp>
3 #include <boost/algorithm/string.hpp>
4 
5 namespace PJ
6 {
7 
8 void RosParserConfig::xmlSaveState(QDomDocument &doc, QDomElement &parent_elem) const
9 {
10  QDomElement stamp_elem = doc.createElement("use_header_stamp");
11  stamp_elem.setAttribute("value", use_header_stamp ? "true" : "false");
12  parent_elem.appendChild(stamp_elem);
13 
14  QDomElement discard_elem = doc.createElement("discard_large_arrays");
15  discard_elem.setAttribute("value", discard_large_arrays ? "true" : "false");
16  parent_elem.appendChild(discard_elem);
17 
18  QDomElement max_elem = doc.createElement("max_array_size");
19  max_elem.setAttribute("value", QString::number(max_array_size));
20  parent_elem.appendChild(max_elem);
21 
22  QDomElement bool_elem = doc.createElement("boolean_strings_to_number");
23  bool_elem.setAttribute("value", boolean_strings_to_number ? "true" : "false");
24  parent_elem.appendChild(bool_elem);
25 
26  QDomElement suffix_elem = doc.createElement("remove_suffix_from_strings");
27  suffix_elem.setAttribute("value", remove_suffix_from_strings ? "true" : "false");
28  parent_elem.appendChild(suffix_elem);
29 
30  QDomElement topics_elem = doc.createElement("selected_topics");
31  topics_elem.setAttribute("value", topics.join(';'));
32  parent_elem.appendChild(topics_elem);
33 }
34 
35 void RosParserConfig::xmlLoadState(const QDomElement &parent_element)
36 {
37  QDomElement stamp_elem = parent_element.firstChildElement("use_header_stamp");
38  use_header_stamp = (stamp_elem.attribute("value") == "true");
39 
40  QDomElement discard_elem = parent_element.firstChildElement("discard_large_arrays");
41  discard_large_arrays = (discard_elem.attribute("value") == "true");
42 
43  QDomElement max_elem = parent_element.firstChildElement("max_array_size");
44  max_array_size = max_elem.attribute("value").toInt();
45 
46  QDomElement bool_elem = parent_element.firstChildElement("boolean_strings_to_number");
47  boolean_strings_to_number = (bool_elem.attribute("value") == "true");
48 
49  QDomElement suffix_elem = parent_element.firstChildElement("remove_suffix_from_strings");
50  remove_suffix_from_strings = (suffix_elem.attribute("value")== "true");
51 
52  QDomElement topics_elem = parent_element.firstChildElement("selected_topics");
53  if(!topics_elem.isNull()) {
54  topics = topics_elem.attribute("value").split(';');
55  }
56 }
57 
58 void RosParserConfig::saveToSettings(QSettings &settings, QString prefix) const
59 {
60  settings.setValue(prefix + "/default_topics", topics);
61  settings.setValue(prefix + "/use_header_stamp", use_header_stamp);
62  settings.setValue(prefix + "/max_array_size", (int)max_array_size);
63  settings.setValue(prefix + "/discard_large_arrays", discard_large_arrays);
64  settings.setValue(prefix + "/boolean_strings_to_number", boolean_strings_to_number);
65  settings.setValue(prefix + "/remove_suffix_from_strings", remove_suffix_from_strings);
66 }
67 
68 void RosParserConfig::loadFromSettings(const QSettings &settings, QString prefix)
69 {
70  topics = settings.value(prefix + "/default_topics", false).toStringList();
71  use_header_stamp = settings.value(prefix + "/use_header_stamp", false).toBool();
72  max_array_size = settings.value(prefix + "/max_array_size", 100).toInt();
73  discard_large_arrays = settings.value(prefix + "/discard_large_arrays", true).toBool();
74  boolean_strings_to_number = settings.value(prefix + "/boolean_strings_to_number", true).toBool();
75  remove_suffix_from_strings = settings.value(prefix + "/remove_suffix_from_strings", true).toBool();
76 }
77 
78 void CompositeParser::addParser(const std::string& topic_name, std::shared_ptr<PJ::MessageParser> parser)
79 {
80  parser->setLargeArraysPolicy(!_config.discard_large_arrays, _config.max_array_size);
81  parser->enableEmbeddedTimestamp(_config.use_header_stamp);
82  _parsers.insert( {topic_name, parser} );
83 }
84 
86 {
87  return _config;
88 }
89 
91 {
92  _config = config;
93  // we don't need this information.
94  _config.topics.clear();
95  for(auto& [name, parser]: _parsers)
96  {
97  parser->setLargeArraysPolicy(!config.discard_large_arrays, config.max_array_size);
98  parser->enableEmbeddedTimestamp(config.use_header_stamp);
99  }
100 }
101 
102 bool CompositeParser::parseMessage(const std::string& topic_name, MessageRef serialized_msg, double& timestamp)
103 {
104  auto it = _parsers.find(topic_name);
105  if (it == _parsers.end())
106  {
107  return false;
108  }
109  auto& parser = it->second;
110  parser->parseMessage(serialized_msg, timestamp);
111  return true;
112 }
113 
114 
115 bool ParseDouble(const std::string &str,
116  double &value,
117  bool remover_suffix,
118  bool parse_boolean)
119 {
120  bool parsed = boost::spirit::qi::parse(str.data(), str.data() + str.size(),
121  boost::spirit::qi::double_, value);
122 
123  if(!parsed && remover_suffix)
124  {
125  int pos = 0;
126  while(pos < str.size())
127  {
128  char c = str[pos];
129  if( !std::isdigit(c) && c!= '-' && c!= '+' && c!='.')
130  {
131  break;
132  }
133  pos++;
134  }
135  if( pos < str.size() )
136  {
137  parsed = boost::spirit::qi::parse(str.data(), str.data() + pos,
138  boost::spirit::qi::double_, value);
139  }
140  }
141 
142  if(!parsed && parse_boolean && str.size() >=4 && str.size() <=5)
143  {
144  std::string temp = str;
145  boost::algorithm::to_lower(temp);
146  if( temp == "true" )
147  {
148  parsed = true;
149  value = 1;
150  }
151  else if( temp == "false" )
152  {
153  parsed = true;
154  value = 0;
155  }
156  }
157 
158  return parsed;
159 }
160 
161 }
PJ::RosParserConfig::use_header_stamp
bool use_header_stamp
Definition: parser_configuration.h:18
PJ::RosParserConfig::saveToSettings
void saveToSettings(QSettings &setting, QString prefix) const
Definition: parser_configuration.cpp:58
value
PJ::CompositeParser::setConfig
void setConfig(const RosParserConfig &config)
Definition: parser_configuration.cpp:90
PJ::RosParserConfig
Definition: parser_configuration.h:14
parser
parser
parser_configuration.h
PJ::CompositeParser::_config
RosParserConfig _config
Definition: parser_configuration.h:51
PJ::RosParserConfig::loadFromSettings
void loadFromSettings(const QSettings &settings, QString prefix)
Definition: parser_configuration.cpp:68
PJ::RosParserConfig::xmlLoadState
void xmlLoadState(const QDomElement &parent_element)
Definition: parser_configuration.cpp:35
PJ::RosParserConfig::xmlSaveState
void xmlSaveState(QDomDocument &doc, QDomElement &plugin_elem) const
Definition: parser_configuration.cpp:8
it
iterator it
PJ::RosParserConfig::discard_large_arrays
bool discard_large_arrays
Definition: parser_configuration.h:19
PJ::RosParserConfig::boolean_strings_to_number
bool boolean_strings_to_number
Definition: parser_configuration.h:20
PJ::CompositeParser::parseMessage
bool parseMessage(const std::string &topic_name, MessageRef serialized_msg, double &timestamp)
Definition: parser_configuration.cpp:102
PJ::CompositeParser::_parsers
std::unordered_map< std::string, std::shared_ptr< PJ::MessageParser > > _parsers
Definition: parser_configuration.h:50
PJ::RosParserConfig::max_array_size
unsigned max_array_size
Definition: parser_configuration.h:17
PJ::CompositeParser::getConfig
const RosParserConfig & getConfig()
Definition: parser_configuration.cpp:85
PJ::MessageRef
PJ::ParseDouble
bool ParseDouble(const std::string &str, double &value, bool remover_suffix, bool parse_boolean)
Definition: parser_configuration.cpp:115
PJ
arg_id_kind::name
@ name
PJ::RosParserConfig::topics
QStringList topics
Definition: parser_configuration.h:16
PJ::RosParserConfig::remove_suffix_from_strings
bool remove_suffix_from_strings
Definition: parser_configuration.h:21
PJ::CompositeParser::addParser
void addParser(const std::string &topic_name, std::shared_ptr< PJ::MessageParser > parser)
Definition: parser_configuration.cpp:78


plotjuggler_ros
Author(s): Davide Faconti
autogenerated on Wed Feb 21 2024 03:22:56