FindStringInFile.hpp
1 #pragma once
2 #include <QFile>
3 #include <QString>
4 
5 #include <algorithm>
6 #include <functional>
7 #include <string_view>
8 
9 #if __has_include(<experimental/functional>)
10 #include <experimental/functional>
11 #endif
12 
13 namespace score
14 {
15 template <typename T>
16 static void findStringInFile(const QString& filepath, std::string_view req, T onSuccess)
17 {
18  QFile f{filepath};
19  if(f.open(QIODevice::ReadOnly))
20  {
21  unsigned char* data = f.map(0, f.size());
22 
23  const char* cbegin = reinterpret_cast<char*>(data);
24  const char* cend = cbegin + f.size();
25 
26 #if defined(__cpp_lib_boyer_moore_searcher)
27  auto it
28  = std::search(cbegin, cend, std::boyer_moore_searcher(req.begin(), req.end()));
29 #elif __has_include(<experimental/functional>)
30  auto it = std::search(
31  cbegin, cend, std::experimental::boyer_moore_searcher(req.begin(), req.end()));
32 #else
33  auto it = std::search(cbegin, cend, req.begin(), req.end());
34 #endif
35  if(it != cend)
36  {
37  onSuccess(f);
38  }
39 
40  f.unmap(data);
41  }
42 }
43 
44 }
Base toolkit upon which the software is built.
Definition: Application.cpp:90