+ Updated all the parsing code to use std::strings everywhere
instead of const char*'s to allow for rules with NULL characters embedded in them. + Updated Rule::Sniff() to actually return a useful value git-svn-id: file:///srv/svn/repos/haiku/trunk/current@662 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -43,23 +43,24 @@ status_t parse(const char *rule, Rule *result, BString *parseError = NULL);
|
|||||||
*/
|
*/
|
||||||
class CharStream {
|
class CharStream {
|
||||||
public:
|
public:
|
||||||
CharStream(const char *string = NULL);
|
CharStream(const std::string &string);
|
||||||
|
CharStream();
|
||||||
~CharStream();
|
~CharStream();
|
||||||
|
|
||||||
status_t SetTo(const char *string);
|
status_t SetTo(const std::string &string);
|
||||||
void Unset();
|
void Unset();
|
||||||
status_t InitCheck() const;
|
status_t InitCheck() const;
|
||||||
bool IsEmpty() const;
|
bool IsEmpty() const;
|
||||||
ssize_t Pos() const;
|
ssize_t Pos() const;
|
||||||
const char *String() const;
|
const std::string& String() const;
|
||||||
|
|
||||||
char Get();
|
char Get();
|
||||||
void Unget();
|
void Unget();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char *fString;
|
std::string fString;
|
||||||
ssize_t fPos;
|
ssize_t fPos;
|
||||||
ssize_t fLen;
|
// ssize_t fLen;
|
||||||
status_t fCStatus;
|
status_t fCStatus;
|
||||||
|
|
||||||
CharStream(const CharStream &ref);
|
CharStream(const CharStream &ref);
|
||||||
@@ -95,9 +96,8 @@ const char* tokenTypeToString(TokenType type);
|
|||||||
class Token {
|
class Token {
|
||||||
public:
|
public:
|
||||||
Token(TokenType type = EmptyToken, const ssize_t pos = -1);
|
Token(TokenType type = EmptyToken, const ssize_t pos = -1);
|
||||||
virtual ~Token();
|
|
||||||
TokenType Type() const;
|
TokenType Type() const;
|
||||||
virtual const char* String() const;
|
virtual const std::string& String() const;
|
||||||
virtual int32 Int() const;
|
virtual int32 Int() const;
|
||||||
virtual double Float() const;
|
virtual double Float() const;
|
||||||
ssize_t Pos() const;
|
ssize_t Pos() const;
|
||||||
@@ -114,11 +114,10 @@ protected:
|
|||||||
*/
|
*/
|
||||||
class StringToken : public Token {
|
class StringToken : public Token {
|
||||||
public:
|
public:
|
||||||
StringToken(const char *string, const ssize_t pos);
|
StringToken(const std::string &str, const ssize_t pos);
|
||||||
virtual ~StringToken();
|
virtual const std::string& String() const;
|
||||||
virtual const char* String() const;
|
|
||||||
protected:
|
protected:
|
||||||
char *fString;
|
std::string fString;
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Integer token class
|
//! Integer token class
|
||||||
@@ -157,10 +156,11 @@ protected:
|
|||||||
*/
|
*/
|
||||||
class TokenStream {
|
class TokenStream {
|
||||||
public:
|
public:
|
||||||
TokenStream(const char *string = NULL);
|
TokenStream(const std::string &string);
|
||||||
|
TokenStream();
|
||||||
~TokenStream();
|
~TokenStream();
|
||||||
|
|
||||||
status_t SetTo(const char *string);
|
status_t SetTo(const std::string &string);
|
||||||
void Unset();
|
void Unset();
|
||||||
status_t InitCheck() const;
|
status_t InitCheck() const;
|
||||||
|
|
||||||
@@ -177,7 +177,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void AddToken(TokenType type, ssize_t pos);
|
void AddToken(TokenType type, ssize_t pos);
|
||||||
void AddString(const char *str, ssize_t pos);
|
void AddString(const std::string &str, ssize_t pos);
|
||||||
void AddInt(const char *str, ssize_t pos);
|
void AddInt(const char *str, ssize_t pos);
|
||||||
void AddFloat(const char *str, ssize_t pos);
|
void AddFloat(const char *str, ssize_t pos);
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ class Err;
|
|||||||
/*! The byte string and mask (if supplied) must be of the same length. */
|
/*! The byte string and mask (if supplied) must be of the same length. */
|
||||||
class Pattern {
|
class Pattern {
|
||||||
public:
|
public:
|
||||||
Pattern(const char *string, const char *mask = NULL);
|
Pattern(const std::string &string, const std::string &mask);
|
||||||
|
Pattern(const std::string &string);
|
||||||
~Pattern();
|
~Pattern();
|
||||||
|
|
||||||
status_t InitCheck() const;
|
status_t InitCheck() const;
|
||||||
@@ -31,7 +32,7 @@ public:
|
|||||||
|
|
||||||
bool Sniff(Range range, BPositionIO *data) const;
|
bool Sniff(Range range, BPositionIO *data) const;
|
||||||
|
|
||||||
status_t SetTo(const char *string, const char *mask = NULL);
|
status_t SetTo(const std::string &string, const std::string &mask);
|
||||||
private:
|
private:
|
||||||
bool Sniff(off_t start, off_t size, BPositionIO *data) const;
|
bool Sniff(off_t start, off_t size, BPositionIO *data) const;
|
||||||
|
|
||||||
|
|||||||
+441
-448
File diff suppressed because it is too large
Load Diff
@@ -14,13 +14,25 @@
|
|||||||
|
|
||||||
using namespace Sniffer;
|
using namespace Sniffer;
|
||||||
|
|
||||||
Pattern::Pattern(const char *string, const char *mask = NULL)
|
Pattern::Pattern(const std::string &string, const std::string &mask)
|
||||||
: fCStatus(B_NO_INIT)
|
: fCStatus(B_NO_INIT)
|
||||||
, fErrorMessage(NULL)
|
, fErrorMessage(NULL)
|
||||||
{
|
{
|
||||||
SetTo(string, mask);
|
SetTo(string, mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Pattern::Pattern(const std::string &string)
|
||||||
|
: fCStatus(B_NO_INIT)
|
||||||
|
, fErrorMessage(NULL)
|
||||||
|
{
|
||||||
|
// Build a mask with all bits turned on of the
|
||||||
|
// appropriate length
|
||||||
|
std::string mask = "";
|
||||||
|
for (int i = 0; i < string.length(); i++)
|
||||||
|
mask += (char)0xFF;
|
||||||
|
SetTo(string, mask);
|
||||||
|
}
|
||||||
|
|
||||||
Pattern::~Pattern() {
|
Pattern::~Pattern() {
|
||||||
delete fErrorMessage;
|
delete fErrorMessage;
|
||||||
}
|
}
|
||||||
@@ -38,29 +50,28 @@ Pattern::GetErr() const {
|
|||||||
return new(nothrow) Err(*fErrorMessage);
|
return new(nothrow) Err(*fErrorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void dumpStr(const std::string &string, const char *label = NULL) {
|
||||||
|
if (label)
|
||||||
|
printf("%s: ", label);
|
||||||
|
for (int i = 0; i < string.length(); i++)
|
||||||
|
printf("%x ", string[i]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
Pattern::SetTo(const char *string, const char *mask) {
|
Pattern::SetTo(const std::string &string, const std::string &mask) {
|
||||||
if (string) {
|
fString = string;
|
||||||
fString = string;
|
if (fString.length() == 0) {
|
||||||
if (fString.length() == 0) {
|
SetStatus(B_BAD_VALUE, "Sniffer pattern error: illegal empty pattern");
|
||||||
SetStatus(B_BAD_VALUE, "Sniffer pattern error: illegal empty pattern");
|
|
||||||
} else {
|
|
||||||
if (mask) {
|
|
||||||
fMask = mask;
|
|
||||||
if (fString.length() != fMask.length()) {
|
|
||||||
SetStatus(B_BAD_VALUE, "Sniffer pattern error: pattern and mask lengths do not match");
|
|
||||||
} else {
|
|
||||||
SetStatus(B_OK);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fMask = "";
|
|
||||||
for (int i = 0; i < fString.length(); i++)
|
|
||||||
fMask += (char)0xFF;
|
|
||||||
SetStatus(B_OK);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
SetStatus(B_BAD_VALUE, "Sniffer parser error: NULL string parameter passed to Pattern::SetTo()");
|
fMask = mask;
|
||||||
|
// dumpStr(string, "data");
|
||||||
|
// dumpStr(mask, "mask");
|
||||||
|
if (fString.length() != fMask.length()) {
|
||||||
|
SetStatus(B_BAD_VALUE, "Sniffer pattern error: pattern and mask lengths do not match");
|
||||||
|
} else {
|
||||||
|
SetStatus(B_OK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,6 +93,7 @@ Pattern::Sniff(Range range, BPositionIO *data) const {
|
|||||||
if (Sniff(i, size, data))
|
if (Sniff(i, size, data))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assumes the BPositionIO object is in the correct
|
// Assumes the BPositionIO object is in the correct
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ Rule::Sniff(BPositionIO *data) const {
|
|||||||
if (*i)
|
if (*i)
|
||||||
result &= (*i)->Sniff(data);
|
result &= (*i)->Sniff(data);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user