* ArgumentList now inherits from BObjectList instead of aggregating it.

* Fixed BString::ICompare() checks.
* Minor other improvements.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43144 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2011-11-02 23:15:07 +00:00
parent 9718a172b0
commit 51b5d92eda
2 changed files with 68 additions and 76 deletions

View File

@ -12,7 +12,7 @@ namespace IMAP {
ArgumentList::ArgumentList()
:
fArguments(5, true)
BObjectList<Argument>(5, true)
{
}
@ -22,12 +22,26 @@ ArgumentList::~ArgumentList()
}
bool
ArgumentList::Contains(const char* string) const
{
for (int32 i = 0; i < CountItems(); i++) {
if (StringArgument* argument
= dynamic_cast<StringArgument*>(ItemAt(i))) {
if (argument->String().ICompare(string) == 0)
return true;
}
}
return false;
}
BString
ArgumentList::StringAt(int32 index) const
{
if (index >= 0 && index < fArguments.CountItems()) {
if (StringArgument* argument = dynamic_cast<StringArgument*>(
fArguments.ItemAt(index)))
if (index >= 0 && index < CountItems()) {
if (StringArgument* argument
= dynamic_cast<StringArgument*>(ItemAt(index)))
return argument->String();
}
return "";
@ -37,8 +51,8 @@ ArgumentList::StringAt(int32 index) const
bool
ArgumentList::IsStringAt(int32 index) const
{
if (index >= 0 && index < fArguments.CountItems()) {
if (dynamic_cast<StringArgument*>(fArguments.ItemAt(index)) != NULL)
if (index >= 0 && index < CountItems()) {
if (dynamic_cast<StringArgument*>(ItemAt(index)) != NULL)
return true;
}
return false;
@ -48,20 +62,19 @@ ArgumentList::IsStringAt(int32 index) const
bool
ArgumentList::EqualsAt(int32 index, const char* string) const
{
return StringAt(index).ICompare(string);
return StringAt(index).ICompare(string) == 0;
}
const ArgumentList&
ArgumentList&
ArgumentList::ListAt(int32 index) const
{
if (index >= 0 && index < fArguments.CountItems()) {
if (ListArgument* argument = dynamic_cast<ListArgument*>(
fArguments.ItemAt(index)))
if (index >= 0 && index < CountItems()) {
if (ListArgument* argument = dynamic_cast<ListArgument*>(ItemAt(index)))
return argument->List();
}
static ArgumentList empty(0, true);
static ArgumentList empty;
return empty;
}
@ -69,9 +82,8 @@ ArgumentList::ListAt(int32 index) const
bool
ArgumentList::IsListAt(int32 index) const
{
if (index >= 0 && index < fArguments.CountItems()) {
if (ListArgument* argument = dynamic_cast<ListArgument*>(
fArguments.ItemAt(index)))
if (index >= 0 && index < CountItems()) {
if (ListArgument* argument = dynamic_cast<ListArgument*>(ItemAt(index)))
return true;
}
return false;
@ -81,9 +93,8 @@ ArgumentList::IsListAt(int32 index) const
bool
ArgumentList::IsListAt(int32 index, char kind) const
{
if (index >= 0 && index < fArguments.CountItems()) {
if (ListArgument* argument = dynamic_cast<ListArgument*>(
fArguments.ItemAt(index)))
if (index >= 0 && index < CountItems()) {
if (ListArgument* argument = dynamic_cast<ListArgument*>(ItemAt(index)))
return argument->Kind() == kind;
}
return false;
@ -109,6 +120,20 @@ ArgumentList::IsIntegerAt(int32 index) const
}
BString
ArgumentList::ToString() const
{
BString string;
for (int32 i = 0; i < CountItems(); i++) {
if (i > 0)
string += ", ";
string += ItemAt(i)->ToString();
}
return string;
}
// #pragma mark -
@ -122,40 +147,12 @@ Argument::~Argument()
}
/*static*/ BString
Argument::ToString(const ArgumentList& arguments)
{
BString string;
for (int32 i = 0; i < arguments.CountItems(); i++) {
if (i > 0)
string += ", ";
string += arguments.ItemAt(i)->ToString();
}
return string;
}
bool
Argument::Contains(const ArgumentList& arguments, const char* string) const
{
for (int32 i = 0; i < arguments.CountItems(); i++) {
if (StringArgument* argument = dynamic_cast<StringArgument*>(
arguments.ItemAt(i))) {
if (argument->String().ICompare(string))
return true;
}
}
return false;
}
// #pragma mark -
ListArgument::ListArgument()
ListArgument::ListArgument(char kind)
:
fList(5, true)
fKind(kind)
{
}
@ -163,9 +160,9 @@ ListArgument::ListArgument()
BString
ListArgument::ToString() const
{
BString string("(");
string += Argument::ToString(response.Arguments());
string += ")";
BString string(fKind == '[' ? "[" : "(");
string += fList.ToString();
string += fKind == '[' ? "]" : ")";
return string;
}
@ -181,6 +178,13 @@ StringArgument::StringArgument(const BString& string)
}
StringArgument::StringArgument(const StringArgument& other)
:
fString(other.fString)
{
}
BString
StringArgument::ToString() const
{
@ -227,8 +231,7 @@ ExpectedParseException::ExpectedParseException(char expected, char instead)
Response::Response()
:
fTag(0),
fArguments(5, true),
fContinued(false)
fContinuation(false)
{
}
@ -243,7 +246,7 @@ Response::SetTo(const char* line) throw(ParseException)
{
MakeEmpty();
fTag = 0;
fContinued = false;
fContinuation = false;
if (line[0] == '*') {
// Untagged response
@ -252,7 +255,7 @@ Response::SetTo(const char* line) throw(ParseException)
} else if (line[0] == '+') {
// Continuation
Consume(line, '+');
fContinued = true;
fContinuation = true;
} else {
// Tagged response
Consume(line, 'A');
@ -262,7 +265,7 @@ Response::SetTo(const char* line) throw(ParseException)
Consume(line, ' ');
}
char c = ParseLine(this, line);
char c = ParseLine(*this, line);
if (c != '\0')
throw ExpectedParseException('\0', c);
}
@ -271,7 +274,7 @@ Response::SetTo(const char* line) throw(ParseException)
bool
Response::IsCommand(const char* command) const
{
return IsStringAt(0, command);
return IsUntagged() && EqualsAt(0, command);
}

View File

@ -18,37 +18,25 @@ namespace IMAP {
class Argument;
class ArgumentList {
class ArgumentList : public BObjectList<Argument> {
public:
ArgumentList();
~ArgumentList();
size_t CountItems()
{ return (size_t)fArguments.CountItems(); }
Argument& ItemAt(size_t index) const
{ return *fArguments.ItemAt(index); }
void MakeEmpty()
{ fArguments.MakeEmpty(); }
bool AddItem(Argument* argument)
{ return fArguments.AddItem(argument); }
Argument* RemoveItem(size_t index)
{ return fArguments.RemoveItemAt(index); }
BString ToString() const;
bool Contains(const char* string) const;
BString StringAt(int32 index) const;
bool IsStringAt(int32 index) const;
bool EqualsAt(int32 index,
const char* string) const;
const ArgumentList& ListAt(int32 index) const;
ArgumentList& ListAt(int32 index) const;
bool IsListAt(int32 index) const;
bool IsListAt(int32 index, char kind) const;
int32 IntegerAt(int32 index) const;
bool IsIntegerAt(int32 index) const;
private:
BObjectList<Argument> fArguments;
BString ToString() const;
};
@ -79,6 +67,7 @@ private:
class StringArgument : public Argument {
public:
StringArgument(const BString& string);
StringArgument(const StringArgument& other);
const BString& String() { return fString; }
@ -122,7 +111,7 @@ public:
bool IsUntagged() const { return fTag == 0; }
int32 Tag() const { return fTag; }
bool IsCommand(const char* command) const;
bool IsContinued() const { return fContinued; }
bool IsContinuation() const { return fContinuation; }
protected:
char ParseLine(ArgumentList& arguments,
@ -140,7 +129,7 @@ protected:
protected:
int32 fTag;
bool fContinued;
bool fContinuation;
};