1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2003 Dr John Maddock |
---|
4 | * Use, modification and distribution is subject to the |
---|
5 | * Boost Software License, Version 1.0. (See accompanying file |
---|
6 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | * |
---|
8 | * This file implements the bcp_implementation virtuals. |
---|
9 | */ |
---|
10 | |
---|
11 | #include "bcp_imp.hpp" |
---|
12 | #include "licence_info.hpp" |
---|
13 | #include <boost/filesystem/operations.hpp> |
---|
14 | #include <boost/filesystem/fstream.hpp> |
---|
15 | #include <iostream> |
---|
16 | #include <stdexcept> |
---|
17 | #include <boost/regex.hpp> |
---|
18 | #include <string> |
---|
19 | |
---|
20 | bcp_implementation::bcp_implementation() |
---|
21 | : m_list_mode(false), m_list_summary_mode(false), m_license_mode(false), m_cvs_mode(false), m_unix_lines(false), m_scan_mode(false), m_bsl_convert_mode(false), m_bsl_summary_mode(false) |
---|
22 | { |
---|
23 | } |
---|
24 | |
---|
25 | bcp_implementation::~bcp_implementation() |
---|
26 | { |
---|
27 | } |
---|
28 | |
---|
29 | bcp_application::~bcp_application() |
---|
30 | { |
---|
31 | } |
---|
32 | |
---|
33 | void bcp_implementation::enable_list_mode() |
---|
34 | { |
---|
35 | m_list_mode = true; |
---|
36 | } |
---|
37 | |
---|
38 | void bcp_implementation::enable_summary_list_mode() |
---|
39 | { |
---|
40 | m_list_mode = true; |
---|
41 | m_list_summary_mode = true; |
---|
42 | } |
---|
43 | |
---|
44 | void bcp_implementation::enable_cvs_mode() |
---|
45 | { |
---|
46 | m_cvs_mode = true; |
---|
47 | } |
---|
48 | |
---|
49 | void bcp_implementation::enable_scan_mode() |
---|
50 | { |
---|
51 | m_scan_mode = true; |
---|
52 | } |
---|
53 | |
---|
54 | void bcp_implementation::enable_license_mode() |
---|
55 | { |
---|
56 | m_license_mode = true; |
---|
57 | } |
---|
58 | |
---|
59 | void bcp_implementation::enable_bsl_convert_mode() |
---|
60 | { |
---|
61 | m_bsl_convert_mode = true; |
---|
62 | } |
---|
63 | |
---|
64 | void bcp_implementation::enable_bsl_summary_mode() |
---|
65 | { |
---|
66 | m_bsl_summary_mode = true; |
---|
67 | } |
---|
68 | |
---|
69 | void bcp_implementation::enable_unix_lines() |
---|
70 | { |
---|
71 | m_unix_lines = true; |
---|
72 | } |
---|
73 | |
---|
74 | void bcp_implementation::set_boost_path(const char* p) |
---|
75 | { |
---|
76 | m_boost_path = fs::path(p, fs::native); |
---|
77 | fs::path check = m_boost_path / "boost" / "version.hpp"; |
---|
78 | if(!fs::exists(check)) |
---|
79 | { |
---|
80 | std::string s = "The Boost path appears to have been incorrectly set: could not find boost/version.hpp in "; |
---|
81 | s += m_boost_path.string(); |
---|
82 | std::runtime_error e(s); |
---|
83 | throw e; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void bcp_implementation::set_destination(const char* p) |
---|
88 | { |
---|
89 | m_dest_path = fs::path(p, fs::native); |
---|
90 | } |
---|
91 | |
---|
92 | void bcp_implementation::add_module(const char* p) |
---|
93 | { |
---|
94 | m_module_list.push_back(p); |
---|
95 | } |
---|
96 | |
---|
97 | fs::path get_short_path(const fs::path& p) |
---|
98 | { |
---|
99 | // truncate path no more than "x/y": |
---|
100 | std::string s = p.string(); |
---|
101 | std::string::size_type n = s.find('/'); |
---|
102 | if(n != std::string::npos) |
---|
103 | { |
---|
104 | n = s.find('/', n+1); |
---|
105 | if(n != std::string::npos) |
---|
106 | s.erase(n); |
---|
107 | } |
---|
108 | return s; |
---|
109 | } |
---|
110 | |
---|
111 | int bcp_implementation::run() |
---|
112 | { |
---|
113 | // |
---|
114 | // check output path is OK: |
---|
115 | // |
---|
116 | if(!m_list_mode && !m_license_mode && !fs::exists(m_dest_path)) |
---|
117 | { |
---|
118 | std::string msg("Destination path does not exist: "); |
---|
119 | msg.append(m_dest_path.native_file_string()); |
---|
120 | std::runtime_error e(msg); |
---|
121 | boost::throw_exception(e); |
---|
122 | } |
---|
123 | // start by building a list of permitted files |
---|
124 | // if m_cvs_mode is true: |
---|
125 | if(m_cvs_mode) |
---|
126 | { |
---|
127 | scan_cvs_path(fs::path()); |
---|
128 | } |
---|
129 | // |
---|
130 | // if in license mode, try to load more/blanket_permission.txt |
---|
131 | // |
---|
132 | fs::path blanket_permission(m_boost_path / "more" / "blanket-permission.txt"); |
---|
133 | if (fs::exists(blanket_permission)) { |
---|
134 | fs::ifstream in(blanket_permission); |
---|
135 | std::string line; |
---|
136 | while (std::getline(in, line)) { |
---|
137 | boost::regex e("([^(]+)\\("); |
---|
138 | boost::smatch result; |
---|
139 | if (boost::regex_search(line, result, e)) |
---|
140 | m_bsl_authors.insert(format_authors_name(result[1])); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | // |
---|
145 | // scan through modules looking for the equivalent |
---|
146 | // file to add to our list: |
---|
147 | // |
---|
148 | std::list<std::string>::const_iterator i = m_module_list.begin(); |
---|
149 | std::list<std::string>::const_iterator j = m_module_list.end(); |
---|
150 | while(i != j) |
---|
151 | { |
---|
152 | // |
---|
153 | // convert *i to a path - could be native or portable: |
---|
154 | // |
---|
155 | fs::path module; |
---|
156 | fs::path exmodule; |
---|
157 | try{ |
---|
158 | module = fs::path(*i); |
---|
159 | exmodule = fs::path(*i + ".hpp"); |
---|
160 | } |
---|
161 | catch(...) |
---|
162 | { |
---|
163 | module = fs::path(*i, fs::native); |
---|
164 | exmodule = fs::path(*i + ".hpp", fs::native); |
---|
165 | } |
---|
166 | |
---|
167 | if(m_scan_mode) |
---|
168 | { |
---|
169 | // in scan mode each module must be a real file: |
---|
170 | add_file_dependencies(module, true); |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | int count = 0; |
---|
175 | if(fs::exists(m_boost_path / "tools" / module)) |
---|
176 | { |
---|
177 | add_path(fs::path("tools") / module); |
---|
178 | ++count; |
---|
179 | } |
---|
180 | if(fs::exists(m_boost_path / "libs" / module)) |
---|
181 | { |
---|
182 | add_path(fs::path("libs") / module); |
---|
183 | ++count; |
---|
184 | } |
---|
185 | if(fs::exists(m_boost_path / "boost" / module)) |
---|
186 | { |
---|
187 | add_path(fs::path("boost") / module); |
---|
188 | ++count; |
---|
189 | } |
---|
190 | if(fs::exists(m_boost_path / "boost" / exmodule)) |
---|
191 | { |
---|
192 | add_path(fs::path("boost") / exmodule); |
---|
193 | ++count; |
---|
194 | } |
---|
195 | if(fs::exists(m_boost_path / module)) |
---|
196 | { |
---|
197 | add_path(module); |
---|
198 | ++count; |
---|
199 | } |
---|
200 | } |
---|
201 | ++i; |
---|
202 | } |
---|
203 | // |
---|
204 | // now perform output: |
---|
205 | // |
---|
206 | std::set<fs::path, path_less>::iterator m, n; |
---|
207 | std::set<fs::path, path_less> short_paths; |
---|
208 | m = m_copy_paths.begin(); |
---|
209 | n = m_copy_paths.end(); |
---|
210 | if(!m_license_mode) |
---|
211 | { |
---|
212 | while(m != n) |
---|
213 | { |
---|
214 | if(m_list_summary_mode) |
---|
215 | { |
---|
216 | fs::path p = get_short_path(*m); |
---|
217 | if(short_paths.find(p) == short_paths.end()) |
---|
218 | { |
---|
219 | short_paths.insert(p); |
---|
220 | std::cout << p.string() << "\n"; |
---|
221 | } |
---|
222 | } |
---|
223 | else if(m_list_mode) |
---|
224 | std::cout << m->string() << "\n"; |
---|
225 | else |
---|
226 | copy_path(*m); |
---|
227 | ++m; |
---|
228 | } |
---|
229 | } |
---|
230 | else |
---|
231 | output_license_info(); |
---|
232 | return 0; |
---|
233 | } |
---|
234 | |
---|
235 | pbcp_application bcp_application::create() |
---|
236 | { |
---|
237 | pbcp_application result(static_cast<bcp_application*>(new bcp_implementation())); |
---|
238 | return result; |
---|
239 | } |
---|