Jpp  master_rocky
the software that should make you happy
configure.hh
Go to the documentation of this file.
1 #ifndef DATAQUEUE_CONFIGURE_HH
2 #define DATAQUEUE_CONFIGURE_HH
3 
4 #include <vector>
5 #include <stdexcept>
6 
7 #include <boost/foreach.hpp>
8 #include <boost/tokenizer.hpp>
9 #include <boost/lexical_cast.hpp>
10 #include <boost/property_tree/ptree.hpp>
11 #include <boost/algorithm/string/trim.hpp>
12 
13 /**
14  * \author cpellegrino
15  */
16 
17 namespace detail
18 {
19  static const boost::char_separator<char> args_sep(";");
20  static const boost::char_separator<char> fields_sep("=");
21  static const boost::char_separator<char> white_spaces("\t ");
22 
23  inline
24  boost::property_tree::ptree parse(std::string str)
25  {
27  boost::property_tree::ptree pt;
28  boost::tokenizer<boost::char_separator<char> > tok(str, args_sep);
29 
30  BOOST_FOREACH(std::string s, tok)
31  {
33  boost::tokenizer<boost::char_separator<char> > fields(s, fields_sep);
34  typedef boost::tokenizer<boost::char_separator<char> >::iterator Iter;
35 
36  Iter it = fields.begin();
37 
38  std::string varname = *it;
39  boost::algorithm::trim(varname);
40 
41  ++it;
42  if (it != fields.end()) {
43  std::string val = *it;
45 
46  pt.put(varname, val);
47  ++it;
48  } else {
49  throw std::runtime_error("Parsing error: malformed input buffer.");
50  }
51 
52  if (it != fields.end()) {
53  throw std::runtime_error("Parsing error: malformed input buffer.");
54  }
55  }
56  return pt;
57  }
58 
59  template<typename T>
60  std::vector<T> vectorize(const std::string& str)
61  {
62  std::vector<T> vec;
63 
64  boost::tokenizer<boost::char_separator<char> > elements(str, white_spaces);
65 
66  BOOST_FOREACH(std::string elem, elements)
67  {
68  vec.push_back(boost::lexical_cast<T>(elem));
69  }
70 
71  return vec;
72  }
73 
74 } // ns detail
75 
76 #endif // DATAQUEUE_CONFIGURE_HH
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
static const boost::char_separator< char > fields_sep("=")
static const boost::char_separator< char > args_sep(";")
std::vector< T > vectorize(const std::string &str)
Definition: configure.hh:60
static const boost::char_separator< char > white_spaces("\t ")
boost::property_tree::ptree parse(std::string str)
Definition: configure.hh:24