crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Versions.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * Versions.hpp
24  *
25  * Namespace for functions getting the versions of the different libraries used by the application.
26  *
27  * Created on: Oct 18, 2018
28  * Author: ans
29  */
30 
31 #ifndef HELPER_VERSIONS_HPP_
32 #define HELPER_VERSIONS_HPP_
33 
34 #include "../Helper/FileSystem.hpp"
35 #include "../Helper/Portability/mysqlcppconn.h"
36 #include "../Helper/SilentInclude/EigenRand.h"
37 #include "../Helper/SilentInclude/tidy.h"
38 
39 #include "../_extern/asio/asio/include/asio/version.hpp"
40 #include "../_extern/jsoncons/include/jsoncons/config/version.hpp"
41 #include "../_extern/rapidjson/include/rapidjson/rapidjson.h"
42 #include "../_extern/wapiti/wapiti.h"
43 
44 extern "C" {
45  #include "../_extern/mongoose/mongoose.h"
46 }
47 
48 #include <Eigen/Core>
49 
50 #include <aspell.h>
51 #include <boost/version.hpp>
52 #include <cppconn/driver.h>
53 #include <curl/curl.h>
54 #include <mapbox/variant.hpp>
55 #include <pcre2.h>
56 #include <pugixml.hpp>
57 #include <uriparser/UriBase.h>
58 #include <zip.h>
59 #include <zlib.h>
60 
61 #include <array> // std::array
62 #include <fstream> // std::ifstream
63 #include <string> // std::getline, std::string, std::to_string
64 #include <string_view> // std::string_view, std::string_view_literals
65 #include <utility> // std::pair
66 #include <vector> // std::vector
67 
70 
71  using std::string_view_literals::operator""sv;
72 
73  /*
74  * CONSTANTS
75  */
76 
79 
81  inline constexpr auto asioMajor{100000};
82 
84  inline constexpr auto asioMinor{100};
85 
87  inline constexpr auto asioModMinor{1000};
88 
90  inline constexpr auto asioPatch{100};
91 
93  inline constexpr auto boostMajor{100000};
94 
96  inline constexpr auto boostMinor{1000};
97 
99  inline constexpr auto boostPatch{100};
100 
102  inline constexpr auto pugixmlMajor{100};
103 
105  inline constexpr auto pugixmlMinor{10};
106 
108  inline constexpr std::array tomotoVersionFile{
109  "src"sv,
110  "_extern"sv,
111  "tomotopy"sv,
112  "tomotopy"sv,
113  "_version.py"sv
114  };
115 
117  inline constexpr auto tomotoVersionBegin{"__version__ = '"sv};
118 
120  inline constexpr auto tomotoVersionEnd{"'"sv};
121 
123 
127  inline constexpr std::string_view utf8CppVersion{"2.1"};
128 
130 
131  /*
132  * DECLARATION
133  */
134 
136  using StringString = std::pair<std::string, std::string>;
137 
140 
141  std::vector<StringString> getLibraryVersions();
142  std::string getLibraryVersionsStr(const std::string& indent);
143  std::string getTomotoVersion();
144 
146 
147  /*
148  * IMPLEMENTATION
149  */
150 
152 
156  inline std::vector<StringString> getLibraryVersions() {
157  std::vector<StringString> result;
158 
159  // asio
160  result.emplace_back(
161  "Asio",
162  std::to_string(ASIO_VERSION / asioMajor)
163  + '.'
164  + std::to_string(ASIO_VERSION / asioMinor % asioModMinor)
165  + '.'
166  + std::to_string(ASIO_VERSION % asioPatch)
167  );
168 
169  // Boost
170  result.emplace_back(
171  "Boost",
172  std::to_string(BOOST_VERSION / boostMajor)
173  + '.'
174  + std::to_string(BOOST_VERSION / boostPatch % boostMinor)
175  + '.'
176  + std::to_string(BOOST_VERSION % boostPatch)
177  );
178 
179  // Eigen
180  result.emplace_back(
181  "Eigen",
182  std::to_string(EIGEN_WORLD_VERSION)
183  + '.'
184  + std::to_string(EIGEN_MAJOR_VERSION)
185  + '.'
186  + std::to_string(EIGEN_MINOR_VERSION)
187  );
188 
189  // EigenRand
190  result.emplace_back(
191  "EigenRand",
192  std::to_string(EIGENRAND_WORLD_VERSION)
193  + '.'
194  + std::to_string(EIGENRAND_MAJOR_VERSION)
195  + '.'
196  + std::to_string(EIGENRAND_MINOR_VERSION)
197  );
198 
199  // GNU Aspell
200  result.emplace_back("GNU Aspell", aspell_version_string());
201 
202  // Howard E. Hinnant's date.h library (no version information available)
203  result.emplace_back("Howard E. Hinnant's date.h", "");
204 
205  // jsoncons
206  result.emplace_back(
207  "jsoncons",
208  std::to_string(JSONCONS_VERSION_MAJOR)
209  + '.'
210  + std::to_string(JSONCONS_VERSION_MINOR)
211  + '.'
212  + std::to_string(JSONCONS_VERSION_PATCH)
213  );
214 
215  // libcurl
216  result.emplace_back("libcurl", curl_version_info(CURLVERSION_NOW)->version);
217 
218  // libzip
219  result.emplace_back("libzip", zip_libzip_version());
220 
221  // Mapbox Variant
222  result.emplace_back(
223  "Mapbox Variant",
224  std::to_string(VARIANT_MAJOR_VERSION)
225  + '.'
226  + std::to_string(VARIANT_MINOR_VERSION)
227  + '.'
228  + std::to_string(VARIANT_PATCH_VERSION)
229  );
230 
231  // mongoose
232  result.emplace_back("Mongoose", MG_VERSION);
233 
234  // MySQL Connector/C++
235  sql::Driver * driver{get_driver_instance()};
236 
237  if(driver != nullptr) {
238  result.emplace_back(
239  driver->getName(),
240  std::to_string(driver->getMajorVersion())
241  + '.'
242  + std::to_string(driver->getMinorVersion())
243  + '.'
244  + std::to_string(driver->getPatchVersion())
245  );
246  }
247 
248  // PCRE2
249  result.emplace_back(
250  "PCRE2",
251  std::to_string(PCRE2_MAJOR)
252  + '.'
253  + std::to_string(PCRE2_MINOR)
254  );
255 
256  // porter2_stemmer (no version information available)
257  result.emplace_back("porter2_stemmer", "");
258 
259  // pugixml
260  result.emplace_back(
261  "pugixml",
262  std::to_string(PUGIXML_VERSION / pugixmlMajor)
263  + '.'
264  + std::to_string(PUGIXML_VERSION % pugixmlMajor / pugixmlMinor)
265  + '.'
266  + std::to_string(PUGIXML_VERSION % pugixmlMinor)
267  );
268 
269  // RapidJSON
270  result.emplace_back("RapidJSON", RAPIDJSON_VERSION_STRING);
271 
272  // tomoto
273  result.emplace_back("tomoto", getTomotoVersion());
274 
275  // tidy-html5
276  result.emplace_back("tidy-html5", tidyLibraryVersion());
277 
278  // uriparser
279  result.emplace_back(
280  "uriparser",
281  std::to_string(URI_VER_MAJOR)
282  + '.'
283  + std::to_string(URI_VER_MINOR)
284  + '.'
285  + std::to_string(URI_VER_RELEASE)
286  + URI_VER_SUFFIX_ANSI
287  );
288 
289  // UTF8-CPP (WARNING: hard-coded version information not necessarily accurate)
290  result.emplace_back("UTF8-CPP", utf8CppVersion);
291 
292  // Wapiti
293  result.emplace_back("Wapiti", Data::wapiti::version);
294 
295  // zlib
296  result.emplace_back("zlib", ZLIB_VERSION);
297 
298  return result;
299  }
300 
302 
310  inline std::string getLibraryVersionsStr(const std::string& indent) {
311  // get versions
312  const std::vector<StringString> versions(getLibraryVersions());
313 
314  // create resulting string
315  std::string result;
316 
317  for(const auto& version : versions) {
318  result += indent;
319  result += version.first;
320 
321  if(!(version.second.empty())) {
322  result += " v";
323  result += version.second;
324  }
325 
326  result += "\n";
327  }
328 
329  if(!versions.empty()) {
330  result.pop_back();
331  }
332 
333  return result;
334  }
335 
337 
341  inline std::string getTomotoVersion() {
342  const char pathSeparator{Helper::FileSystem::getPathSeparator()};
343  std::string pathToFile;
344  std::string version;
345 
346  for(const auto& pathElement : tomotoVersionFile) {
347  pathToFile += pathElement;
348  pathToFile += pathSeparator;
349  }
350 
351  if(!pathToFile.empty()) {
352  pathToFile.pop_back();
353  }
354 
355  std::ifstream in(pathToFile);
356 
357  if(in.is_open()) {
358  std::string line;
359 
360  while(std::getline(in, line)) {
361  if(line.substr(0, tomotoVersionBegin.size()) == tomotoVersionBegin) {
362  const auto end{
363  line.find(tomotoVersionEnd, tomotoVersionBegin.size())
364  };
365 
366  if(end != std::string::npos) {
367  version = line.substr(
368  tomotoVersionBegin.size(),
369  end - tomotoVersionBegin.size()
370  );
371  }
372  }
373  }
374 
375  in.close();
376  }
377 
378  if(version.empty()) {
379  return "<unknown>";
380  }
381 
382  return version;
383  }
384 
385 } /* namespace crawlservpp::Helper::Versions */
386 
387 #endif /* HELPER_VERSIONS_HPP_ */
constexpr auto pugixmlMajor
Divisor to retrieve the major version of the pugixml library.
Definition: Versions.hpp:102
constexpr auto boostMinor
Divisor to retrieve the minor version of the Boost library.
Definition: Versions.hpp:96
constexpr auto asioMajor
Divisor to retrieve the major version of the Asio library.
Definition: Versions.hpp:81
constexpr auto asioPatch
Mod divisor to retrieve the patch version of the Asio library.
Definition: Versions.hpp:90
Namespace for functions getting the versions of the different libraries used by the application...
Definition: Versions.hpp:69
constexpr auto tomotoVersionBegin
The beginning of the line containing the version of the tomoto library.
Definition: Versions.hpp:117
constexpr std::string_view utf8CppVersion
The version of the UTF8-CPP library.
Definition: Versions.hpp:127
std::string getTomotoVersion()
Gets the version of the tomoto library if available.
Definition: Versions.hpp:341
std::vector< StringString > getLibraryVersions()
Gets the versions of the used libraries.
Definition: Versions.hpp:156
std::pair< std::string, std::string > StringString
A pair of strings.
Definition: Versions.hpp:136
char getPathSeparator()
Gets the preferred separator for file paths in the current operating system.
Definition: FileSystem.hpp:187
constexpr auto tomotoVersionEnd
The end of the line containing the version of the tomoto library.
Definition: Versions.hpp:120
constexpr auto asioModMinor
Mod divisor to retrieve the minor version of the Asio library.
Definition: Versions.hpp:87
constexpr auto boostPatch
Mod divisor to retrieve the patch level of the Boost library.
Definition: Versions.hpp:99
constexpr auto boostMajor
Divisor to retrieve the major version of the Boost library.
Definition: Versions.hpp:93
constexpr std::array tomotoVersionFile
The path from where to read the version of the tomoto library from.
Definition: Versions.hpp:108
std::string getLibraryVersionsStr(const std::string &indent)
Gets the versions of the used libraries as one indented string.
Definition: Versions.hpp:310
constexpr auto pugixmlMinor
Divisor to retrieve the minor version of the pugixml library.
Definition: Versions.hpp:105
constexpr auto asioMinor
Divisor to retrieve the minor version of the Asio library.
Definition: Versions.hpp:84