NSVD Reader  0.0.1
boost.hpp
Go to the documentation of this file.
1 
7 /*
8  * These codes are licensed under CC0.
9  * http://creativecommons.org/publicdomain/zero/1.0/
10  */
11 #ifndef __NODAMUSHI_SVD_BOOST_HPP__
12 #define __NODAMUSHI_SVD_BOOST_HPP__
13 
14 # include <string>
15 # include <istream>
16 # include <boost/property_tree/ptree.hpp>
17 # include <boost/property_tree/xml_parser.hpp>
18 # include <boost/algorithm/string/trim.hpp>
20 
21 namespace nodamushi{
22 namespace svd{
23 
39 {
40  using ptree = boost::property_tree::ptree;
41  using itr = ptree::iterator;
42 
43  boost_svd_reader()=default;
44 
46  tree(t.get_child("")),name("root"),value(""),
47  it(tree.begin()),end(tree.end()),attr(false),_ok(true)
48  {
49  skip_comment();
50  }
51 
53  tree(t.get_child("")),name(n),value(v),
54  it(tree.begin()),end(tree.end()),attr(a),_ok(true)
55  {
56  boost::algorithm::trim(value);
57  skip_comment();
58  }
66  boost_svd_reader(const std::string& file_name,bool throw_error=false):
67  tree(),name(""),value(""),
68  it(),end(it),attr(false),_ok(false)
69  {
70  if(file_read_exception::check(file_name,throw_error)){
71  try{
72  boost::property_tree::read_xml(file_name,tree);
73  _ok = true;
74  }catch(boost::property_tree::xml_parser::xml_parser_error& e){
75  if(throw_error){
76  std::string m = e.message();
77  throw xml_parse_exception(file_name,m,e.line());
78  }
79  }
80  if(_ok){
81  it=tree.begin();
82  end=tree.end();
83  skip_comment();
84  }
85  }
86  }
87 
88 
96  boost_svd_reader(std::istream& file,bool throw_error=false):
97  tree(),name(""),value(""),
98  it(),end(it),attr(false),_ok(false)
99  {
100  try{
101  boost::property_tree::xml_parser::read_xml(file,tree);
102  _ok = true;
103  }catch(boost::property_tree::xml_parser::xml_parser_error& e){
104  if(throw_error){
105  std::string m = e.message();
106  throw xml_parse_exception("stream",m,e.line());
107  }
108  }
109  if(_ok){
110  it=tree.begin();
111  end=tree.end();
112  skip_comment();
113  }
114  }
115 
120  bool ok()const noexcept{return _ok;}
121 
122  //-------------------------------------------------------
123  // svd_reader interface
124  //-------------------------------------------------------
126  bool is_attribute()const noexcept{return attr;}
128  string_ref get_name()const noexcept{return name;}
130  string_ref get_value()const noexcept{return value;}
133  {
134  auto v = * it;
135  ++it;
136  skip_comment();
137  auto& name = v.first;
138  auto& subtree = v.second;
139  auto& value = v.second.data();
140  bool a= attr || name == "<xmlattr>";
141  return boost_svd_reader(subtree,name,value,a);
142  }
144  operator bool()const{return it != end;}
145 
146  private:
147  void skip_comment()
148  {
149  while(it != end && it->first == "<xmlcomment>"){
150  ++it;
151  }
152  }
153  ptree tree;
154  std::string name;
155  std::string value;
156  itr it,end;
157  bool attr;
158  bool _ok;
159 };
160 
161 }}// end namespace nodamushi::svd
162 
163 #endif // __NODAMUSHI_SVD_BOOST_HPP__
static bool check(const std::string &file_name, bool throw_exception)
Definition: svd_reader.hpp:54
boost_svd_reader next_child() noexcept
svd_reader interface function
Definition: boost.hpp:132
const std::string & string_ref
Definition: string_type.hpp:44
boost_svd_reader(ptree &t, string_ref n, string_ref v, bool a)
Definition: boost.hpp:52
SVD reader with Boost property tree.
Definition: boost.hpp:38
string_ref get_value() const noexcept
svd_reader interface function
Definition: boost.hpp:130
string_ref get_name() const noexcept
svd_reader interface function
Definition: boost.hpp:128
boost_svd_reader(const std::string &file_name, bool throw_error=false)
constructor with file name
Definition: boost.hpp:66
This class reperesents SVD(xml) element / attribute.
Definition: value.hpp:53
boost_svd_reader(std::istream &file, bool throw_error=false)
constructor with std::istream
Definition: boost.hpp:96
boost::property_tree::ptree ptree
Definition: boost.hpp:40
bool is_attribute() const noexcept
svd_reader interface function
Definition: boost.hpp:126
svd_reader
bool ok() const noexcept
file open / xml parse status
Definition: boost.hpp:120