xref: /haiku/src/kits/storage/sniffer/RPatternList.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 //----------------------------------------------------------------------
2 //  This software is part of the Haiku distribution and is covered
3 //  by the MIT License.
4 //---------------------------------------------------------------------
5 /*!
6 	\file RPatternList.cpp
7 	MIME sniffer rpattern list implementation
8 */
9 
10 #include <sniffer/Err.h>
11 #include <sniffer/RPattern.h>
12 #include <sniffer/RPatternList.h>
13 #include <DataIO.h>
14 #include <stdio.h>
15 
16 using namespace BPrivate::Storage::Sniffer;
17 
18 RPatternList::RPatternList()
19 	: DisjList()
20 {
21 }
22 
23 RPatternList::~RPatternList() {
24 	// Clear our rpattern list
25 	std::vector<RPattern*>::iterator i;
26 	for (i = fList.begin(); i != fList.end(); i++)
27 		delete *i;
28 }
29 
30 status_t
31 RPatternList::InitCheck() const {
32 	return B_OK;
33 }
34 
35 Err*
36 RPatternList::GetErr() const {
37 	return NULL;
38 }
39 
40 /*! Sniffs the given data stream, searching for a match
41 	with any of the list's patterns. Each pattern is searched
42 	over its own specified range.
43 */
44 bool
45 RPatternList::Sniff(BPositionIO *data) const {
46 	if (InitCheck() != B_OK)
47 		return false;
48 	else {
49 		bool result = false;
50 		std::vector<RPattern*>::const_iterator i;
51 		for (i = fList.begin(); i != fList.end(); i++) {
52 			if (*i)
53 				result |= (*i)->Sniff(data, fCaseInsensitive);
54 		}
55 		return result;
56 	}
57 }
58 
59 /*! \brief Returns the number of bytes needed to perform a complete sniff, or an error
60 	code if something goes wrong.
61 */
62 ssize_t
63 RPatternList::BytesNeeded() const
64 {
65 	ssize_t result = InitCheck();
66 
67 	// Tally up the BytesNeeded() values for all the RPatterns and return the largest.
68 	if (result == B_OK) {
69 		result = 0; // Just to be safe...
70 		std::vector<RPattern*>::const_iterator i;
71 		for (i = fList.begin(); i != fList.end(); i++) {
72 			if (*i) {
73 				ssize_t bytes = (*i)->BytesNeeded();
74 				if (bytes >= 0) {
75 					if (bytes > result)
76 						result = bytes;
77 				} else {
78 					result = bytes;
79 					break;
80 				}
81 			}
82 		}
83 	}
84 	return result;
85 }
86 
87 void
88 RPatternList::Add(RPattern *rpattern) {
89 	if (rpattern)
90 		fList.push_back(rpattern);
91 }
92 
93 
94 
95