@@ -118,12 +118,12 @@ while (![file isAtEndOfStream]) { size_t length; length = [file readIntoBuffer: buffer length: of_pagesize]; - [self addItemsFromCArray: buffer - count: length]; + [self addItems: buffer + count: length]; } [self freeMemory: buffer]; } @finally { [file release]; @@ -185,11 +185,11 @@ @throw [OFInvalidFormatException exceptionWithClass: [self class]]; count >>= 1; cString = [string UTF8String]; - data = [self allocMemoryWithSize: count]; + items = [self allocMemoryWithSize: count]; for (i = 0; i < count; i++) { uint8_t c1 = cString[2 * i]; uint8_t c2 = cString[2 * i + 1]; uint8_t byte; @@ -212,11 +212,11 @@ byte |= c2 - 'A' + 10; else @throw [OFInvalidFormatException exceptionWithClass: [self class]]; - data[i] = byte; + items[i] = byte; } } @catch (id e) { [self release]; @throw e; } @@ -288,91 +288,91 @@ - (size_t)itemSize { return itemSize; } -- (void*)cArray +- (void*)items { - return data; + return items; } - (void*)itemAtIndex: (size_t)index { if (index >= count) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - return data + index * itemSize; + return items + index * itemSize; } - (void*)firstItem { - if (data == NULL || count == 0) + if (items == NULL || count == 0) return NULL; - return data; + return items; } - (void*)lastItem { - if (data == NULL || count == 0) + if (items == NULL || count == 0) return NULL; - return data + (count - 1) * itemSize; + return items + (count - 1) * itemSize; } - (void)addItem: (const void*)item { if (SIZE_MAX - count < 1) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - data = [self resizeMemory: data - size: itemSize - count: count + 1]; + items = [self resizeMemory: items + size: itemSize + count: count + 1]; - memcpy(data + count * itemSize, item, itemSize); + memcpy(items + count * itemSize, item, itemSize); count++; } - (void)insertItem: (const void*)item atIndex: (size_t)index { - [self insertItemsFromCArray: item - atIndex: index - count: 1]; -} - -- (void)addItemsFromCArray: (const void*)cArray - count: (size_t)nItems -{ - if (nItems > SIZE_MAX - count) - @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - - data = [self resizeMemory: data - size: itemSize - count: count + nItems]; - - memcpy(data + count * itemSize, cArray, nItems * itemSize); - count += nItems; -} - -- (void)insertItemsFromCArray: (const void*)cArray - atIndex: (size_t)index - count: (size_t)nItems -{ - if (nItems > SIZE_MAX - count || index > count) - @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - - data = [self resizeMemory: data - size: itemSize - count: count + nItems]; - - memmove(data + (index + nItems) * itemSize, data + index * itemSize, - (count - index) * itemSize); - memcpy(data + index * itemSize, cArray, nItems * itemSize); - - count += nItems; + [self insertItems: item + atIndex: index + count: 1]; +} + +- (void)addItems: (const void*)items_ + count: (size_t)count_ +{ + if (count_ > SIZE_MAX - count) + @throw [OFOutOfRangeException exceptionWithClass: [self class]]; + + items = [self resizeMemory: items + size: itemSize + count: count + count_]; + + memcpy(items + count * itemSize, items_, count_ * itemSize); + count += count_; +} + +- (void)insertItems: (const void*)items_ + atIndex: (size_t)index + count: (size_t)count_ +{ + if (count_ > SIZE_MAX - count || index > count) + @throw [OFOutOfRangeException exceptionWithClass: [self class]]; + + items = [self resizeMemory: items + size: itemSize + count: count + count_]; + + memmove(items + (index + count_) * itemSize, items + index * itemSize, + (count - index) * itemSize); + memcpy(items + index * itemSize, items_, count_ * itemSize); + + count += count_; } - (void)removeItemAtIndex: (size_t)index { [self removeItemsInRange: of_range(index, 1)]; @@ -382,19 +382,19 @@ { if (range.length > SIZE_MAX - range.location || range.location + range.length > count) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - memmove(data + range.location * itemSize, - data + (range.location + range.length) * itemSize, + memmove(items + range.location * itemSize, + items + (range.location + range.length) * itemSize, (count - range.location - range.length) * itemSize); count -= range.length; @try { - data = [self resizeMemory: data - size: itemSize - count: count]; + items = [self resizeMemory: items + size: itemSize + count: count]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -403,32 +403,32 @@ if (count == 0) return; count--; @try { - data = [self resizeMemory: data - size: itemSize - count: count]; + items = [self resizeMemory: items + size: itemSize + count: count]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only made it smaller */ } } - (void)removeAllItems { - [self freeMemory: data]; + [self freeMemory: items]; - data = NULL; + items = NULL; count = 0; } - copy { OFDataArray *copy = [[[self class] alloc] initWithItemSize: itemSize]; - [copy addItemsFromCArray: data - count: count]; + [copy addItems: items + count: count]; return copy; } - (BOOL)isEqual: (id)object @@ -441,11 +441,11 @@ otherDataArray = object; if ([otherDataArray count] != count || [otherDataArray itemSize] != itemSize) return NO; - if (memcmp([otherDataArray cArray], data, count * itemSize)) + if (memcmp([otherDataArray items], items, count * itemSize)) return NO; return YES; } @@ -467,11 +467,11 @@ selector: _cmd]; otherCount = [otherDataArray count]; minimumCount = (count > otherCount ? otherCount : count); - if ((comparison = memcmp(data, [otherDataArray cArray], + if ((comparison = memcmp(items, [otherDataArray items], minimumCount * itemSize)) == 0) { if (count > otherCount) return OF_ORDERED_DESCENDING; if (count < otherCount) return OF_ORDERED_ASCENDING; @@ -490,11 +490,11 @@ size_t i; OF_HASH_INIT(hash); for (i = 0; i < count * itemSize; i++) - OF_HASH_ADD(hash, ((char*)data)[i]); + OF_HASH_ADD(hash, ((uint8_t*)items)[i]); OF_HASH_FINALIZE(hash); return hash; } @@ -509,11 +509,11 @@ if (i > 0) [ret appendString: @" "]; for (j = 0; j < itemSize; j++) - [ret appendFormat: @"%02x", data[i * itemSize + j]]; + [ret appendFormat: @"%02x", items[i * itemSize + j]]; } [ret appendString: @">"]; [ret makeImmutable]; @@ -525,28 +525,28 @@ OFMutableString *ret = [OFMutableString string]; size_t i, j; for (i = 0; i < count; i++) for (j = 0; j < itemSize; j++) - [ret appendFormat: @"%02x", data[i * itemSize + j]]; + [ret appendFormat: @"%02x", items[i * itemSize + j]]; [ret makeImmutable]; return ret; } - (OFString*)stringByBase64Encoding { - return of_base64_encode(data, count * itemSize); + return of_base64_encode(items, count * itemSize); } - (void)writeToFile: (OFString*)path { OFFile *file = [[OFFile alloc] initWithPath: path mode: @"wb"]; @try { - [file writeBuffer: data + [file writeBuffer: items length: count * itemSize]; } @finally { [file release]; } } @@ -563,11 +563,11 @@ pool = objc_autoreleasePoolPush(); element = [OFXMLElement elementWithName: [self className] namespace: OF_SERIALIZATION_NS - stringValue: of_base64_encode(data, count * itemSize)]; + stringValue: of_base64_encode(items, count * itemSize)]; [element retain]; objc_autoreleasePoolPop(pool); @@ -585,62 +585,62 @@ lastPageByte = of_pagesize - 1; newSize = ((count + 1) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) - data = [self resizeMemory: data - size: newSize]; + items = [self resizeMemory: items + size: newSize]; - memcpy(data + count * itemSize, item, itemSize); + memcpy(items + count * itemSize, item, itemSize); count++; size = newSize; } -- (void)addItemsFromCArray: (const void*)cArray - count: (size_t)nItems +- (void)addItems: (const void*)items_ + count: (size_t)count_ { size_t newSize, lastPageByte; - if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize) + if (count_ > SIZE_MAX - count || count + count_ > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; lastPageByte = of_pagesize - 1; - newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; + newSize = ((count + count_) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) - data = [self resizeMemory: data - size: newSize]; + items = [self resizeMemory: items + size: newSize]; - memcpy(data + count * itemSize, cArray, nItems * itemSize); + memcpy(items + count * itemSize, items_, count_ * itemSize); - count += nItems; + count += count_; size = newSize; } -- (void)insertItemsFromCArray: (const void*)cArray - atIndex: (size_t)index - count: (size_t)nItems +- (void)insertItems: (const void*)items_ + atIndex: (size_t)index + count: (size_t)count_ { size_t newSize, lastPageByte; - if (nItems > SIZE_MAX - count || index > count || - count + nItems > SIZE_MAX / itemSize) + if (count_ > SIZE_MAX - count || index > count || + count + count_ > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; lastPageByte = of_pagesize - 1; - newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; + newSize = ((count + count_) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) - data = [self resizeMemory: data - size: newSize]; + items = [self resizeMemory: items + size: newSize]; - memmove(data + (index + nItems) * itemSize, data + index * itemSize, + memmove(items + (index + count_) * itemSize, items + index * itemSize, (count - index) * itemSize); - memcpy(data + index * itemSize, cArray, nItems * itemSize); + memcpy(items + index * itemSize, items_, count_ * itemSize); - count += nItems; + count += count_; size = newSize; } - (void)removeItemsInRange: (of_range_t)range { @@ -648,21 +648,21 @@ if (range.length > SIZE_MAX - range.location || range.location + range.length > count) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - memmove(data + range.location * itemSize, - data + (range.location + range.length) * itemSize, + memmove(items + range.location * itemSize, + items + (range.location + range.length) * itemSize, (count - range.location - range.length) * itemSize); count -= range.length; lastPageByte = of_pagesize - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) - data = [self resizeMemory: data - size: newSize]; + items = [self resizeMemory: items + size: newSize]; size = newSize; } - (void)removeLastItem { @@ -675,12 +675,12 @@ lastPageByte = of_pagesize - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) { @try { - data = [self resizeMemory: data - size: newSize]; + items = [self resizeMemory: items + size: newSize]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only made it smaller */ } size = newSize; @@ -687,12 +687,12 @@ } } - (void)removeAllItems { - [self freeMemory: data]; + [self freeMemory: items]; - data = NULL; + items = NULL; count = 0; size = 0; } @end