Debian Codegraph @

src/ApvlvSearch.h

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.h
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #ifndef _APVLV_SEARCH_H_
29 #define _APVLV_SEARCH_H_
30
31 #include <atomic>
32 #include <string>
33 #include <thread>
34 #include <vector>
35
36 #include "ApvlvQueue.h"
37
38 namespace apvlv
39 {
40
41 struct SearchMatch
42 {
43 std::string match;
44 std::string line;
45 size_t pos;
46 size_t length;
47 };
48
49 using SearchMatchList = std::vector<SearchMatch>;
50
51 struct SearchPageMatch
52 {
53 int page;
54 SearchMatchList matches;
55 };
56
57 struct SearchFileMatch
58 {
59 std::string filename;
60 std::vector<SearchPageMatch> page_matches;
61 };
62
63 class SearchOptions
64 {
65 public:
66 friend bool
67 operator== (SearchOptions const &opt, SearchOptions const &other)
68 {
69 return opt.mText == other.mText
70 && opt.mCaseSensitive != other.mCaseSensitive
71 && opt.mRegex != other.mRegex && opt.mTypes != other.mTypes
72 && opt.mFromDir != other.mFromDir;
73 }
74
75 std::string mText;
76 bool mCaseSensitive;
77 bool mRegex;
78 std::string mFromDir;
79 std::vector<std::string> mTypes;
80 };
81
82 class Searcher
83 {
84 public:
85 Searcher ();
86 ~Searcher ();
87
88 void submit (const SearchOptions &options);
89 std::unique_ptr<SearchFileMatch> get ();
90
91 private:
92 void dispatch ();
93 void dirFunc ();
94 void fileLoopFunc ();
95 void fileFunc (const std::string &path);
96
97 std::vector<std::thread> mTasks;
98
99 SearchOptions mOptions;
100 LockQueue<std::string> mFilenameQueue;
101 LockQueue<std::unique_ptr<SearchFileMatch>> mResults;
102 std::atomic<bool> mRestart;
103 std::atomic<bool> mQuit;
104 };
105
106 std::vector<std::pair<size_t, size_t>> grep (const std::string &source,
107 const std::string &text,
108 bool is_case, bool is_regex);
109
110 }
111
112 #endif