Make BStringList derive from BFlattenable. The implementation is more or less copied from the mail version to stay compatible. Fix IndexOf which

was the implementation of HasString.
This commit is contained in:
czeidler 2011-12-15 19:34:45 +13:00
parent 5e4d3774eb
commit 779061f0cd
3 changed files with 96 additions and 4 deletions

View File

@ -7,11 +7,12 @@
#include <BeBuild.h>
#include <Flattenable.h>
#include <List.h>
#include <String.h>
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;

View File

@ -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',

View File

@ -5,6 +5,7 @@
#include <StringList.h>
#include <TypeConstants.h>
#include <algorithm>
@ -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
{