]>
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 | ||
14 | #include <string> | |
15 | #include <boost/filesystem/path.hpp> | |
16 | #include <boost/intrusive_ptr.hpp> | |
b32b8144 | 17 | #include "string_view.hpp" |
7c673cae FG |
18 | #include <stdexcept> |
19 | #include <cassert> | |
20 | #include <iosfwd> | |
21 | ||
22 | namespace quickbook { | |
23 | ||
24 | namespace fs = boost::filesystem; | |
25 | ||
26 | struct file; | |
27 | typedef boost::intrusive_ptr<file> file_ptr; | |
28 | ||
29 | struct file_position | |
30 | { | |
31 | file_position() : line(1), column(1) {} | |
b32b8144 | 32 | file_position(std::ptrdiff_t l, std::ptrdiff_t c) : line(l), column(c) {} |
7c673cae | 33 | |
b32b8144 FG |
34 | std::ptrdiff_t line; |
35 | std::ptrdiff_t column; | |
7c673cae FG |
36 | |
37 | bool operator==(file_position const& other) const | |
38 | { | |
39 | return line == other.line && column == other.column; | |
40 | } | |
41 | ||
42 | friend std::ostream& operator<<(std::ostream&, file_position const&); | |
43 | }; | |
44 | ||
45 | struct file | |
46 | { | |
47 | private: | |
48 | // Non copyable | |
49 | file& operator=(file const&); | |
50 | file(file const&); | |
51 | public: | |
52 | fs::path const path; | |
53 | std::string source_; | |
54 | bool is_code_snippets; | |
55 | private: | |
56 | unsigned qbk_version; | |
57 | unsigned ref_count; | |
58 | public: | |
b32b8144 FG |
59 | quickbook::string_view source() const { return source_; } |
60 | ||
61 | file(fs::path const& path_, quickbook::string_view source_view, | |
62 | unsigned qbk_version_) : | |
63 | path(path_), | |
64 | source_(source_view.begin(), source_view.end()), | |
65 | is_code_snippets(false), | |
66 | qbk_version(qbk_version_), | |
67 | ref_count(0) | |
7c673cae FG |
68 | {} |
69 | ||
b32b8144 FG |
70 | explicit file(file const& f, quickbook::string_view s) : |
71 | path(f.path), source_(s.begin(), s.end()), | |
7c673cae FG |
72 | is_code_snippets(f.is_code_snippets), |
73 | qbk_version(f.qbk_version), ref_count(0) | |
74 | {} | |
75 | ||
76 | virtual ~file() { | |
77 | assert(!ref_count); | |
78 | } | |
79 | ||
80 | unsigned version() const { | |
81 | assert(qbk_version); | |
82 | return qbk_version; | |
83 | } | |
84 | ||
85 | void version(unsigned v) { | |
86 | // Check that either version hasn't been set, or it was | |
87 | // previously set to the same version (because the same | |
88 | // file has been loaded twice). | |
89 | assert(!qbk_version || qbk_version == v); | |
90 | qbk_version = v; | |
91 | } | |
92 | ||
b32b8144 | 93 | virtual file_position position_of(string_iterator) const; |
7c673cae FG |
94 | |
95 | friend void intrusive_ptr_add_ref(file* ptr) { ++ptr->ref_count; } | |
96 | ||
97 | friend void intrusive_ptr_release(file* ptr) | |
98 | { if(--ptr->ref_count == 0) delete ptr; } | |
99 | }; | |
100 | ||
101 | // If version isn't supplied then it must be set later. | |
102 | file_ptr load(fs::path const& filename, | |
103 | unsigned qbk_version = 0); | |
104 | ||
105 | struct load_error : std::runtime_error | |
106 | { | |
107 | explicit load_error(std::string const& arg) | |
108 | : std::runtime_error(arg) {} | |
109 | }; | |
110 | ||
111 | // Interface for creating fake files which are mapped to | |
112 | // real files, so that the position can be found later. | |
113 | ||
114 | struct mapped_file_builder_data; | |
115 | ||
116 | struct mapped_file_builder | |
117 | { | |
b32b8144 FG |
118 | typedef string_iterator iterator; |
119 | typedef quickbook::string_view::size_type pos_type; | |
7c673cae FG |
120 | |
121 | mapped_file_builder(); | |
122 | ~mapped_file_builder(); | |
123 | ||
124 | void start(file_ptr); | |
125 | file_ptr release(); | |
126 | void clear(); | |
127 | ||
128 | bool empty() const; | |
b32b8144 | 129 | pos_type get_pos() const; |
7c673cae | 130 | |
b32b8144 FG |
131 | void add_at_pos(quickbook::string_view, iterator); |
132 | void add(quickbook::string_view); | |
7c673cae | 133 | void add(mapped_file_builder const&); |
b32b8144 FG |
134 | void add(mapped_file_builder const&, pos_type, pos_type); |
135 | void unindent_and_add(quickbook::string_view); | |
7c673cae FG |
136 | private: |
137 | mapped_file_builder_data* data; | |
138 | ||
139 | mapped_file_builder(mapped_file_builder const&); | |
140 | mapped_file_builder& operator=(mapped_file_builder const&); | |
141 | }; | |
142 | } | |
143 | ||
144 | #endif // BOOST_QUICKBOOK_FILES_HPP |