boost-info-parser.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_BOOST_INFO_PARSER_HPP
24 #define NDN_BOOST_INFO_PARSER_HPP
25 
26 #include <istream>
27 #include <string>
28 #include <vector>
29 #include <utility>
30 #include <ndn-cpp/common.hpp>
31 
32 namespace ndn {
33 
47 {
48 public:
49  BoostInfoTree(const std::string& value = "", BoostInfoTree* parent = 0)
50  : value_(value), parent_(parent), lastChild_(0)
51  {
52  }
53 
59  void
61  (const std::string& treeName, ptr_lib::shared_ptr<BoostInfoTree> newTree);
62 
69  const BoostInfoTree&
70  createSubtree(const std::string& treeName, const std::string& value = "");
71 
77  std::vector<const BoostInfoTree*>
78  operator [] (const std::string& key) const;
79 
86  const std::string*
87  getFirstValue(const std::string& key) const
88  {
89  std::vector<const BoostInfoTree*> list = (*this)[key];
90  if (list.size() >= 1)
91  return &list[0]->value_;
92  else
93  return 0;
94  }
95 
96  const std::string&
97  getValue() const { return value_; }
98 
99  BoostInfoTree*
100  getParent() { return parent_; }
101 
102  BoostInfoTree*
103  getLastChild() { return lastChild_; }
104 
105  std::string
106  prettyPrint(int indentLevel = 1) const;
107 
108 private:
115  std::vector<ptr_lib::shared_ptr<BoostInfoTree> >*
116  find(const std::string& treeName);
117 
118  static std::vector<std::string>
119  split(const std::string &input, char separator);
120 
121  // We can't use a map for subtrees_ because we want the keys to be in order.
122  std::vector<std::pair<std::string, std::vector<ptr_lib::shared_ptr<BoostInfoTree> > > > subtrees_;
123  std::string value_;
124  BoostInfoTree* parent_;
125  BoostInfoTree* lastChild_;
126 };
127 
128 inline std::ostream&
129 operator << (std::ostream& os, const BoostInfoTree& tree)
130 {
131  os << tree.prettyPrint();
132  return os;
133 }
134 
140 {
141 public:
143  : root_(new BoostInfoTree())
144  {
145  }
146 
152  const BoostInfoTree&
153  read(const std::string& fileName);
154 
162  const BoostInfoTree&
163  read(const std::string& input, const std::string& inputName);
164 
165  // TODO: Implement readPropertyList.
166 
171  void
172  write(const std::string& fileName) const;
173 
178  const BoostInfoTree&
179  getRoot() const { return *root_; }
180 
181 private:
191  static void
192  shlex_split(const std::string& s, std::vector<std::string>& result);
193 
201  read(std::istream& stream, BoostInfoTree* ctx);
202 
207  parseLine(const std::string& line, BoostInfoTree* context);
208 
209  ptr_lib::shared_ptr<BoostInfoTree> root_;
210 };
211 
212 }
213 
214 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
const BoostInfoTree & createSubtree(const std::string &treeName, const std::string &value="")
Create a new BoostInfo and insert it as a sub-tree with the given name.
Definition: boost-info-parser.cpp:53
void addSubtree(const std::string &treeName, ptr_lib::shared_ptr< BoostInfoTree > newTree)
Insert a BoostInfoTree as a sub-tree with the given name.
Definition: boost-info-parser.cpp:37
void write(const std::string &fileName) const
Write the root tree of this BoostInfoParser as file in Boost's INFO format.
Definition: boost-info-parser.cpp:188
BoostInfoTree is provided for compatibility with the Boost INFO property list format used in ndn-cxx...
Definition: boost-info-parser.hpp:46
const BoostInfoTree & getRoot() const
Get the root tree of this parser.
Definition: boost-info-parser.hpp:179
const BoostInfoTree & read(const std::string &fileName)
Add the contents of the file to the root BoostInfoTree.
std::vector< const BoostInfoTree * > operator[](const std::string &key) const
Look up using the key and return a list of the subtrees.
Definition: boost-info-parser.cpp:61
A BoostInfoParser reads files in Boost's INFO format and constructs a BoostInfoTree.
Definition: boost-info-parser.hpp:139
const std::string * getFirstValue(const std::string &key) const
Look up using the key and return string value of the first subtree.
Definition: boost-info-parser.hpp:87