diff --git a/headers/os/support/StringList.h b/headers/os/support/StringList.h index dba5edf70a..c067d3c5fe 100644 --- a/headers/os/support/StringList.h +++ b/headers/os/support/StringList.h @@ -7,11 +7,12 @@ #include +#include #include #include -class BStringList { +class BStringList : public BFlattenable { public: BStringList(int32 count = 20); BStringList(const BStringList& other); @@ -25,6 +26,8 @@ public: bool Remove(const BString& string, bool ignoreCase = false); + void Remove(const BStringList& list, + bool ignoreCase = false); BString Remove(int32 index); bool Remove(int32 index, int32 count); bool Replace(int32 index, const BString& string); @@ -59,6 +62,15 @@ public: bool operator==(const BStringList& other) const; bool operator!=(const BStringList& other) const; + // BFlattenable + virtual bool IsFixedSize() const; + virtual type_code TypeCode() const; + virtual bool AllowsTypeCode(type_code code) const; + virtual ssize_t FlattenedSize() const; + virtual status_t Flatten(void* buffer, ssize_t size) const; + virtual status_t Unflatten(type_code code, const void* buffer, + ssize_t size); + private: void _IncrementRefCounts() const; void _DecrementRefCounts() const; diff --git a/headers/os/support/TypeConstants.h b/headers/os/support/TypeConstants.h index 67cd0de4a6..9272b04cdf 100644 --- a/headers/os/support/TypeConstants.h +++ b/headers/os/support/TypeConstants.h @@ -52,6 +52,7 @@ enum { B_SIZE_T_TYPE = 'SIZT', B_SSIZE_T_TYPE = 'SSZT', B_STRING_TYPE = 'CSTR', + B_STRING_LIST_TYPE = 'STRL', B_TIME_TYPE = 'TIME', B_UINT16_TYPE = 'USHT', B_UINT32_TYPE = 'ULNG', diff --git a/src/kits/support/StringList.cpp b/src/kits/support/StringList.cpp index 9e0dc8ce22..10c196ef3e 100644 --- a/src/kits/support/StringList.cpp +++ b/src/kits/support/StringList.cpp @@ -5,6 +5,7 @@ #include +#include #include @@ -126,6 +127,14 @@ BStringList::Remove(const BString& string, bool ignoreCase) } +void +BStringList::Remove(const BStringList& list, bool ignoreCase) +{ + for (int32 i = 0; i < list.CountStrings(); i++) + Remove(list.StringAt(i), ignoreCase); +} + + BString BStringList::Remove(int32 index) { @@ -233,16 +242,16 @@ BStringList::IndexOf(const BString& string, bool ignoreCase) const for (int32 i = 0; i < count; i++) { BString element(StringAt(i)); if (length == element.Length() && string.ICompare(element) == 0) - return true; + return i; } } else { for (int32 i = 0; i < count; i++) { if (string == StringAt(i)) - return true; + return i; } } - return false; + return -1; } @@ -311,6 +320,76 @@ BStringList::operator==(const BStringList& other) const } +bool +BStringList::IsFixedSize() const +{ + return false; +} + + +type_code +BStringList::TypeCode() const +{ + return B_STRING_LIST_TYPE; +} + + + +bool +BStringList::AllowsTypeCode(type_code code) const +{ + return code == B_STRING_LIST_TYPE; +} + + +ssize_t +BStringList::FlattenedSize() const +{ + ssize_t size = 0; + for (int32 i = 0; i < CountStrings(); i++) { + const char* str = StringAt(i).String(); + size += strlen(str) + 1; + } + + return size; +} + + +status_t +BStringList::Flatten(void* buffer, ssize_t size) const +{ + if (size < FlattenedSize()) + return B_NO_MEMORY; + + for (int32 i = 0; i < CountStrings(); i++) { + const char* str = StringAt(i).String(); + ssize_t storeSize = strlen(str) + 1; + memcpy(buffer, str, storeSize); + buffer = (void*)((const char*)buffer + storeSize); + } + + return B_OK; +} + + +status_t +BStringList::Unflatten(type_code code, const void* buffer, ssize_t size) +{ + if (code != B_STRING_LIST_TYPE) + return B_ERROR; + + const char* str = (const char*)buffer; + for (off_t offset = 0; offset < size; offset ++) { + if (((int8*)buffer)[offset] == 0) { + Add(str); + str = (const char*)buffer + offset + 1; + } + } + + return B_OK; +} + + void BStringList::_IncrementRefCounts() const {