1 //---------------------------------------------------------------------- 2 // This software is part of the Haiku distribution and is covered 3 // by the MIT License. 4 //--------------------------------------------------------------------- 5 /*! 6 \file RPattern.cpp 7 MIME sniffer rpattern implementation 8 */ 9 10 #include <sniffer/Err.h> 11 #include <sniffer/Pattern.h> 12 #include <sniffer/Range.h> 13 #include <sniffer/RPattern.h> 14 #include <DataIO.h> 15 16 using namespace BPrivate::Storage::Sniffer; 17 18 RPattern::RPattern(Range range, Pattern *pattern) 19 : fRange(range) 20 , fPattern(pattern) 21 { 22 } 23 24 status_t 25 RPattern::InitCheck() const { 26 status_t err = fRange.InitCheck(); 27 if (!err) 28 err = fPattern ? B_OK : B_BAD_VALUE; 29 if (!err) 30 err = fPattern->InitCheck(); 31 return err; 32 } 33 34 Err* 35 RPattern::GetErr() const { 36 if (fRange.InitCheck() != B_OK) 37 return fRange.GetErr(); 38 else if (fPattern) { 39 if (fPattern->InitCheck() != B_OK) 40 return fPattern->GetErr(); 41 else 42 return NULL; 43 } else 44 return new Err("Sniffer parser error: RPattern::RPattern() -- NULL pattern parameter", -1); 45 } 46 47 RPattern::~RPattern() { 48 delete fPattern; 49 } 50 51 //! Sniffs the given data stream over the object's range for the object's pattern 52 bool 53 RPattern::Sniff(BPositionIO *data, bool caseInsensitive) const { 54 if (!data || InitCheck() != B_OK) 55 return false; 56 else 57 return fPattern->Sniff(fRange, data, caseInsensitive); 58 } 59 60 /*! \brief Returns the number of bytes needed to perform a complete sniff, or an error 61 code if something goes wrong. 62 */ 63 ssize_t 64 RPattern::BytesNeeded() const 65 { 66 ssize_t result = InitCheck(); 67 if (result == B_OK) 68 result = fPattern->BytesNeeded(); 69 if (result >= 0) 70 result += fRange.End(); 71 return result; 72 } 73 74 75