Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -37,10 +37,16 @@ * \param first The first object in the array * \return A new autoreleased OFArray */ + arrayWithObjects: (OFObject*)first, ...; +/** + * \param objs A C array of objects. + * \return A new autoreleased OFArray + */ ++ arrayWithCArray: (OFObject**)objs; + /** * Initializes an OFArray with the specified object. * * \param obj An object * \return An initialized OFArray @@ -63,10 +69,18 @@ * \return An initialized OFArray */ - initWithObject: (OFObject*)first andArgList: (va_list)args; +/** + * Initializes an OFArray with the objects from the specified C array. + * + * \param objs A C array of objects + * \return An initialized OFArray + */ +- initWithCArray: (OFObject**)objs; + /** * \return The number of objects in the OFArray */ - (size_t)count; Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -37,10 +37,15 @@ andArgList: args] autorelease]; va_end(args); return ret; } + ++ arrayWithCArray: (OFObject**)objs +{ + return [[[self alloc] initWithCArray: objs] autorelease]; +} - init { self = [super init]; @@ -112,10 +117,31 @@ [first retain]; for (i = 0; i < len; i++) [objs[i] retain]; + return self; +} + +- initWithCArray: (OFObject**)objs +{ + id *obj; + + self = [self init]; + + @try { + for (obj = objs; *obj != nil; obj++) + [array add: obj]; + } @catch (OFException *e) { + [self dealloc]; + @throw e; + } + + /* Retain objects after adding them for the case adding failed */ + for (obj = objs; *obj != nil; obj++) + [*obj retain]; + return self; } - (size_t)count { Index: tests/OFArray/OFArray.m ================================================================== --- tests/OFArray/OFArray.m +++ tests/OFArray/OFArray.m @@ -28,24 +28,34 @@ } @catch (exception *e) { \ puts("CAUGHT! Error string was:"); \ puts([[e string] cString]); \ puts("Resuming..."); \ } + +id c_array[] = { + @"Foo", + @"Bar", + @"Baz", + nil +}; int main() { OFArray *a = [OFArray arrayWithObjects: @"Foo", @"Bar", @"Baz", nil]; OFArray *b = [OFMutableArray array]; + OFArray *c = [OFArray arrayWithCArray: c_array]; [b add: @"Foo"]; [b add: @"Bar"]; [b add: @"Baz"]; assert([a count] == 3); assert([b count] == 3); + assert([c count] == 3); assert([a isEqual: b]); + assert([a isEqual: c]); [b removeNObjects: 1]; [b add: @"Baz"]; assert([a isEqual: b]);