SchemeParser.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
21 //----------------------------------------------------------------------
22 #include "SchemeParser.h"
23 
24 #include <boost/foreach.hpp>
25 
26 namespace icl_core
27 {
28 
29 std::ostream &operator<<(std::ostream &stream, const Scheme &scheme)
30 {
31  stream << scheme.scheme_name << scheme.specifier;
32 
33  bool first = true;
34  BOOST_FOREACH(Query query, scheme.queries)
35  {
36  if ( first )
37  {
38  stream << "?";
39  first = false;
40  }
41  else
42  {
43  stream << "&";
44  }
45  stream << query.name << "=" << query.value;
46  }
47 
48  if (scheme.anchor.size() > 0)
49  {
50  stream << "#" << scheme.anchor;
51  }
52 
53  return stream;
54 }
55 
56 
57 void SchemeFunction::operator () (char const* str, char const* end) const
58 {
59  std::string name(str, end);
60  for (size_t i = 0; i < name.size(); ++i)
61  {
62  name[i] = tolower(name[i]);
63  }
64  if (name == "file://")
65  {
67  }
68  else if (name == "http://")
69  {
71  }
72  else if (name == "camera://")
73  {
75  }
76  else if (name == "gps://")
77  {
79  }
80  else
81  {
83  }
85 }
86 
87 void SpecifierFunction::operator () (char const* str, char const* end) const
88 {
89  std::string name(str, end);
91 }
92 
93 void AnchorFunction::operator () (char const* str, char const* end) const
94 {
95  std::string name(str, end);
96  m_scheme_handler->anchor = name;
97 }
98 
99 void QueryKeyFunction::operator () (char const* str, char const* end) const
100 {
101  std::string name(str, end);
102  Query query;
103  query.name = name;
104  m_queries->push_back(query);
105 }
106 
107 void QueryValueFunction::operator () (char const* str, char const* end) const
108 {
109  std::string value(str, end);
110  if (m_queries->empty())
111  {
112  Query query;
113  query.name = "";
114  m_queries->push_back(query);
115  }
116  QueryList::reverse_iterator rit = m_queries->rbegin();
117  assert(rit != m_queries->rend()); // Just to please Klocwork.
118  rit->value = value;
119 }
120 
122 {
123  m_scheme.scheme_type = icl_core::OtherScheme;
124 }
125 
127 {
128 }
129 
131 {
132  return this->parseScheme(str, m_scheme, m_info);
133 }
134 
135 const BOOST_SPIRIT_NAMESPACE::parse_info<>& SchemeParser::getParseInfo() const
136 {
137  return m_info;
138 }
139 
141 {
142  return m_scheme;
143 }
144 
145 bool SchemeParser::parseScheme(const String &str, Scheme &scheme_handler,
146  BOOST_SPIRIT_NAMESPACE::parse_info<> &info)
147 {
148  using BOOST_SPIRIT_NAMESPACE::rule;
149  using BOOST_SPIRIT_NAMESPACE::alnum_p;
150  using BOOST_SPIRIT_NAMESPACE::ch_p;
151  using BOOST_SPIRIT_NAMESPACE::space_p;
152  using BOOST_SPIRIT_NAMESPACE::str_p;
153  using BOOST_SPIRIT_NAMESPACE::alpha_p;
154  using BOOST_SPIRIT_NAMESPACE::anychar_p;
155 
156  SchemeFunction addScheme;
157  addScheme.m_scheme_handler = &scheme_handler;
158 
159  SpecifierFunction addSpecifier;
160  addSpecifier.m_scheme_handler = &scheme_handler;
161 
162  AnchorFunction addAnchor;
163  addAnchor.m_scheme_handler = &scheme_handler;
164 
165  QueryKeyFunction addName;
166  addName.m_queries = &scheme_handler.queries;
167 
168  QueryValueFunction addValue;
169  addValue.m_queries = &scheme_handler.queries;
170 
171  // extended word rule, alphanumeric charactes, separated by _, -, ., or whitespace
172  rule<> extword_p = +alnum_p >> *((ch_p('_') | ch_p('-') | ch_p('.') | space_p) >> +alnum_p);
173  rule<> anchor_word = +alnum_p >> !(ch_p('-') >> +alnum_p);
174 
175  // special scheme characters
176  rule<> scheme_ch = str_p("://");
177  rule<> anchor_ch = ch_p('#');
178  rule<> querystart_ch = ch_p('?');
179  rule<> querydelim_ch = ch_p('&');
180 
181  // scheme, path and file rules
182  rule<> scheme_p = +alpha_p >> *((ch_p('+')) >> +alpha_p) >> scheme_ch; // file+something://
183  rule<> specifier_p = +(anychar_p - querystart_ch - anchor_ch); /* almost everything between xyz://
184  * and ?query=value or #anchor */
185  rule<> anchor_p = anchor_ch >> anchor_word[addAnchor];
186 
187  // query rules
188  rule<> query_key = +alnum_p >> *(alnum_p | (ch_p('_') >> alnum_p));
189  rule<> query_value = +(anychar_p - (querystart_ch | querydelim_ch | space_p));
190  rule<> query_pair = query_key[addName] >> ch_p('=') >> query_value[addValue];
191  rule<> query_p = querystart_ch >> query_pair >> *(querydelim_ch >> query_pair);
192 
193  // scheme rule
194  rule<> scheme_rule = !scheme_p[addScheme] >> specifier_p[addSpecifier] >> !anchor_p >> !query_p;
195 
196  scheme_handler.queries.clear();
197  info = parse(str.c_str(), scheme_rule);
198 
199  return info.full;
200 }
201 
203 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
204 
205  bool SchemeParser::ParseScheme(const String &str)
206  {
207  return parseScheme(str);
208  }
209 
210  const BOOST_SPIRIT_NAMESPACE::parse_info<> &SchemeParser::GetParseInfo() const
211  {
212  return getParseInfo();
213  }
214 
215  const icl_core::Scheme &SchemeParser::GetSchemeResult() const
216  {
217  return getSchemeResult();
218  }
219 
220  bool SchemeParser::ParseScheme(const String &str, Scheme &scheme_handler, BOOST_SPIRIT_NAMESPACE::parse_info<> &info)
221  {
222  return parseScheme(str, scheme_handler, info);
223  }
224 
225 #endif
226 
227 }
std::ostream & operator<<(std::ostream &os, StampedBase const &stamped)
Definition: DataHeader.cpp:27
file:///path/file?key=value
Definition: SchemeParser.h:65
gps://type (gpsd/oxfs/...)
Definition: SchemeParser.h:68
bool parseScheme(const String &str)
camera://source?key=value
Definition: SchemeParser.h:67
const BOOST_SPIRIT_NAMESPACE::parse_info & getParseInfo() const
virtual void operator()(char const *str, char const *end) const
~SchemeParser()
Destructor.
virtual void operator()(char const *str, char const *end) const
std::string String
Definition: BaseTypes.h:43
http://domain.org/path/to/site.ext?key=value
Definition: SchemeParser.h:66
SchemeType scheme_type
Definition: SchemeParser.h:74
QueryList queries
Definition: SchemeParser.h:78
virtual void operator()(char const *str, char const *end) const
const icl_core::Scheme & getSchemeResult() const
Returns the parser result.
virtual void operator()(char const *str, char const *end) const
Contains icl_core::SchemeParser.
SchemeParser()
Constructor.
virtual void operator()(char const *str, char const *end) const


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58