Debian Codegraph @

src/ApvlvSearch.cc

raw
1 /*
2 * This file is part of the apvlv package
3 *
4 * Copyright (C) 2008 Alf.
5 *
6 * Contact: Alf <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 /* @CPPFILE SearchDialog.cc
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #include <QDebug>
29 #include <algorithm>
30 #include <filesystem>
31 #include <regex>
32 #include <stack>
33
34 #include "ApvlvFile.h"
35 #include "ApvlvParams.h"
36 #include "ApvlvSearch.h"
37
38 namespace apvlv
39 {
40 using namespace std;
41
42 Searcher::Searcher () : mRestart (false), mQuit (false)
43 {
44 auto task = thread (&Searcher::dispatch, this);
45 mTasks.emplace_back (std::move (task));
46
47 auto thread_count = thread::hardware_concurrency () - 1;
48 auto thread_value
49 = ApvlvParams::instance ()->getStringOrDefault ("thread_count", "auto");
50 if (thread_value != "auto")
51 {
52 thread_count = ApvlvParams::instance ()->getIntOrDefault ("thread_count",
53 thread_count);
54 }
55
56 for (auto ind = 0u; ind < thread_count; ++ind)
57 {
58 task = thread (&Searcher::fileLoopFunc, this);
59 mTasks.emplace_back (std::move (task));
60 }
61 }
62
63 Searcher::~Searcher ()
64 {
65 mRestart.store (true);
66 mQuit.store (true);
67 std::ranges::for_each (mTasks, [] (thread &task) { task.join (); });
68 qDebug ("all search threads ended");
69 }
70
71 void
72 Searcher::submit (const SearchOptions &options)
73 {
74 mOptions = options;
75 auto path = filesystem::path (options.mFromDir);
76 if (is_regular_file (path))
77 {
78 mFilenameQueue.push (absolute (path).string ());
79 }
80 mRestart.store (true);
81 }
82
83 unique_ptr<SearchFileMatch>
84 Searcher::get ()
85 {
86 unique_ptr<SearchFileMatch> ptr;
87 mResults.pop (ptr);
88 return ptr;
89 }
90
91 void
92 Searcher::dispatch ()
93 {
94 while (mQuit.load () == false)
95 {
96 if (mRestart.load () == true)
97 {
98 this_thread::sleep_for (2s);
99
100 mFilenameQueue.clear ();
101 mResults.clear ();
102
103 mRestart.store (false);
104
105 try
106 {
107 dirFunc ();
108 }
109 catch (const exception &ext)
110 {
111 qWarning () << "search occurred error: " << ext.what ();
112 }
113 }
114 else
115 {
116 this_thread::sleep_for (1s);
117 }
118 }
119 }
120
121 void
122 Searcher::dirFunc ()
123 {
124 qDebug () << "searching " << QString::fromLocal8Bit (mOptions.mText)
125 << " from " << QString::fromLocal8Bit (mOptions.mFromDir);
126 stack<string> dirs;
127 dirs.push (mOptions.mFromDir);
128 while (!dirs.empty ())
129 {
130 if (mRestart.load () == true || mQuit.load () == true)
131 {
132 return;
133 }
134
135 auto dir = dirs.top ();
136 dirs.pop ();
137 filesystem::directory_iterator itr (dir);
138 for (const auto &entry : itr)
139 {
140 if (mRestart.load () == true || mQuit.load () == true)
141 {
142 return;
143 }
144
145 if (entry.is_directory ())
146 {
147 if (entry.path ().filename () != "."
148 && entry.path ().filename () != "..")
149 dirs.push (entry.path ().string ());
150 }
151 else if (entry.is_regular_file ())
152 {
153 auto ext = entry.path ().extension ();
154 if (ext.empty ())
155 continue;
156
157 auto titr
158 = find (mOptions.mTypes.begin (), mOptions.mTypes.end (),
159 entry.path ().extension ());
160 if (titr != mOptions.mTypes.end ())
161 {
162 mFilenameQueue.push (entry.path ().string ());
163 }
164 }
165 }
166 }
167 }
168
169 void
170 Searcher::fileLoopFunc ()
171 {
172 while (mQuit.load () == false)
173 {
174 string name;
175 if (mFilenameQueue.pop (name))
176 {
177 fileFunc (name);
178 }
179 else
180 {
181 this_thread::sleep_for (1s);
182 }
183 }
184 }
185
186 void
187 Searcher::fileFunc (const string &path)
188 {
189 auto file = FileFactory::loadFile (path);
190 if (file)
191 {
192 qDebug () << "searching for " << QString::fromLocal8Bit (path);
193 auto result = file->grepFile (mOptions.mText, mOptions.mCaseSensitive,
194 mOptions.mRegex, mRestart);
195 if (result)
196 mResults.push (std::move (result));
197 }
198 }
199
200 vector<pair<size_t, size_t>>
201 grep (const string &source, const string &text, bool is_case, bool is_regex)
202 {
203 vector<pair<size_t, size_t>> results;
204 if (is_regex == true)
205 {
206 regex regex_1{ text };
207 const sregex_token_iterator end;
208 sregex_token_iterator iter;
209 vector<string> regex_texts;
210 while ((iter
211 = regex_token_iterator (source.begin (), source.end (), regex_1))
212 != end)
213 {
214 regex_texts.push_back (iter->str ());
215 }
216 size_t pos = 0;
217 for (auto const &r_text : regex_texts)
218 {
219 pos = source.find (r_text, pos);
220 pair res{ pos, r_text.size () };
221 results.emplace_back (std::move (res));
222 }
223 }
224 else
225 {
226 auto p_source = &source;
227 auto p_text = &text;
228 if (is_case == false)
229 {
230 auto nsource = source;
231 auto ntext = text;
232 std::ranges::transform (nsource, nsource.begin (), ::tolower);
233 std::ranges::transform (ntext, ntext.begin (), ::tolower);
234 p_source = &nsource;
235 p_text = &ntext;
236 }
237
238 auto pos = p_source->find (*p_text);
239 while (pos != string::npos)
240 {
241 pair res{ pos, p_text->size () };
242 results.emplace_back (std::move (res));
243 pos = p_source->find (*p_text, pos + 1);
244 }
245 }
246
247 return results;
248 }
249
250 }