From 51b5d92eda4464632f1cc42b97ac691d7f8e9377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 2 Nov 2011 23:15:07 +0000 Subject: [PATCH] * 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 --- .../imap/imap_lib/Response.cpp | 117 +++++++++--------- .../imap/imap_lib/Response.h | 27 ++-- 2 files changed, 68 insertions(+), 76 deletions(-) diff --git a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Response.cpp b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Response.cpp index 0454883c8d..de3a2ea19e 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Response.cpp +++ b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Response.cpp @@ -12,7 +12,7 @@ namespace IMAP { ArgumentList::ArgumentList() : - fArguments(5, true) + BObjectList(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(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( - fArguments.ItemAt(index))) + if (index >= 0 && index < CountItems()) { + if (StringArgument* argument + = dynamic_cast(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(fArguments.ItemAt(index)) != NULL) + if (index >= 0 && index < CountItems()) { + if (dynamic_cast(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( - fArguments.ItemAt(index))) + if (index >= 0 && index < CountItems()) { + if (ListArgument* argument = dynamic_cast(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( - fArguments.ItemAt(index))) + if (index >= 0 && index < CountItems()) { + if (ListArgument* argument = dynamic_cast(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( - fArguments.ItemAt(index))) + if (index >= 0 && index < CountItems()) { + if (ListArgument* argument = dynamic_cast(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( - 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); } diff --git a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Response.h b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Response.h index f6201970c7..7ca0916ef2 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Response.h +++ b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Response.h @@ -18,37 +18,25 @@ namespace IMAP { class Argument; -class ArgumentList { +class ArgumentList : public BObjectList { 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 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; };