1/*
2 * Copyright (c) 1998-2007 Matthijs Hollemans
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22#ifndef GREPPER_H
23#define GREPPER_H
24
25#include <Messenger.h>
26
27class FileIterator;
28class Model;
29
30// Executes "grep" in a background thread.
31class Grepper {
32public:
33								Grepper(const char* pattern, const Model* model,
34									const BHandler* target,
35									FileIterator* iterator);
36	virtual						~Grepper();
37
38			bool				IsValid() const;
39
40			void				Start();
41			void				Cancel();
42
43private:
44	// Spawns the real grepper thread.
45	static	int32				_SpawnThread(void* cookie);
46
47	// The thread function that does the actual grepping.
48			int32				_GrepperThread();
49
50	// Remembers, and possibly escapes, the search pattern.
51			void				_SetPattern(const char* source);
52
53	// Prepends all quotes, dollars and backslashes with at backslash
54	// to prevent the shell from misinterpreting them.
55			bool				_EscapeSpecialChars(char* buffer,
56									ssize_t bufferSize);
57	private:
58	// The (escaped) search pattern.
59			char*				fPattern;
60
61	// The settings from the model.
62			BMessenger			fTarget;
63			bool				fEscapeText : 1;
64			bool				fCaseSensitive : 1;
65			uint32				fEncoding;
66
67	// The supplier of files to grep
68			FileIterator*		fIterator;
69
70	// Our thread's ID.
71			thread_id			fThreadId;
72
73	// Whether our thread must quit.
74	volatile bool				fMustQuit;
75};
76
77#endif // GREPPER_H
78