]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
11fdf7f2 TL |
14 | #include <cassert> |
15 | #include <iosfwd> | |
16 | #include <stdexcept> | |
7c673cae FG |
17 | #include <string> |
18 | #include <boost/filesystem/path.hpp> | |
19 | #include <boost/intrusive_ptr.hpp> | |
b32b8144 | 20 | #include "string_view.hpp" |
7c673cae | 21 | |
11fdf7f2 TL |
22 | namespace quickbook |
23 | { | |
7c673cae FG |
24 | |
25 | namespace fs = boost::filesystem; | |
26 | ||
27 | struct file; | |
28 | typedef boost::intrusive_ptr<file> file_ptr; | |
29 | ||
30 | struct file_position | |
31 | { | |
32 | file_position() : line(1), column(1) {} | |
11fdf7f2 TL |
33 | file_position(std::ptrdiff_t l, std::ptrdiff_t c) : line(l), column(c) |
34 | { | |
35 | } | |
7c673cae | 36 | |
b32b8144 FG |
37 | std::ptrdiff_t line; |
38 | std::ptrdiff_t column; | |
11fdf7f2 | 39 | |
7c673cae FG |
40 | bool operator==(file_position const& other) const |
41 | { | |
42 | return line == other.line && column == other.column; | |
43 | } | |
11fdf7f2 | 44 | |
7c673cae FG |
45 | friend std::ostream& operator<<(std::ostream&, file_position const&); |
46 | }; | |
47 | ||
92f5a8d4 TL |
48 | file_position relative_position( |
49 | string_iterator begin, string_iterator iterator); | |
50 | ||
7c673cae FG |
51 | struct file |
52 | { | |
11fdf7f2 | 53 | private: |
7c673cae FG |
54 | // Non copyable |
55 | file& operator=(file const&); | |
56 | file(file const&); | |
11fdf7f2 TL |
57 | |
58 | public: | |
7c673cae FG |
59 | fs::path const path; |
60 | std::string source_; | |
61 | bool is_code_snippets; | |
11fdf7f2 TL |
62 | |
63 | private: | |
7c673cae FG |
64 | unsigned qbk_version; |
65 | unsigned ref_count; | |
11fdf7f2 TL |
66 | |
67 | public: | |
b32b8144 FG |
68 | quickbook::string_view source() const { return source_; } |
69 | ||
11fdf7f2 TL |
70 | 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 | ||
82 | 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 | { | |
7c673cae FG |
89 | } |
90 | ||
11fdf7f2 TL |
91 | virtual ~file() { assert(!ref_count); } |
92 | ||
93 | unsigned version() const | |
94 | { | |
7c673cae FG |
95 | assert(qbk_version); |
96 | return qbk_version; | |
97 | } | |
98 | ||
11fdf7f2 TL |
99 | void version(unsigned v) |
100 | { | |
7c673cae FG |
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 | ||
b32b8144 | 108 | virtual file_position position_of(string_iterator) const; |
7c673cae FG |
109 | |
110 | friend void intrusive_ptr_add_ref(file* ptr) { ++ptr->ref_count; } | |
111 | ||
112 | friend void intrusive_ptr_release(file* ptr) | |
11fdf7f2 TL |
113 | { |
114 | if (--ptr->ref_count == 0) delete ptr; | |
115 | } | |
7c673cae FG |
116 | }; |
117 | ||
118 | // If version isn't supplied then it must be set later. | |
11fdf7f2 | 119 | file_ptr load(fs::path const& filename, unsigned qbk_version = 0); |
7c673cae FG |
120 | |
121 | struct load_error : std::runtime_error | |
122 | { | |
11fdf7f2 | 123 | explicit load_error(std::string const& arg) : std::runtime_error(arg) {} |
7c673cae FG |
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 | { | |
b32b8144 FG |
133 | typedef string_iterator iterator; |
134 | typedef quickbook::string_view::size_type pos_type; | |
7c673cae FG |
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; | |
b32b8144 | 144 | pos_type get_pos() const; |
7c673cae | 145 | |
b32b8144 FG |
146 | void add_at_pos(quickbook::string_view, iterator); |
147 | void add(quickbook::string_view); | |
7c673cae | 148 | void add(mapped_file_builder const&); |
b32b8144 FG |
149 | void add(mapped_file_builder const&, pos_type, pos_type); |
150 | void unindent_and_add(quickbook::string_view); | |
11fdf7f2 TL |
151 | |
152 | private: | |
7c673cae FG |
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 |