1 /*============================================================================= 2 Copyright (c) 2002 2004 2006 Joel de Guzman 3 Copyright (c) 2004 Eric Niebler 4 Copyright (c) 2011 Daniel James 5 6 Use, modification and distribution is subject to the Boost Software 7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 http://www.boost.org/LICENSE_1_0.txt) 9 =============================================================================*/ 10 11 #if !defined(BOOST_QUICKBOOK_FILES_HPP) 12 #define BOOST_QUICKBOOK_FILES_HPP 13 14 #include <cassert> 15 #include <iosfwd> 16 #include <stdexcept> 17 #include <string> 18 #include <boost/filesystem/path.hpp> 19 #include <boost/intrusive_ptr.hpp> 20 #include "string_view.hpp" 21 22 namespace quickbook 23 { 24 25 namespace fs = boost::filesystem; 26 27 struct file; 28 typedef boost::intrusive_ptr<file> file_ptr; 29 30 struct file_position 31 { file_positionquickbook::file_position32 file_position() : line(1), column(1) {} file_positionquickbook::file_position33 file_position(std::ptrdiff_t l, std::ptrdiff_t c) : line(l), column(c) 34 { 35 } 36 37 std::ptrdiff_t line; 38 std::ptrdiff_t column; 39 operator ==quickbook::file_position40 bool operator==(file_position const& other) const 41 { 42 return line == other.line && column == other.column; 43 } 44 45 friend std::ostream& operator<<(std::ostream&, file_position const&); 46 }; 47 48 file_position relative_position( 49 string_iterator begin, string_iterator iterator); 50 51 struct file 52 { 53 private: 54 // Non copyable 55 file& operator=(file const&); 56 file(file const&); 57 58 public: 59 fs::path const path; 60 std::string source_; 61 bool is_code_snippets; 62 63 private: 64 unsigned qbk_version; 65 unsigned ref_count; 66 67 public: sourcequickbook::file68 quickbook::string_view source() const { return source_; } 69 filequickbook::file70 file( 71 fs::path const& path_, 72 quickbook::string_view source_view, 73 unsigned qbk_version_) 74 : path(path_) 75 , source_(source_view.begin(), source_view.end()) 76 , is_code_snippets(false) 77 , qbk_version(qbk_version_) 78 , ref_count(0) 79 { 80 } 81 filequickbook::file82 explicit file(file const& f, quickbook::string_view s) 83 : path(f.path) 84 , source_(s.begin(), s.end()) 85 , is_code_snippets(f.is_code_snippets) 86 , qbk_version(f.qbk_version) 87 , ref_count(0) 88 { 89 } 90 ~filequickbook::file91 virtual ~file() { assert(!ref_count); } 92 versionquickbook::file93 unsigned version() const 94 { 95 assert(qbk_version); 96 return qbk_version; 97 } 98 versionquickbook::file99 void version(unsigned v) 100 { 101 // Check that either version hasn't been set, or it was 102 // previously set to the same version (because the same 103 // file has been loaded twice). 104 assert(!qbk_version || qbk_version == v); 105 qbk_version = v; 106 } 107 108 virtual file_position position_of(string_iterator) const; 109 intrusive_ptr_add_ref(file * ptr)110 friend void intrusive_ptr_add_ref(file* ptr) { ++ptr->ref_count; } 111 intrusive_ptr_release(file * ptr)112 friend void intrusive_ptr_release(file* ptr) 113 { 114 if (--ptr->ref_count == 0) delete ptr; 115 } 116 }; 117 118 // If version isn't supplied then it must be set later. 119 file_ptr load(fs::path const& filename, unsigned qbk_version = 0); 120 121 struct load_error : std::runtime_error 122 { load_errorquickbook::load_error123 explicit load_error(std::string const& arg) : std::runtime_error(arg) {} 124 }; 125 126 // Interface for creating fake files which are mapped to 127 // real files, so that the position can be found later. 128 129 struct mapped_file_builder_data; 130 131 struct mapped_file_builder 132 { 133 typedef string_iterator iterator; 134 typedef quickbook::string_view::size_type pos_type; 135 136 mapped_file_builder(); 137 ~mapped_file_builder(); 138 139 void start(file_ptr); 140 file_ptr release(); 141 void clear(); 142 143 bool empty() const; 144 pos_type get_pos() const; 145 146 void add_at_pos(quickbook::string_view, iterator); 147 void add(quickbook::string_view); 148 void add(mapped_file_builder const&); 149 void add(mapped_file_builder const&, pos_type, pos_type); 150 void unindent_and_add(quickbook::string_view); 151 152 private: 153 mapped_file_builder_data* data; 154 155 mapped_file_builder(mapped_file_builder const&); 156 mapped_file_builder& operator=(mapped_file_builder const&); 157 }; 158 } 159 160 #endif // BOOST_QUICKBOOK_FILES_HPP 161