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 <string>
27 #include <vector>
28 #include <utility>
29 #include <ndn-cpp/common.hpp>
30 
31 namespace ndn {
32 
46 {
47 public:
48  BoostInfoTree(const std::string& value = "", BoostInfoTree* parent = 0)
49  : value_(value), parent_(parent), lastChild_(0)
50  {
51  }
52 
58  void
60  (const std::string& treeName, ptr_lib::shared_ptr<BoostInfoTree> newTree);
61 
68  const BoostInfoTree&
69  createSubtree(const std::string& treeName, const std::string& value = "");
70 
76  std::vector<const BoostInfoTree*>
77  operator [] (const std::string& key) const;
78 
85  const std::string*
86  getFirstValue(const std::string& key) const
87  {
88  std::vector<const BoostInfoTree*> list = (*this)[key];
89  if (list.size() >= 1)
90  return &list[0]->value_;
91  else
92  return 0;
93  }
94 
95  const std::string&
96  getValue() const { return value_; }
97 
98  BoostInfoTree*
99  getParent() { return parent_; }
100 
101  BoostInfoTree*
102  getLastChild() { return lastChild_; }
103 
104  std::string
105  prettyPrint(int indentLevel = 1) const;
106 
107 private:
114  std::vector<ptr_lib::shared_ptr<BoostInfoTree> >*
115  find(const std::string& treeName);
116 
117  static std::vector<std::string>
118  split(const std::string &input, char separator);
119 
120  // We can't use a map for subtrees_ because we want the keys to be in order.
121  std::vector<std::pair<std::string, std::vector<ptr_lib::shared_ptr<BoostInfoTree> > > > subtrees_;
122  std::string value_;
123  BoostInfoTree* parent_;
124  BoostInfoTree* lastChild_;
125 };
126 
127 inline std::ostream&
128 operator << (std::ostream& os, const BoostInfoTree& tree)
129 {
130  os << tree.prettyPrint();
131  return os;
132 }
133 
139 {
140 public:
142  : root_(new BoostInfoTree())
143  {
144  }
145 
151  const BoostInfoTree&
152  read(const std::string& fileName);
153 
154  // TODO: Implement readPropertyList.
155 
160  void
161  write(const std::string& fileName) const;
162 
167  const BoostInfoTree&
168  getRoot() const { return *root_; }
169 
170 private:
180  static void
181  shlex_split(const std::string& s, std::vector<std::string>& result);
182 
190  read(const std::string& fileName, BoostInfoTree* ctx);
191 
196  parseLine(const std::string& line, BoostInfoTree* context);
197 
198  ptr_lib::shared_ptr<BoostInfoTree> root_;
199 };
200 
201 }
202 
203 #endif
Copyright (C) 2013-2015 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:100
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:84
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:226
BoostInfoTree is provided for compatibility with the Boost INFO property list format used in ndn-cxx...
Definition: boost-info-parser.hpp:45
const BoostInfoTree & getRoot() const
Get the root tree of this parser.
Definition: boost-info-parser.hpp:168
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:108
A BoostInfoParser reads files in Boost's INFO format and constructs a BoostInfoTree.
Definition: boost-info-parser.hpp:138
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:86