#include <stdio.h>
#include "Err.h"
#include "Pattern.h"
#include "PatternList.h"
using namespace BPrivate::Storage::Sniffer;
PatternList::PatternList(Range range)
:
DisjList(),
fRange(range)
{
}
PatternList::~PatternList()
{
std::vector<Pattern*>::iterator i;
for (i = fList.begin(); i != fList.end(); i++)
delete *i;
}
status_t
PatternList::InitCheck() const
{
return fRange.InitCheck();
}
Err*
PatternList::GetErr() const
{
return fRange.GetErr();
}
bool
PatternList::Sniff(const Data& data) const
{
if (InitCheck() != B_OK) {
return false;
} else {
bool result = false;
std::vector<Pattern*>::const_iterator i;
for (i = fList.begin(); i != fList.end(); i++) {
if (*i)
result |= (*i)->Sniff(fRange, data, fCaseInsensitive);
}
return result;
}
}
ssize_t
PatternList::BytesNeeded() const
{
ssize_t result = InitCheck();
if (result == B_OK) {
result = 0;
std::vector<Pattern*>::const_iterator i;
for (i = fList.begin(); i != fList.end(); i++) {
if (*i) {
ssize_t bytes = (*i)->BytesNeeded();
if (bytes >= 0) {
if (bytes > result)
result = bytes;
} else {
result = bytes;
break;
}
}
}
}
if (result >= 0)
result += fRange.End();
return result;
}
void
PatternList::Add(Pattern* pattern)
{
if (pattern)
fList.push_back(pattern);
}