Ticket #5588: path_resolver.patch
File path_resolver.patch, 9.5 KB (added by , 11 years ago) |
---|
-
new file property_tree/detail/info_parser_default_path_resolver.hpp
diff --git a/property_tree/detail/info_parser_default_path_resolver.hpp b/property_tree/detail/info_parser_default_path_resolver.hpp new file mode 100644 index 0000000..9d3cde4
- + 1 #ifndef BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_DEFAULT_PATH_RESOLVER_HPP_INCLUDED 2 #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_DEFAULT_PATH_RESOLVER_HPP_INCLUDED 3 4 namespace boost { namespace property_tree { namespace info_parser 5 { 6 /*! 7 * \brief Path resolver class implementation 8 * 9 * This implementation will not resolve 10 * paths: it will simply return 11 * path argument. It was created 12 * for compatibility purposes. 13 */ 14 class default_path_resolver 15 { 16 public: 17 inline const std::string &resolve(const std::string &path) 18 { 19 return path; 20 } 21 inline void enter(const std::string &) {} 22 inline void enter() {} 23 inline void exit() {} 24 }; 25 } } } 26 27 #endif -
property_tree/detail/info_parser_read.hpp
diff --git a/property_tree/detail/info_parser_read.hpp b/property_tree/detail/info_parser_read.hpp index b87c2cb..e07daba 100644
a b namespace boost { namespace property_tree { namespace info_parser 175 175 } 176 176 177 177 // Build ptree from info stream 178 template<class Ptree, class Ch >178 template<class Ptree, class Ch, class PathResolver> 179 179 void read_info_internal(std::basic_istream<Ch> &stream, 180 180 Ptree &pt, 181 181 const std::string &filename, 182 PathResolver &path_resolver, 182 183 int include_depth) 183 184 { 184 185 typedef std::basic_string<Ch> str_t; … … namespace boost { namespace property_tree { namespace info_parser 227 228 str_t s = read_string(text, NULL); 228 229 std::string inc_name = 229 230 convert_chtype<char, Ch>(s.c_str()); 230 std::basic_ifstream<Ch> inc_stream(inc_name.c_str()); 231 std::basic_ifstream<Ch> inc_stream( 232 path_resolver.resolve(inc_name.c_str())); 231 233 if (!inc_stream.good()) 232 234 BOOST_PROPERTY_TREE_THROW(info_parser_error( 233 235 "cannot open include file " + inc_name, 234 236 filename, line_no)); 237 path_resolver.enter(inc_name); 235 238 read_info_internal(inc_stream, *stack.top(), 236 inc_name, include_depth + 1); 239 inc_name, path_resolver, 240 include_depth + 1); 241 path_resolver.exit(); 237 242 } else { // Unknown directive 238 243 BOOST_PROPERTY_TREE_THROW(info_parser_error( 239 244 "unknown directive", filename, line_no)); -
new file property_tree/detail/info_parser_relative_path_resolver.hpp
diff --git a/property_tree/detail/info_parser_relative_path_resolver.hpp b/property_tree/detail/info_parser_relative_path_resolver.hpp new file mode 100644 index 0000000..d4f3f7f
- + 1 #ifndef BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_RELATIVE_PATH_RESOLVER_HPP_INCLUDED 2 #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_RELATIVE_PATH_RESOLVER_HPP_INCLUDED 3 4 #include <boost/config.hpp> 5 6 #include <stack> 7 8 #include <boost/filesystem/path.hpp> 9 #include <boost/filesystem/operations.hpp> 10 11 namespace boost { namespace property_tree { namespace info_parser 12 { 13 /*! 14 * \brief Path resolver class implementation 15 * that resolves paths relatively 16 * 17 * This implementation will resolve paths 18 * relative to base path (directory path of the file contains '#include' directive) 19 * 20 * \note The drawback of this implementation 21 * is dependency on boost::filesystem library 22 * 23 */ 24 class relative_path_resolver 25 { 26 std::stack<boost::filesystem::path> stack; 27 inline boost::filesystem::path resolve_(const boost::filesystem::path &path) 28 { 29 if (path.is_absolute()) 30 return path; 31 else 32 { 33 if (stack.empty()) 34 return boost::filesystem::absolute(boost::filesystem::current_path()/path); 35 else 36 return boost::filesystem::absolute(stack.top()/path); 37 } 38 } 39 public: 40 inline std::string resolve(const boost::filesystem::path &path) 41 { 42 return resolve(path).string(); 43 } 44 inline void enter(const boost::filesystem::path &path) 45 { 46 stack.push(resolve_(path).parent_path()); 47 } 48 inline void enter() 49 { 50 if (stack.empty()) 51 stack.push(boost::filesystem::current_path()); 52 else 53 // this case is unreachable 54 // probably should not be used 55 // TODO: should we use BOOST_ASSERT here? 56 stack.push(stack.top()); 57 } 58 inline void exit() 59 { 60 stack.pop(); 61 } 62 }; 63 } } } 64 65 #endif -
property_tree/info_parser.hpp
diff --git a/property_tree/info_parser.hpp b/property_tree/info_parser.hpp index 683ddad..764ef8a 100644
a b 15 15 #include <boost/property_tree/detail/info_parser_writer_settings.hpp> 16 16 #include <boost/property_tree/detail/info_parser_read.hpp> 17 17 #include <boost/property_tree/detail/info_parser_write.hpp> 18 #include <boost/property_tree/detail/info_parser_default_path_resolver.hpp> 18 19 #include <istream> 19 20 20 21 namespace boost { namespace property_tree { namespace info_parser … … namespace boost { namespace property_tree { namespace info_parser 29 30 template<class Ptree, class Ch> 30 31 void read_info(std::basic_istream<Ch> &stream, Ptree &pt) 31 32 { 33 default_path_resolver path_resolver; 34 read_info(stream, pt, path_resolver); 35 } 36 37 template<class Ptree, class Ch, class PathResolver> 38 void read_info(std::basic_istream<Ch> &stream, Ptree &pt, 39 PathResolver &path_resolver) 40 { 32 41 Ptree local; 33 read_info_internal(stream, local, std::string(), 0); 42 path_resolver.enter(); 43 read_info_internal(stream, local, std::string(), path_resolver, 0); 44 path_resolver.exit(); 34 45 pt.swap(local); 35 46 } 36 47 … … namespace boost { namespace property_tree { namespace info_parser 43 54 void read_info(std::basic_istream<Ch> &stream, Ptree &pt, 44 55 const Ptree &default_ptree) 45 56 { 57 default_path_resolver path_resolver; 58 read_info(stream, pt, default_ptree, path_resolver); 59 } 60 61 template<class Ptree, class Ch, class PathResolver> 62 void read_info(std::basic_istream<Ch> &stream, Ptree &pt, 63 const Ptree &default_ptree, PathResolver &path_resolver) 64 { 46 65 try { 47 read_info(stream, pt); 66 path_resolver.enter(); 67 read_info(stream, pt, path_resolver); 68 path_resolver.exit(); 48 69 } catch(file_parser_error &) { 49 70 pt = default_ptree; 50 71 } … … namespace boost { namespace property_tree { namespace info_parser 62 83 void read_info(const std::string &filename, Ptree &pt, 63 84 const std::locale &loc = std::locale()) 64 85 { 86 default_path_resolver path_resolver; 87 read_info(filename, pt, path_resolver, loc); 88 } 89 90 template<class Ptree, class PathResolver> 91 void read_info(const std::string &filename, Ptree &pt, 92 PathResolver &path_resolver, 93 const std::locale &loc = std::locale()) 94 { 65 95 std::basic_ifstream<typename Ptree::key_type::value_type> 66 96 stream(filename.c_str()); 67 97 if (!stream) { … … namespace boost { namespace property_tree { namespace info_parser 70 100 } 71 101 stream.imbue(loc); 72 102 Ptree local; 73 read_info_internal(stream, local, filename, 0); 103 path_resolver.enter(filename); 104 read_info_internal(stream, local, filename, path_resolver, 0); 105 path_resolver.exit(); 74 106 pt.swap(local); 75 107 } 76 108 … … namespace boost { namespace property_tree { namespace info_parser 81 113 * @note Replaces the existing contents. Strong exception guarantee. 82 114 * @param default_ptree If parsing fails, pt is set to a copy of this tree. 83 115 */ 84 template<class Ptree> 116 template<class Ptree, class PathResolver> 117 void read_info(const std::string &filename, 118 Ptree &pt, 119 const Ptree &default_ptree, 120 const std::locale &loc = std::locale()) 121 { 122 default_path_resolver path_resolver; 123 read_info(filename, pt, default_ptree, loc, path_resolver); 124 } 125 126 template<class Ptree, class PathResolver> 85 127 void read_info(const std::string &filename, 86 128 Ptree &pt, 87 129 const Ptree &default_ptree, 130 PathResolver &path_resolver, 88 131 const std::locale &loc = std::locale()) 89 132 { 90 133 try { 91 read_info(filename, pt, loc);134 read_info(filename, pt, path_resolver, loc); 92 135 } catch(file_parser_error &) { 93 136 pt = default_ptree; 94 137 }