Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -298,10 +298,12 @@ 4BB25E8B139C388A00F574EA /* OFString+Serialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BB25E85139C388A00F574EA /* OFString+Serialization.m */; }; 4BB25E8C139C388A00F574EA /* OFXMLElement+Serialization.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BB25E86139C388A00F574EA /* OFXMLElement+Serialization.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4BB25E8D139C388A00F574EA /* OFXMLElement+Serialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BB25E87139C388A00F574EA /* OFXMLElement+Serialization.m */; }; 4BB524C1143D1E4E0085FBCC /* OFProcess.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BB524BF143D1E4E0085FBCC /* OFProcess.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4BB524C2143D1E4E0085FBCC /* OFProcess.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BB524C0143D1E4E0085FBCC /* OFProcess.m */; settings = {ATTRIBUTES = (Public, ); }; }; + 4BC090441584F6760040640F /* OFInvalidJSONException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BC090421584F6760040640F /* OFInvalidJSONException.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4BC090451584F6760040640F /* OFInvalidJSONException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BC090431584F6760040640F /* OFInvalidJSONException.m */; }; 4BD653C5143B8489006182F0 /* OFTCPSocket+SOCKS5.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BD653C3143B8489006182F0 /* OFTCPSocket+SOCKS5.h */; }; 4BD653C6143B8489006182F0 /* OFTCPSocket+SOCKS5.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BD653C4143B8489006182F0 /* OFTCPSocket+SOCKS5.m */; }; 4BD98C03133814220048DD5B /* objfw-defs.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BD98C011338140B0048DD5B /* objfw-defs.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4BDF37B51338055600F9A81A /* config.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BDF37B41338055600F9A81A /* config.h */; }; 4BF33AFB133807590059CEF7 /* ObjFW.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B3D23761337FBC800DD29B8 /* ObjFW.framework */; }; @@ -661,10 +663,12 @@ 4BB50DD012F863C700C9393F /* of_asprintf.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = of_asprintf.m; path = src/of_asprintf.m; sourceTree = SOURCE_ROOT; }; 4BB524BF143D1E4E0085FBCC /* OFProcess.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFProcess.h; path = src/OFProcess.h; sourceTree = ""; }; 4BB524C0143D1E4E0085FBCC /* OFProcess.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFProcess.m; path = src/OFProcess.m; sourceTree = ""; }; 4BBA36C411406AB700CBA3AC /* atomic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = atomic.h; path = src/atomic.h; sourceTree = ""; }; 4BBA36C511406AB700CBA3AC /* macros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = macros.h; path = src/macros.h; sourceTree = ""; }; + 4BC090421584F6760040640F /* OFInvalidJSONException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFInvalidJSONException.h; path = src/exceptions/OFInvalidJSONException.h; sourceTree = ""; }; + 4BC090431584F6760040640F /* OFInvalidJSONException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFInvalidJSONException.m; path = src/exceptions/OFInvalidJSONException.m; sourceTree = ""; }; 4BD653C3143B8489006182F0 /* OFTCPSocket+SOCKS5.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "OFTCPSocket+SOCKS5.h"; path = "src/OFTCPSocket+SOCKS5.h"; sourceTree = ""; }; 4BD653C4143B8489006182F0 /* OFTCPSocket+SOCKS5.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "OFTCPSocket+SOCKS5.m"; path = "src/OFTCPSocket+SOCKS5.m"; sourceTree = ""; }; 4BD86D801237A6C600ED9912 /* OFBlock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFBlock.h; path = src/OFBlock.h; sourceTree = SOURCE_ROOT; }; 4BD86D811237A6C600ED9912 /* OFBlock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFBlock.m; path = src/OFBlock.m; sourceTree = SOURCE_ROOT; }; 4BD98C011338140B0048DD5B /* objfw-defs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objfw-defs.h"; path = "src/objfw-defs.h"; sourceTree = SOURCE_ROOT; }; @@ -792,10 +796,12 @@ 4B17FF92133A317B003E6DCD /* OFInvalidArgumentException.m */, 4B17FF97133A3242003E6DCD /* OFInvalidEncodingException.h */, 4B17FF98133A3243003E6DCD /* OFInvalidEncodingException.m */, 4B17FF9B133A32B7003E6DCD /* OFInvalidFormatException.h */, 4B17FF9C133A32B8003E6DCD /* OFInvalidFormatException.m */, + 4BC090421584F6760040640F /* OFInvalidJSONException.h */, + 4BC090431584F6760040640F /* OFInvalidJSONException.m */, 4B17FFB7133A380F003E6DCD /* OFInvalidServerReplyException.h */, 4B17FFB8133A3813003E6DCD /* OFInvalidServerReplyException.m */, 4B29BC5D133AC9C40004B236 /* OFLinkFailedException.h */, 4B29BC5E133AC9C40004B236 /* OFLinkFailedException.m */, 4B90B79C133AD87D00BD33CB /* OFListenFailedException.h */, @@ -1229,10 +1235,11 @@ 4B17FFAD133A3591003E6DCD /* OFHTTPRequestFailedException.h in Headers */, 4B17FFA5133A3411003E6DCD /* OFInitializationFailedException.h in Headers */, 4B17FF93133A317C003E6DCD /* OFInvalidArgumentException.h in Headers */, 4B17FF99133A3245003E6DCD /* OFInvalidEncodingException.h in Headers */, 4B17FF9D133A32BA003E6DCD /* OFInvalidFormatException.h in Headers */, + 4BC090441584F6760040640F /* OFInvalidJSONException.h in Headers */, 4B17FFB9133A381D003E6DCD /* OFInvalidServerReplyException.h in Headers */, 4B29BC61133AC9CB0004B236 /* OFLinkFailedException.h in Headers */, 4B90B7A6133AD87D00BD33CB /* OFListenFailedException.h in Headers */, 4B17FFA1133A3335003E6DCD /* OFMalformedXMLException.h in Headers */, 4B17FF8F133A3036003E6DCD /* OFMemoryNotPartOfObjectException.h in Headers */, @@ -1522,10 +1529,11 @@ 4B17FFAE133A3591003E6DCD /* OFHTTPRequestFailedException.m in Sources */, 4B17FFA6133A3411003E6DCD /* OFInitializationFailedException.m in Sources */, 4B17FF94133A317C003E6DCD /* OFInvalidArgumentException.m in Sources */, 4B17FF9A133A3245003E6DCD /* OFInvalidEncodingException.m in Sources */, 4B17FF9E133A32BA003E6DCD /* OFInvalidFormatException.m in Sources */, + 4BC090451584F6760040640F /* OFInvalidJSONException.m in Sources */, 4B17FFBA133A381D003E6DCD /* OFInvalidServerReplyException.m in Sources */, 4B29BC62133AC9CB0004B236 /* OFLinkFailedException.m in Sources */, 4B90B7A7133AD87D00BD33CB /* OFListenFailedException.m in Sources */, 4B17FFA2133A3335003E6DCD /* OFMalformedXMLException.m in Sources */, 4B17FF90133A3036003E6DCD /* OFMemoryNotPartOfObjectException.m in Sources */, Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -270,10 +270,26 @@ * * \return A copy of the array with the order reversed */ - (OFArray*)reversedArray; +/** + * \brief Creates a new array with the specified object added. + * + * \param object The object to add + * \return A new array with the specified object added + */ +- (OFArray*)arrayByAddingObject: (id)object; + +/** + * \brief Creates a new array with the objects from the specified array added. + * + * \param array The array with objects to add + * \return A new array with the objects from the specified array added + */ +- (OFArray*)arrayByAddingObjectsFromArray: (OFArray*)array; + #ifdef OF_HAVE_BLOCKS /** * \brief Executes a block for each object. * * \param block The block to execute for each object Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -244,12 +244,12 @@ size_t count; id *buffer; container = [[[OFObject alloc] init] autorelease]; count = [self count]; - buffer = [container allocMemoryWithItemSize: sizeof(*buffer) - count: [self count]]; + buffer = [container allocMemoryWithSize: sizeof(*buffer) + count: [self count]]; [self getObjects: buffer inRange: of_range(0, count)]; return buffer; @@ -333,12 +333,12 @@ if (![self isKindOfClass: [OFMutableArray class]]) return [OFArray_subarray arrayWithArray: self range: range]; - buffer = [self allocMemoryWithItemSize: sizeof(*buffer) - count: range.length]; + buffer = [self allocMemoryWithSize: sizeof(*buffer) + count: range.length]; @try { [self getObjects: buffer inRange: range]; @@ -601,18 +601,38 @@ if (stop) break; } } #endif + +- (OFArray*)arrayByAddingObject: (id)object +{ + OFMutableArray *ret = [[self mutableCopy] autorelease]; + + [ret addObject: object]; + [ret makeImmutable]; + + return ret; +} + +- (OFArray*)arrayByAddingObjectsFromArray: (OFArray*)array +{ + OFMutableArray *ret = [[self mutableCopy] autorelease]; + + [ret addObjectsFromArray: array]; + [ret makeImmutable]; + + return ret; +} #ifdef OF_HAVE_BLOCKS - (OFArray*)mappedArrayUsingBlock: (of_array_map_block_t)block { OFArray *ret; size_t count = [self count]; - id *tmp = [self allocMemoryWithItemSize: sizeof(id) - count: count]; + id *tmp = [self allocMemoryWithSize: sizeof(id) + count: count]; @try { [self enumerateObjectsUsingBlock: ^ (id object, size_t index, BOOL *stop) { tmp[index] = block(object, index); @@ -629,12 +649,12 @@ - (OFArray*)filteredArrayUsingBlock: (of_array_filter_block_t)block { OFArray *ret; size_t count = [self count]; - id *tmp = [self allocMemoryWithItemSize: sizeof(id) - count: count]; + id *tmp = [self allocMemoryWithSize: sizeof(id) + count: count]; @try { __block size_t i = 0; [self enumerateObjectsUsingBlock: ^ (id object, size_t index, Index: src/OFAutoreleasePool.m ================================================================== --- src/OFAutoreleasePool.m +++ src/OFAutoreleasePool.m @@ -119,12 +119,12 @@ if (previousPool != nil) previousPool->nextPool = self; size = GROW_SIZE; - objects = [self allocMemoryWithItemSize: sizeof(id) - count: GROW_SIZE]; + objects = [self allocMemoryWithSize: sizeof(id) + count: GROW_SIZE]; } @catch (id e) { [self release]; @throw e; } @@ -133,11 +133,11 @@ - (void)_addObject: (id)object { if (count + 1 > size) { objects = [self resizeMemory: objects - itemSize: sizeof(id) + size: sizeof(id) count: size + GROW_SIZE]; size += GROW_SIZE; } objects[count] = object; Index: src/OFBlock.m ================================================================== --- src/OFBlock.m +++ src/OFBlock.m @@ -384,27 +384,27 @@ { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } -- (void*)allocMemoryForNItems: (size_t)nitems - withSize: (size_t)size +- (void*)allocMemoryWithSize: (size_t)size + count: (size_t)count { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (void*)resizeMemory: (void*)ptr - toSize: (size_t)size + size: (size_t)size { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (void*)resizeMemory: (void*)ptr - toNItems: (size_t)nitems - withSize: (size_t)size + size: (size_t)size + count: (size_t)count { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } Index: src/OFConstantString.m ================================================================== --- src/OFConstantString.m +++ src/OFConstantString.m @@ -60,19 +60,19 @@ { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } -- (void*)allocMemoryForNItems: (size_t)nitems - withSize: (size_t)size +- (void*)allocMemoryWithSize: (size_t)itemSize + count: (size_t)count { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (void*)resizeMemory: (void*)ptr - toSize: (size_t)size + size: (size_t)size { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } @@ -180,27 +180,27 @@ { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } -- (void*)allocMemoryForNItems: (size_t)nitems - withSize: (size_t)size +- (void*)allocMemoryWithSize: (size_t)size + count: (size_t)count { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (void*)resizeMemory: (void*)ptr - toSize: (size_t)size + size: (size_t)size { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (void*)resizeMemory: (void*)ptr - toNItems: (size_t)nitems - withSize: (size_t)size + size: (size_t)size + count: (size_t)count { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -109,12 +109,12 @@ char *buffer = [self allocMemoryWithSize: of_pagesize]; while (![file isAtEndOfStream]) { size_t length; - length = [file readNBytes: of_pagesize - intoBuffer: buffer]; + length = [file readIntoBuffer: buffer + length: of_pagesize]; [self addItemsFromCArray: buffer count: length]; } [self freeMemory: buffer]; @@ -255,11 +255,11 @@ { if (SIZE_MAX - count < 1) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data - itemSize: itemSize + size: itemSize count: count + 1]; memcpy(data + count * itemSize, item, itemSize); count++; @@ -278,11 +278,11 @@ { if (nItems > SIZE_MAX - count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data - itemSize: itemSize + size: itemSize count: count + nItems]; memcpy(data + count * itemSize, cArray, nItems * itemSize); count += nItems; } @@ -293,11 +293,11 @@ { if (nItems > SIZE_MAX - count || index > count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data - itemSize: itemSize + size: itemSize count: count + nItems]; memmove(data + (index + nItems) * itemSize, data + index * itemSize, (count - index) * itemSize); memcpy(data + index * itemSize, cArray, nItems * itemSize); @@ -320,11 +320,11 @@ (count - range.start - range.length) * itemSize); count -= range.length; @try { data = [self resizeMemory: data - itemSize: itemSize + size: itemSize count: count]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -335,11 +335,11 @@ @throw [OFOutOfRangeException exceptionWithClass: isa]; count--; @try { data = [self resizeMemory: data - itemSize: itemSize + size: itemSize count: count]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only made it smaller */ } } @@ -435,12 +435,12 @@ { OFFile *file = [[OFFile alloc] initWithPath: path mode: @"wb"]; @try { - [file writeNBytes: count * itemSize - fromBuffer: data]; + [file writeBuffer: data + length: count * itemSize]; } @finally { [file release]; } } @@ -478,11 +478,11 @@ lastPageByte = of_pagesize - 1; newSize = ((count + 1) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data - toSize: newSize]; + size: newSize]; memcpy(data + count * itemSize, item, itemSize); count++; size = newSize; @@ -499,11 +499,11 @@ lastPageByte = of_pagesize - 1; newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data - toSize: newSize]; + size: newSize]; memcpy(data + count * itemSize, cArray, nItems * itemSize); count += nItems; size = newSize; @@ -522,11 +522,11 @@ lastPageByte = of_pagesize - 1; newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data - toSize: newSize]; + size: newSize]; memmove(data + (index + nItems) * itemSize, data + index * itemSize, (count - index) * itemSize); memcpy(data + index * itemSize, cArray, nItems * itemSize); @@ -549,11 +549,11 @@ lastPageByte = of_pagesize - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data - toSize: newSize]; + size: newSize]; size = newSize; } - (void)removeLastItem { @@ -567,11 +567,11 @@ newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) { @try { data = [self resizeMemory: data - toSize: newSize]; + size: newSize]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only made it smaller */ } size = newSize; Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -350,12 +350,12 @@ } - (OFArray*)allKeys { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - id *keys = [self allocMemoryWithItemSize: sizeof(id) - count: [self count]]; + id *keys = [self allocMemoryWithSize: sizeof(id) + count: [self count]]; OFArray *ret; OFEnumerator *enumerator; id key; size_t i = 0; @@ -380,12 +380,12 @@ } - (OFArray*)allObjects { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - id *objects = [self allocMemoryWithItemSize: sizeof(id) - count: [self count]]; + id *objects = [self allocMemoryWithSize: sizeof(id) + count: [self count]]; OFArray *ret; OFEnumerator *enumerator; id object; size_t i = 0; Index: src/OFDictionary_hashtable.m ================================================================== --- src/OFDictionary_hashtable.m +++ src/OFDictionary_hashtable.m @@ -79,12 +79,12 @@ exceptionWithClass: isa selector: _cmd]; hashtable = (OFDictionary_hashtable*)dictionary; - data = [self allocMemoryWithItemSize: sizeof(*data) - count: hashtable->size]; + data = [self allocMemoryWithSize: sizeof(*data) + count: hashtable->size]; for (i = 0; i < hashtable->size; i++) data[i] = NULL; size = hashtable->size; @@ -139,12 +139,12 @@ newSize <<= 1; if (newSize == 0) @throw [OFOutOfRangeException exceptionWithClass: isa]; - data = [self allocMemoryWithItemSize: sizeof(*data) - count: newSize]; + data = [self allocMemoryWithSize: sizeof(*data) + count: newSize]; for (i = 0; i < newSize; i++) data[i] = NULL; size = newSize; @@ -206,12 +206,12 @@ if (key == nil || object == nil) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; - data = [self allocMemoryWithItemSize: sizeof(*data) - count: 2]; + data = [self allocMemoryWithSize: sizeof(*data) + count: 2]; size = 2; for (i = 0; i < size; i++) data[i] = NULL; @@ -272,12 +272,12 @@ newSize <<= 1; if (newSize == 0) @throw [OFOutOfRangeException exceptionWithClass: isa]; - data = [self allocMemoryWithItemSize: sizeof(*data) - count: newSize]; + data = [self allocMemoryWithSize: sizeof(*data) + count: newSize]; for (j = 0; j < newSize; j++) data[j] = NULL; size = newSize; @@ -393,12 +393,12 @@ newSize <<= 1; if (newSize == 0) @throw [OFOutOfRangeException exceptionWithClass: isa]; - data = [self allocMemoryWithItemSize: sizeof(*data) - count: newSize]; + data = [self allocMemoryWithSize: sizeof(*data) + count: newSize]; for (j = 0; j < newSize; j++) data[j] = NULL; size = newSize; @@ -646,12 +646,12 @@ } - (OFArray*)allKeys { OFArray *ret; - id *keys = [self allocMemoryWithItemSize: sizeof(*keys) - count: count]; + id *keys = [self allocMemoryWithSize: sizeof(*keys) + count: count]; size_t i, j; for (i = j = 0; i < size; i++) if (data[i] != NULL && data[i] != DELETED) keys[j++] = data[i]->key; @@ -669,12 +669,12 @@ } - (OFArray*)allObjects { OFArray *ret; - id *objects = [self allocMemoryWithItemSize: sizeof(*objects) - count: count]; + id *objects = [self allocMemoryWithSize: sizeof(*objects) + count: count]; size_t i, j; for (i = j = 0; i < size; i++) if (data[i] != NULL && data[i] != DELETED) objects[j++] = data[i]->object; Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -523,14 +523,16 @@ mode: @"rb"]; destinationFile = [OFFile fileWithPath: destination mode: @"wb"]; while (![sourceFile isAtEndOfStream]) { - size_t len = [sourceFile readNBytes: of_pagesize - intoBuffer: buffer]; - [destinationFile writeNBytes: len - fromBuffer: buffer]; + size_t length; + + length = [sourceFile readIntoBuffer: buffer + length: of_pagesize]; + [destinationFile writeBuffer: buffer + length: length]; } #if !defined(_WIN32) && !defined(_PSP) if (!override) { struct stat s; @@ -694,12 +696,12 @@ return YES; return atEndOfStream; } -- (size_t)_readNBytes: (size_t)length - intoBuffer: (void*)buffer +- (size_t)_readIntoBuffer: (void*)buffer + length: (size_t)length { ssize_t ret; if (fileDescriptor == -1 || atEndOfStream || (ret = read(fileDescriptor, buffer, length)) < 0) @@ -711,12 +713,12 @@ atEndOfStream = YES; return ret; } -- (void)_writeNBytes: (size_t)length - fromBuffer: (const void*)buffer +- (void)_writeBuffer: (const void*)buffer + length: (size_t)length { if (fileDescriptor == -1 || atEndOfStream || write(fileDescriptor, buffer, length) < length) @throw [OFWriteFailedException exceptionWithClass: isa stream: self Index: src/OFHTTPRequest.m ================================================================== --- src/OFHTTPRequest.m +++ src/OFHTTPRequest.m @@ -226,11 +226,11 @@ /* * Work around a bug with packet bisection in lighttpd when using * HTTPS. */ - [sock setBuffersWrites: YES]; + [sock setWriteBufferEnabled: YES]; if (requestType == OF_HTTP_REQUEST_TYPE_GET) type = "GET"; if (requestType == OF_HTTP_REQUEST_TYPE_HEAD) type = "HEAD"; @@ -272,11 +272,11 @@ [sock writeString: @"\r\n"]; /* Work around a bug in lighttpd, see above */ [sock flushWriteBuffer]; - [sock setBuffersWrites: NO]; + [sock setWriteBufferEnabled: NO]; if (requestType == OF_HTTP_REQUEST_TYPE_POST) [sock writeString: queryString]; @try { @@ -420,12 +420,12 @@ while (toRead > 0) { size_t length = (toRead < of_pagesize ? toRead : of_pagesize); - length = [sock readNBytes: length - intoBuffer: buffer]; + length = [sock readIntoBuffer: buffer + length: length]; [delegate request: self didReceiveData: buffer withLength: length]; [pool2 releaseObjects]; @@ -451,12 +451,13 @@ [pool2 releaseObjects]; } } else { size_t length; - while ((length = [sock readNBytes: of_pagesize - intoBuffer: buffer]) > 0) { + while ((length = [sock + readIntoBuffer: buffer + length: of_pagesize]) > 0) { [delegate request: self didReceiveData: buffer withLength: length]; [pool2 releaseObjects]; Index: src/OFMutableArray.h ================================================================== --- src/OFMutableArray.h +++ src/OFMutableArray.h @@ -30,19 +30,35 @@ * * \param object An object to add */ - (void)addObject: (id)object; +/** + * \brief Adds the objects from the specified OFArray to the end of the array. + * + * \brief array An array of objects to add + */ +- (void)addObjectsFromArray: (OFArray*)array; + /** * \brief Inserts an object to the OFArray at the specified index. * * \param object An object to add * \param index The index where the object should be inserted */ - (void)insertObject: (id)object atIndex: (size_t)index; +/** + * \brief Inserts the objects from the specified OFArray at the specified index. + * + * \param array An array of objects + * \param index The index where the objects should be inserted + */ +- (void)insertObjectsFromArray: (OFArray*)array + atIndex: (size_t)index; + /** * \brief Replaces the first object equivalent to the specified object with the * other specified object. * * \param oldObject The object to replace Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -180,17 +180,42 @@ - (void)addObject: (id)object { [self insertObject: object atIndex: [self count]]; } + +- (void)addObjectsFromArray: (OFArray*)array +{ + [self insertObjectsFromArray: array + atIndex: [self count]]; +} - (void)insertObject: (id)object atIndex: (size_t)index { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } + +- (void)insertObjectsFromArray: (OFArray*)array + atIndex: (size_t)index +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFEnumerator *enumerator = [array objectEnumerator]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) { + id object = [enumerator nextObject]; + + assert(object != nil); + + [self insertObject: object + atIndex: index + i]; + } + + [pool release]; +} - (void)replaceObjectAtIndex: (size_t)index withObject: (id)object { @throw [OFNotImplementedException exceptionWithClass: isa Index: src/OFMutableArray_adjacent.m ================================================================== --- src/OFMutableArray_adjacent.m +++ src/OFMutableArray_adjacent.m @@ -49,10 +49,26 @@ atIndex: index]; [object retain]; mutations++; } + +- (void)insertObjectsFromArray: (OFArray*)array_ + atIndex: (size_t)index +{ + id *objects = [array_ objects]; + size_t i, count = [array_ count]; + + [array insertItemsFromCArray: objects + atIndex: index + count: count]; + + for (i = 0; i < count; i++) + [objects[i] retain]; + + mutations++; +} - (void)replaceObject: (id)oldObject withObject: (id)newObject { id *objects = [array cArray]; @@ -162,12 +178,12 @@ size_t i, count = [array count]; if (range.length > count - range.start) @throw [OFOutOfRangeException exceptionWithClass: isa]; - copy = [self allocMemoryWithItemSize: sizeof(*copy) - count: range.length]; + copy = [self allocMemoryWithSize: sizeof(*copy) + count: range.length]; memcpy(copy, objects + range.start, range.length * sizeof(id)); @try { [array removeItemsInRange: range]; mutations++; Index: src/OFMutableDictionary_hashtable.m ================================================================== --- src/OFMutableDictionary_hashtable.m +++ src/OFMutableDictionary_hashtable.m @@ -58,12 +58,12 @@ return; if (newSize == 0) @throw [OFOutOfRangeException exceptionWithClass: isa]; - newData = [self allocMemoryWithItemSize: sizeof(*newData) - count: newSize]; + newData = [self allocMemoryWithSize: sizeof(*newData) + count: newSize]; for (i = 0; i < newSize; i++) newData[i] = NULL; for (i = 0; i < size; i++) { Index: src/OFMutableSet.m ================================================================== --- src/OFMutableSet.m +++ src/OFMutableSet.m @@ -157,12 +157,12 @@ { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; size_t count = [self count]; id *cArray; - cArray = [self allocMemoryWithItemSize: sizeof(id) - count: count]; + cArray = [self allocMemoryWithSize: sizeof(id) + count: count]; @try { OFEnumerator *enumerator = [self objectEnumerator]; id object; size_t i = 0; Index: src/OFMutableString_UTF8.m ================================================================== --- src/OFMutableString_UTF8.m +++ src/OFMutableString_UTF8.m @@ -67,12 +67,12 @@ return; } unicodeLen = [self length]; - unicodeString = [self allocMemoryWithItemSize: sizeof(of_unichar_t) - count: unicodeLen]; + unicodeString = [self allocMemoryWithSize: sizeof(of_unichar_t) + count: unicodeLen]; i = j = 0; newCStringLength = 0; while (i < s->cStringLength) { @@ -176,12 +176,12 @@ return; } if (length > oldLength) { s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength - - oldLength + length + 1]; + size: s->cStringLength - oldLength + + length + 1]; memmove(s->cString + index + length, s->cString + index + oldLength, s->cStringLength - index - oldLength); memcpy(s->cString + index, buffer, length); @@ -206,11 +206,11 @@ s->cStringLength += length; s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } return; @@ -237,11 +237,11 @@ @throw [OFInvalidEncodingException exceptionWithClass: isa]; } s->hashed = NO; s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + + size: s->cStringLength + UTF8StringLength + 1]; memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength + 1); s->cStringLength += UTF8StringLength; s->length += length; @@ -265,11 +265,11 @@ @throw [OFInvalidEncodingException exceptionWithClass: isa]; } s->hashed = NO; s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + + size: s->cStringLength + UTF8StringLength + 1]; memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength); s->cStringLength += UTF8StringLength; s->length += length; @@ -312,11 +312,11 @@ UTF8StringLength = [string UTF8StringLength]; s->hashed = NO; s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + + size: s->cStringLength + UTF8StringLength + 1]; memcpy(s->cString + s->cStringLength, [string UTF8String], UTF8StringLength); s->cStringLength += UTF8StringLength; @@ -451,11 +451,11 @@ s->cStringLength); newCStringLength = s->cStringLength + [string UTF8StringLength]; s->hashed = NO; s->cString = [self resizeMemory: s->cString - toSize: newCStringLength + 1]; + size: newCStringLength + 1]; memmove(s->cString + index + [string UTF8StringLength], s->cString + index, s->cStringLength - index); memcpy(s->cString + index, [string UTF8String], [string UTF8StringLength]); @@ -494,11 +494,11 @@ s->cStringLength -= end - start; s->cString[s->cStringLength] = 0; @try { s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -523,11 +523,11 @@ newCStringLength = s->cStringLength - (end - start) + [replacement UTF8StringLength]; s->hashed = NO; s->cString = [self resizeMemory: s->cString - toSize: newCStringLength + 1]; + size: newCStringLength + 1]; memmove(s->cString + start + [replacement UTF8StringLength], s->cString + end, s->cStringLength - end); memcpy(s->cString + start, [replacement UTF8String], [replacement UTF8StringLength]); @@ -570,11 +570,11 @@ continue; @try { newCString = [self resizeMemory: newCString - toSize: newCStringLength + i - last + + size: newCStringLength + i - last + replacementLength + 1]; } @catch (id e) { [self freeMemory: newCString]; @throw e; } @@ -589,14 +589,13 @@ i += searchLength - 1; last = i + 1; } @try { - newCString = [self - resizeMemory: newCString - toSize: newCStringLength + - s->cStringLength - last + 1]; + newCString = [self resizeMemory: newCString + size: newCStringLength + + s->cStringLength - last + 1]; } @catch (id e) { [self freeMemory: newCString]; @throw e; } memcpy(newCString + newCStringLength, s->cString + last, @@ -628,11 +627,11 @@ memmove(s->cString, s->cString + i, s->cStringLength); s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -656,11 +655,11 @@ s->cStringLength -= d; s->length -= d; @try { s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -696,11 +695,11 @@ memmove(s->cString, s->cString + i, s->cStringLength); s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } Index: src/OFNumber.m ================================================================== --- src/OFNumber.m +++ src/OFNumber.m @@ -1138,10 +1138,12 @@ return [self retain]; } - (OFString*)description { + OFMutableString *ret; + switch (type) { case OF_NUMBER_BOOL: return (value.bool_ ? @"YES" : @"NO"); case OF_NUMBER_UCHAR: case OF_NUMBER_USHORT: @@ -1167,13 +1169,27 @@ case OF_NUMBER_INTMAX: case OF_NUMBER_PTRDIFF: case OF_NUMBER_INTPTR: return [OFString stringWithFormat: @"%jd", [self intMaxValue]]; case OF_NUMBER_FLOAT: - return [OFString stringWithFormat: @"%g", value.float_]; + ret = [OFMutableString stringWithFormat: @"%g", value.float_]; + + if (![ret containsString: @"."]) + [ret appendString: @".0"]; + + [ret makeImmutable]; + + return ret; case OF_NUMBER_DOUBLE: - return [OFString stringWithFormat: @"%lg", value.double_]; + ret = [OFMutableString stringWithFormat: @"%lg", value.double_]; + + if (![ret containsString: @"."]) + [ret appendString: @".0"]; + + [ret makeImmutable]; + + return ret; default: @throw [OFInvalidFormatException exceptionWithClass: isa]; } } Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -499,16 +499,16 @@ * \brief Allocates memory for the specified number of items and stores it in * the object's memory pool. * * It will be free'd automatically when the object is deallocated. * - * \param itemSize The size of each item to allocate + * \param size The size of each item to allocate * \param count The number of items to allocate * \return A pointer to the allocated memory */ -- (void*)allocMemoryWithItemSize: (size_t)itemSize - count: (size_t)count; +- (void*)allocMemoryWithSize: (size_t)size + count: (size_t)count; /** * \brief Resizes memory in the object's memory pool to the specified size. * * If the pointer is NULL, this is equivalent to allocating memory. @@ -517,26 +517,26 @@ * \param pointer A pointer to the already allocated memory * \param size The new size for the memory chunk * \return A pointer to the resized memory chunk */ - (void*)resizeMemory: (void*)pointer - toSize: (size_t)size; + size: (size_t)size; /** * \brief Resizes memory in the object's memory pool to the specific number of * items of the specified size. * * If the pointer is NULL, this is equivalent to allocating memory. * If the size or number of items is 0, this is equivalent to freeing memory. * * \param pointer A pointer to the already allocated memory - * \param itemSize The size of each item to resize to + * \param size The size of each item to resize to * \param count The number of items to resize to * \return A pointer to the resized memory chunk */ - (void*)resizeMemory: (void*)pointer - itemSize: (size_t)itemSize + size: (size_t)size count: (size_t)count; /** * \brief Frees allocated memory and removes it from the object's memory pool. * Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -608,24 +608,24 @@ PRE_IVAR->lastMem = preMem; return (char*)pointer + PRE_MEM_ALIGN; } -- (void*)allocMemoryWithItemSize: (size_t)itemSize - count: (size_t)count +- (void*)allocMemoryWithSize: (size_t)size + count: (size_t)count { - if (itemSize == 0 || count == 0) + if (size == 0 || count == 0) return NULL; - if (count > SIZE_MAX / itemSize) + if (count > SIZE_MAX / size) @throw [OFOutOfRangeException exceptionWithClass: isa]; - return [self allocMemoryWithSize: itemSize * count]; + return [self allocMemoryWithSize: size * count]; } - (void*)resizeMemory: (void*)pointer - toSize: (size_t)size + size: (size_t)size { void *new; struct pre_mem *preMem; if (pointer == NULL) @@ -660,27 +660,27 @@ return (char*)new + PRE_MEM_ALIGN; } - (void*)resizeMemory: (void*)pointer - itemSize: (size_t)itemSize + size: (size_t)size count: (size_t)count { if (pointer == NULL) - return [self allocMemoryWithItemSize: itemSize - count: count]; + return [self allocMemoryWithSize: size + count: count]; - if (itemSize == 0 || count == 0) { + if (size == 0 || count == 0) { [self freeMemory: pointer]; return NULL; } - if (count > SIZE_MAX / itemSize) + if (count > SIZE_MAX / size) @throw [OFOutOfRangeException exceptionWithClass: isa]; return [self resizeMemory: pointer - toSize: itemSize * count]; + size: size * count]; } - (void)freeMemory: (void*)pointer { if (pointer == NULL) @@ -837,26 +837,26 @@ { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } -+ (void*)allocMemoryWithItemSize: (size_t)itemSize - count: (size_t)count ++ (void*)allocMemoryWithSize: (size_t)size + count: (size_t)count { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } + (void*)resizeMemory: (void*)pointer - toSize: (size_t)size + size: (size_t)size { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } + (void*)resizeMemory: (void*)pointer - itemSize: (size_t)itemSize + size: (size_t)size count: (size_t)count { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } Index: src/OFProcess.m ================================================================== --- src/OFProcess.m +++ src/OFProcess.m @@ -89,12 +89,12 @@ case 0:; OFString **objects = [arguments objects]; size_t i, count = [arguments count]; char **argv; - argv = [self allocMemoryWithItemSize: sizeof(char*) - count: count + 2]; + argv = [self allocMemoryWithSize: sizeof(char*) + count: count + 2]; argv[0] = (char*)[programName cStringWithEncoding: OF_STRING_ENCODING_NATIVE]; for (i = 0; i < count; i++) @@ -234,12 +234,12 @@ return YES; return atEndOfStream; } -- (size_t)_readNBytes: (size_t)length - intoBuffer: (void*)buffer +- (size_t)_readIntoBuffer: (void*)buffer + length: (size_t)length { #ifndef _WIN32 ssize_t ret; #else DWORD ret; @@ -266,12 +266,12 @@ atEndOfStream = YES; return ret; } -- (void)_writeNBytes: (size_t)length - fromBuffer: (const void*)buffer +- (void)_writeBuffer: (const void*)buffer + length: (size_t)length { #ifndef _WIN32 if (writePipe[1] == -1 || atEndOfStream || write(writePipe[1], buffer, length) < length) #else Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -30,29 +30,29 @@ /** * \brief A base class for different types of streams. * * \warning Even though the OFCopying protocol is implemented, it does - * not return an independant copy of the stream but instead + * not return an independent copy of the stream but instead * retains it. This is so that the stream can be used as a key for a - * dictionary so context can be associated with a stream. Using a + * dictionary, so context can be associated with a stream. Using a * stream in more than one thread at the same time is not thread-safe, - * even if copy was called! - * - * \warning If you want to subclass this, override _readNBytes:intoBuffer:, - * _writeNBytes:fromBuffer: and _isAtEndOfStream, but nothing else. - * Those are not defined in the headers, but do the actual work. - * OFStream uses those and does all the caching and other stuff. If - * you override these methods without the _ prefix, you *WILL* break - * caching and get broken results! + * even if copy was called to create one "instance" for every thread! + * + * \warning If you want to subclass this, override _readIntoBuffer:length:, + * _writeBuffer:length: and _isAtEndOfStream, but nothing else, as + * those are are the methods that do the actual work. OFStream uses + * those for all other methods and does all the caching and other + * stuff for you. If you override these methods without the _ prefix, + * you will break caching and get broken results! */ @interface OFStream: OFObject { char *cache; char *writeBuffer; size_t cacheLength, writeBufferLength; - BOOL buffersWrites; + BOOL writeBufferEnabled; BOOL blocking; BOOL waitingForDelimiter; } #ifdef OF_HAVE_PROPERTIES @@ -70,37 +70,37 @@ /** * \brief Reads at most size bytes from the stream into a buffer. * * On network streams, this might read less than the specified number of bytes. * If you want to read exactly the specified number of bytes, use - * -[readExactlyNBytes:intoBuffer:]. + * -[readIntoBuffer:exactLength:]. * * \param buffer The buffer into which the data is read * \param length The length of the data that should be read at most. - * The buffer MUST be at least this big! + * The buffer must be at least this big! * \return The number of bytes read */ -- (size_t)readNBytes: (size_t)size - intoBuffer: (void*)buffer; +- (size_t)readIntoBuffer: (void*)buffer + length: (size_t)size; /** * \brief Reads exactly the specified length bytes from the stream into a * buffer. * - * Unlike readNBytes:intoBuffer:, this method does not return when less than the + * Unlike readIntoBuffer:length:, this method does not return when less than the * specified length has been read - instead, it waits until it got exactly the * specified length. * * \warning Only call this when you know that specified amount of data is * available! Otherwise you will get an exception! * * \param buffer The buffer into which the data is read * \param length The length of the data that should be read. - * The buffer MUST be EXACTLY this big! + * The buffer must be exactly this big! */ -- (void)readExactlyNBytes: (size_t)length - intoBuffer: (void*)buffer; + - (void)readIntoBuffer: (void*)buffer + exactLength: (size_t)length; /** * \brief Reads a uint8_t from the stream. * * \warning Only call this when you know that enough data is available! @@ -170,12 +170,12 @@ * \param nInt16s The number of uint16_ts to read * \param buffer A buffer of sufficient size to store the specified number of * uint16_ts * \return The number of bytes read */ -- (size_t)readNBigEndianInt16s: (size_t)nInt16s - intoBuffer: (uint16_t*)buffer; +- (size_t)readBigEndianInt16sIntoBuffer: (uint16_t*)buffer + count: (size_t)nInt16s; /** * \brief Reads the specified number of uint32_ts from the stream which are * encoded in big endian. * @@ -185,12 +185,12 @@ * \param nInt32s The number of uint32_ts to read * \param buffer A buffer of sufficient size to store the specified number of * uint32_ts * \return The number of bytes read */ -- (size_t)readNBigEndianInt32s: (size_t)nInt32s - intoBuffer: (uint32_t*)buffer; +- (size_t)readBigEndianInt32sIntoBuffer: (uint32_t*)buffer + count: (size_t)nInt32s; /** * \brief Reads the specified number of uint64_ts from the stream which are * encoded in big endian. * @@ -200,12 +200,12 @@ * \param nInt64s The number of uint64_ts to read * \param buffer A buffer of sufficient size to store the specified number of * uint64_ts * \return The number of bytes read */ -- (size_t)readNBigEndianInt64s: (size_t)nInt64s - intoBuffer: (uint64_t*)buffer; +- (size_t)readBigEndianInt64sIntoBuffer: (uint64_t*)buffer + count: (size_t)nInt64s; /** * \brief Reads the specified number of floats from the stream which are encoded * in big endian. * @@ -215,12 +215,12 @@ * \param nFloatss The number of floats to read * \param buffer A buffer of sufficient size to store the specified number of * floats * \return The number of bytes read */ -- (size_t)readNBigEndianFloats: (size_t)nFloats - intoBuffer: (float*)buffer; +- (size_t)readBigEndianFloatsIntoBuffer: (float*)buffer + count: (size_t)nFloats; /** * \brief Reads the specified number of doubles from the stream which are * encoded in big endian. * @@ -230,12 +230,12 @@ * \param nDoubles The number of doubles to read * \param buffer A buffer of sufficient size to store the specified number of * doubles * \return The number of bytes read */ -- (size_t)readNBigEndianDoubles: (size_t)nDoubles - intoBuffer: (double*)buffer; +- (size_t)readBigEndianDoublesIntoBuffer: (double*)buffer + count: (size_t)nDoubles; /** * \brief Reads a uint16_t from the stream which is encoded in little endian. * * \warning Only call this when you know that enough data is available! @@ -295,12 +295,12 @@ * \param nInt16s The number of uint16_ts to read * \param buffer A buffer of sufficient size to store the specified number of * uint16_ts * \return The number of bytes read */ -- (size_t)readNLittleEndianInt16s: (size_t)nInt16s - intoBuffer: (uint16_t*)buffer; +- (size_t)readLittleEndianInt16sIntoBuffer: (uint16_t*)buffer + count: (size_t)nInt16s; /** * \brief Reads the specified number of uint32_ts from the stream which are * encoded in little endian. * @@ -310,12 +310,12 @@ * \param nInt32s The number of uint32_ts to read * \param buffer A buffer of sufficient size to store the specified number of * uint32_ts * \return The number of bytes read */ -- (size_t)readNLittleEndianInt32s: (size_t)nInt32s - intoBuffer: (uint32_t*)buffer; +- (size_t)readLittleEndianInt32sIntoBuffer: (uint32_t*)buffer + count: (size_t)nInt32s; /** * \brief Reads the specified number of uint64_ts from the stream which are * encoded in little endian. * @@ -325,12 +325,12 @@ * \param nInt64s The number of uint64_ts to read * \param buffer A buffer of sufficient size to store the specified number of * uint64_ts * \return The number of bytes read */ -- (size_t)readNLittleEndianInt64s: (size_t)nInt64s - intoBuffer: (uint64_t*)buffer; +- (size_t)readLittleEndianInt64sIntoBuffer: (uint64_t*)buffer + count: (size_t)nInt64s; /** * \brief Reads the specified number of floats from the stream which are * encoded in little endian. * @@ -340,12 +340,12 @@ * \param nFloats The number of floats to read * \param buffer A buffer of sufficient size to store the specified number of * floats * \return The number of bytes read */ -- (size_t)readNLittleEndianFloats: (size_t)nFloats - intoBuffer: (float*)buffer; +- (size_t)readLittleEndianFloatsIntoBuffer: (float*)buffer + count: (size_t)nFloats; /** * \brief Reads the specified number of doubles from the stream which are * encoded in little endian. * @@ -355,12 +355,12 @@ * \param nDoubles The number of doubles to read * \param buffer A buffer of sufficient size to store the specified number of * doubles * \return The number of bytes read */ -- (size_t)readNLittleEndianDoubles: (size_t)nDoubles - intoBuffer: (double*)buffer; +- (size_t)readLittleEndianDoublesIntoBuffer: (double*)buffer + count: (size_t)nDoubles; /** * \brief Reads the specified number of items with an item size of 1 from the * stream and returns them in an OFDataArray. * @@ -368,11 +368,11 @@ * Otherwise you will get an exception! * * \param nItems The number of items to read * \return An OFDataArray with at nItems items. */ -- (OFDataArray*)readDataArrayWithNItems: (size_t)nItems; +- (OFDataArray*)readDataArrayWithSize: (size_t)size; /** * \brief Reads the specified number of items with the specified item size from * the stream and returns them in an OFDataArray. * @@ -382,11 +382,11 @@ * \param itemSize The size of each item * \param nItems The number of items to read * \return An OFDataArray with at nItems items. */ - (OFDataArray*)readDataArrayWithItemSize: (size_t)itemSize - andNItems: (size_t)nItems; + count: (size_t)nItems; /** * \brief Returns an OFDataArray with all the remaining data of the stream. * * \return An OFDataArray with an item size of 1 with all the data of the @@ -423,12 +423,12 @@ * * \param encoding The encoding of the string to read from the stream * \param length The length (in bytes) of the string to read from the stream * \return A string with the specified length */ -- (OFString*)readStringWithEncoding: (of_string_encoding_t)encoding - length: (size_t)length; +- (OFString*)readStringWithLength: (size_t)length + encoding: (of_string_encoding_t)encoding; /** * \brief Reads until a newline, \\0 or end of stream occurs. * * \return The line that was read, autoreleased, or nil if the end of the @@ -484,11 +484,11 @@ * \param encoding The encoding used by the stream * \return The line that was read, autoreleased, or nil if the end of the * stream has been reached. */ - (OFString*)readTillDelimiter: (OFString*)delimiter - withEncoding: (of_string_encoding_t)encoding; + encoding: (of_string_encoding_t)encoding; /** * \brief Tries to reads until the specified string or \\0 is found or the end * of stream (see readTillDelimiter:) and returns nil if not enough data * has been received yet. @@ -508,25 +508,25 @@ * \param encoding The encoding used by the stream * \return The line that was read, autoreleased, or nil if the end of the * stream has been reached. */ - (OFString*)tryReadTillDelimiter: (OFString*)delimiter - withEncoding: (of_string_encoding_t)encoding; + encoding: (of_string_encoding_t)encoding; /** * \brief Returns a boolen whether writes are buffered. * * \return A boolean whether writes are buffered */ -- (BOOL)buffersWrites; +- (BOOL)writeBufferEnabled; /** * \brief Enables or disables the write buffer. * * \param enable Whether the write buffer should be enabled or disabled */ -- (void)setBuffersWrites: (BOOL)enable; +- (void)setWriteBufferEnabled: (BOOL)enable; /** * \brief Writes everythig in the write buffer to the stream. */ - (void)flushWriteBuffer; @@ -535,12 +535,12 @@ * \brief Writes from a buffer into the stream. * * \param buffer The buffer from which the data is written to the stream * \param length The length of the data that should be written */ -- (void)writeNBytes: (size_t)length - fromBuffer: (const void*)buffer; +- (void)writeBuffer: (const void*)buffer + length: (size_t)length; /** * \brief Writes a uint8_t into the stream. * * \param int8 A uint8_t @@ -589,12 +589,12 @@ * \param nInt16 The number of uint16_ts to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNBigEndianInt16s: (size_t)nInt16s - fromBuffer: (const uint16_t*)buffer; +- (size_t)writeBigEndianInt16s: (const uint16_t*)buffer + count: (size_t)nInt16s; /** * \brief Writes the specified number of uint32_ts into the stream, encoded in * big endian. * @@ -601,12 +601,12 @@ * \param nInt32 The number of uint32_ts to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNBigEndianInt32s: (size_t)nInt32s - fromBuffer: (const uint32_t*)buffer; +- (size_t)writeBigEndianInt32s: (const uint32_t*)buffer + count: (size_t)nInt32s; /** * \brief Writes the specified number of uint64_ts into the stream, encoded in * big endian. * @@ -613,12 +613,12 @@ * \param nInt64 The number of uint64_ts to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNBigEndianInt64s: (size_t)nInt64s - fromBuffer: (const uint64_t*)buffer; +- (size_t)writeBigEndianInt64s: (const uint64_t*)buffer + count: (size_t)nInt64s; /** * \brief Writes the specified number of floats into the stream, encoded in big * endian. * @@ -625,12 +625,12 @@ * \param nFloats The number of floats to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNBigEndianFloats: (size_t)nFloats - fromBuffer: (const float*)buffer; +- (size_t)writeBigEndianFloats: (const float*)buffer + count: (size_t)nFloats; /** * \brief Writes the specified number of doubles into the stream, encoded in * big endian. * @@ -637,12 +637,12 @@ * \param nDoubles The number of doubles to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNBigEndianDoubles: (size_t)nDoubles - fromBuffer: (const double*)buffer; +- (size_t)writeBigEndianDoubles: (const double*)buffer + count: (size_t)nDoubles; /** * \brief Writes a uint16_t into the stream, encoded in little endian. * * \param int16 A uint16_t @@ -684,12 +684,12 @@ * \param nInt16 The number of uint16_ts to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNLittleEndianInt16s: (size_t)nInt16s - fromBuffer: (const uint16_t*)buffer; +- (size_t)writeLittleEndianInt16s: (const uint16_t*)buffer + count: (size_t)nInt16s; /** * \brief Writes the specified number of uint32_ts into the stream, encoded in * little endian. * @@ -696,12 +696,12 @@ * \param nInt32 The number of uint32_ts to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNLittleEndianInt32s: (size_t)nInt32s - fromBuffer: (const uint32_t*)buffer; +- (size_t)writeLittleEndianInt32s: (const uint32_t*)buffer + count: (size_t)nInt32s; /** * \brief Writes the specified number of uint64_ts into the stream, encoded in * little endian. * @@ -708,12 +708,12 @@ * \param nInt64 The number of uint64_ts to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNLittleEndianInt64s: (size_t)nInt64s - fromBuffer: (const uint64_t*)buffer; +- (size_t)writeLittleEndianInt64s: (const uint64_t*)buffer + count: (size_t)nInt64s; /** * \brief Writes the specified number of floats into the stream, encoded in * little endian. * @@ -720,12 +720,12 @@ * \param nFloats The number of floats to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNLittleEndianFloats: (size_t)nFloats - fromBuffer: (const float*)buffer; +- (size_t)writeLittleEndianFloats: (const float*)buffer + count: (size_t)nFloats; /** * \brief Writes the specified number of doubles into the stream, encoded in * little endian. * @@ -732,12 +732,12 @@ * \param nDoubles The number of doubles to write * \param buffer The buffer from which the data is written to the stream after * it has been byte swapped if necessary * \return The number of bytes written to the stream */ -- (size_t)writeNLittleEndianDoubles: (size_t)nDoubles - fromBuffer: (const double*)buffer; +- (size_t)writeLittleEndianDoubles: (const double*)buffer + count: (size_t)nDoubles; /** * \brief Writes from an OFDataArray into the stream. * * \param dataArray The OFDataArray to write into the stream @@ -781,16 +781,16 @@ * \param format A string used as format * \param arguments The arguments used in the format string * \return The number of bytes written */ - (size_t)writeFormat: (OFConstantString*)format - withArguments: (va_list)arguments; + arguments: (va_list)arguments; /** - * \brief Returns the number of bytes still present in the internal cache. + * \brief Returns the number of bytes still present in the internal read cache. * - * \return The number of bytes still present in the internal cache. + * \return The number of bytes still present in the internal read cache. */ - (size_t)pendingBytes; /** * \brief Returns whether the stream is in blocking mode. @@ -819,11 +819,11 @@ /** * \brief Closes the stream. */ - (void)close; -- (size_t)_readNBytes: (size_t)length - intoBuffer: (void*)buffer; -- (void)_writeNBytes: (size_t)length - fromBuffer: (const void*)buffer; +- (size_t)_readIntoBuffer: (void*)buffer + length: (size_t)length; +- (void)_writeBuffer: (const void*)buffer + length: (size_t)length; - (BOOL)_isWaitingForDelimiter; @end Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -74,19 +74,19 @@ { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } -- (size_t)_readNBytes: (size_t)length - intoBuffer: (void*)buffer +- (size_t)_readIntoBuffer: (void*)buffer + length: (size_t)length { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } -- (void)_writeNBytes: (size_t)length - fromBuffer: (const void*)buffer +- (void)_writeBuffer: (const void*)buffer + length: (size_t)length { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } @@ -101,16 +101,16 @@ return NO; return [self _isAtEndOfStream]; } -- (size_t)readNBytes: (size_t)length - intoBuffer: (void*)buffer +- (size_t)readIntoBuffer: (void*)buffer + length: (size_t)length { if (cache == NULL) - return [self _readNBytes: length - intoBuffer: buffer]; + return [self _readIntoBuffer: buffer + length: length]; if (length >= cacheLength) { size_t ret = cacheLength; memcpy(buffer, cache, cacheLength); @@ -131,87 +131,87 @@ return length; } } -- (void)readExactlyNBytes: (size_t)length - intoBuffer: (void*)buffer +- (void)readIntoBuffer: (void*)buffer + exactLength: (size_t)length { size_t readLength = 0; while (readLength < length) - readLength += [self readNBytes: length - readLength - intoBuffer: (char*)buffer + readLength]; + readLength += [self readIntoBuffer: (char*)buffer + readLength + length: length - readLength]; } - (uint8_t)readInt8 { uint8_t ret; - [self readExactlyNBytes: 1 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 1]; return ret; } - (uint16_t)readBigEndianInt16 { uint16_t ret; - [self readExactlyNBytes: 2 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 2]; return of_bswap16_if_le(ret); } - (uint32_t)readBigEndianInt32 { uint32_t ret; - [self readExactlyNBytes: 4 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 4]; return of_bswap32_if_le(ret); } - (uint64_t)readBigEndianInt64 { uint64_t ret; - [self readExactlyNBytes: 8 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 8]; return of_bswap64_if_le(ret); } - (float)readBigEndianFloat { float ret; - [self readExactlyNBytes: 4 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 4]; return of_bswap_float_if_le(ret); } - (double)readBigEndianDouble { double ret; - [self readExactlyNBytes: 8 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 8]; return of_bswap_double_if_le(ret); } -- (size_t)readNBigEndianInt16s: (size_t)nInt16s - intoBuffer: (uint16_t*)buffer +- (size_t)readBigEndianInt16sIntoBuffer: (uint16_t*)buffer + count: (size_t)nInt16s { size_t size = nInt16s * sizeof(uint16_t); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifndef OF_BIG_ENDIAN size_t i; for (i = 0; i < nInt16s; i++) @@ -219,17 +219,17 @@ #endif return size; } -- (size_t)readNBigEndianInt32s: (size_t)nInt32s - intoBuffer: (uint32_t*)buffer +- (size_t)readBigEndianInt32sIntoBuffer: (uint32_t*)buffer + count: (size_t)nInt32s { size_t size = nInt32s * sizeof(uint32_t); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifndef OF_BIG_ENDIAN size_t i; for (i = 0; i < nInt32s; i++) @@ -237,17 +237,17 @@ #endif return size; } -- (size_t)readNBigEndianInt64s: (size_t)nInt64s - intoBuffer: (uint64_t*)buffer +- (size_t)readBigEndianInt64sIntoBuffer: (uint64_t*)buffer + count: (size_t)nInt64s { size_t size = nInt64s * sizeof(uint64_t); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifndef OF_BIG_ENDIAN size_t i; for (i = 0; i < nInt64s; i++) @@ -255,17 +255,17 @@ #endif return size; } -- (size_t)readNBigEndianFloats: (size_t)nFloats - intoBuffer: (float*)buffer +- (size_t)readBigEndianFloatsIntoBuffer: (float*)buffer + count: (size_t)nFloats { size_t size = nFloats * sizeof(float); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifndef OF_FLOAT_BIG_ENDIAN size_t i; for (i = 0; i < nFloats; i++) @@ -273,17 +273,17 @@ #endif return size; } -- (size_t)readNBigEndianDoubles: (size_t)nDoubles - intoBuffer: (double*)buffer +- (size_t)readBigEndianDoublesIntoBuffer: (double*)buffer + count: (size_t)nDoubles { size_t size = nDoubles * sizeof(double); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifndef OF_FLOAT_BIG_ENDIAN size_t i; for (i = 0; i < nDoubles; i++) @@ -295,63 +295,63 @@ - (uint16_t)readLittleEndianInt16 { uint16_t ret; - [self readExactlyNBytes: 2 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 2]; return of_bswap16_if_be(ret); } - (uint32_t)readLittleEndianInt32 { uint32_t ret; - [self readExactlyNBytes: 4 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 4]; return of_bswap32_if_be(ret); } - (uint64_t)readLittleEndianInt64 { uint64_t ret; - [self readExactlyNBytes: 8 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 8]; return of_bswap64_if_be(ret); } - (float)readLittleEndianFloat { float ret; - [self readExactlyNBytes: 4 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 4]; return of_bswap_float_if_be(ret); } - (double)readLittleEndianDouble { double ret; - [self readExactlyNBytes: 8 - intoBuffer: (char*)&ret]; + [self readIntoBuffer: (char*)&ret + exactLength: 8]; return of_bswap_double_if_be(ret); } -- (size_t)readNLittleEndianInt16s: (size_t)nInt16s - intoBuffer: (uint16_t*)buffer +- (size_t)readLittleEndianInt16sIntoBuffer: (uint16_t*)buffer + count: (size_t)nInt16s { size_t size = nInt16s * sizeof(uint16_t); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifdef OF_BIG_ENDIAN size_t i; for (i = 0; i < nInt16s; i++) @@ -359,17 +359,17 @@ #endif return size; } -- (size_t)readNLittleEndianInt32s: (size_t)nInt32s - intoBuffer: (uint32_t*)buffer +- (size_t)readLittleEndianInt32sIntoBuffer: (uint32_t*)buffer + count: (size_t)nInt32s { size_t size = nInt32s * sizeof(uint32_t); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifdef OF_BIG_ENDIAN size_t i; for (i = 0; i < nInt32s; i++) @@ -377,17 +377,17 @@ #endif return size; } -- (size_t)readNLittleEndianInt64s: (size_t)nInt64s - intoBuffer: (uint64_t*)buffer +- (size_t)readLittleEndianInt64sIntoBuffer: (uint64_t*)buffer + count: (size_t)nInt64s { size_t size = nInt64s * sizeof(uint64_t); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifdef OF_BIG_ENDIAN size_t i; for (i = 0; i < nInt64s; i++) @@ -395,17 +395,17 @@ #endif return size; } -- (size_t)readNLittleEndianFloats: (size_t)nFloats - intoBuffer: (float*)buffer +- (size_t)readLittleEndianFloatsIntoBuffer: (float*)buffer + count: (size_t)nFloats { size_t size = nFloats * sizeof(float); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifdef OF_FLOAT_BIG_ENDIAN size_t i; for (i = 0; i < nFloats; i++) @@ -413,17 +413,17 @@ #endif return size; } -- (size_t)readNLittleEndianDoubles: (size_t)nDoubles - intoBuffer: (double*)buffer +- (size_t)readLittleEndianDoublesIntoBuffer: (double*)buffer + count: (size_t)nDoubles { size_t size = nDoubles * sizeof(double); - [self readExactlyNBytes: size - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: size]; #ifdef OF_FLOAT_BIG_ENDIAN size_t i; for (i = 0; i < nDoubles; i++) @@ -431,29 +431,29 @@ #endif return size; } -- (OFDataArray*)readDataArrayWithNItems: (size_t)nItems +- (OFDataArray*)readDataArrayWithSize: (size_t)nItems { return [self readDataArrayWithItemSize: 1 - andNItems: nItems]; + count: nItems]; } - (OFDataArray*)readDataArrayWithItemSize: (size_t)itemSize - andNItems: (size_t)nItems + count: (size_t)nItems { OFDataArray *dataArray; char *tmp; dataArray = [OFDataArray dataArrayWithItemSize: itemSize]; - tmp = [self allocMemoryWithItemSize: itemSize - count: nItems]; + tmp = [self allocMemoryWithSize: itemSize + count: nItems]; @try { - [self readExactlyNBytes: nItems * itemSize - intoBuffer: tmp]; + [self readIntoBuffer: tmp + exactLength: nItems * itemSize]; [dataArray addItemsFromCArray: tmp count: nItems]; } @finally { [self freeMemory: tmp]; @@ -472,12 +472,12 @@ @try { while (![self isAtEndOfStream]) { size_t length; - length = [self readNBytes: of_pagesize - intoBuffer: buffer]; + length = [self readIntoBuffer: buffer + length: of_pagesize]; [dataArray addItemsFromCArray: buffer count: length]; } } @finally { [self freeMemory: buffer]; @@ -486,24 +486,24 @@ return dataArray; } - (OFString*)readStringWithLength: (size_t)length { - return [self readStringWithEncoding: OF_STRING_ENCODING_UTF_8 - length: length]; + return [self readStringWithLength: length + encoding: OF_STRING_ENCODING_UTF_8]; } -- (OFString*)readStringWithEncoding: (of_string_encoding_t)encoding - length: (size_t)length +- (OFString*)readStringWithLength: (size_t)length + encoding: (of_string_encoding_t)encoding { OFString *ret; char *buffer = [self allocMemoryWithSize: length + 1]; buffer[length] = 0; @try { - [self readExactlyNBytes: length - intoBuffer: buffer]; + [self readIntoBuffer: buffer + exactLength: length]; ret = [OFString stringWithCString: buffer encoding: encoding]; } @finally { [self freeMemory: buffer]; @@ -573,12 +573,12 @@ waitingForDelimiter = NO; return ret; } - bufferLength = [self _readNBytes: of_pagesize - intoBuffer: buffer]; + bufferLength = [self _readIntoBuffer: buffer + length: of_pagesize]; /* Look if there's a newline or \0 */ for (i = 0; i < bufferLength; i++) { if (OF_UNLIKELY(buffer[i] == '\n' || buffer[i] == '\0')) { @@ -607,11 +607,11 @@ * Append data to cache to prevent loss * of data due to wrong encoding. */ cache = [self resizeMemory: cache - toSize: cacheLength + + size: cacheLength + bufferLength]; if (cache != NULL) memcpy(cache + cacheLength, buffer, bufferLength); @@ -638,15 +638,15 @@ } } /* There was no newline or \0 */ cache = [self resizeMemory: cache - toSize: cacheLength + bufferLength]; + size: cacheLength + bufferLength]; /* * It's possible that cacheLen + len is 0 and thus cache was - * set to NULL by resizeMemory:toSize:. + * set to NULL by resizeMemory:size:. */ if (cache != NULL) memcpy(cache + cacheLength, buffer, bufferLength); cacheLength += bufferLength; @@ -678,11 +678,11 @@ { return [self tryReadLineWithEncoding: OF_STRING_ENCODING_UTF_8]; } - (OFString*)tryReadTillDelimiter: (OFString*)delimiter - withEncoding: (of_string_encoding_t)encoding + encoding: (of_string_encoding_t)encoding { const char *delimiterUTF8String; size_t i, j, delimiterLength, bufferLength, retLength; char *retCString, *buffer, *newCache; OFString *ret; @@ -747,12 +747,12 @@ waitingForDelimiter = NO; return ret; } - bufferLength = [self _readNBytes: of_pagesize - intoBuffer: buffer]; + bufferLength = [self _readIntoBuffer: buffer + length: of_pagesize]; /* Look if there's a delimiter or \0 */ for (i = 0; i < bufferLength; i++) { if (buffer[i] != delimiterUTF8String[j++]) j = 0; @@ -801,15 +801,15 @@ } } /* Neither the delimiter nor \0 was found */ cache = [self resizeMemory: cache - toSize: cacheLength + bufferLength]; + size: cacheLength + bufferLength]; /* * It's possible that cacheLen + len is 0 and thus cache was - * set to NULL by resizeMemory:toSize:. + * set to NULL by resizeMemory:size:. */ if (cache != NULL) memcpy(cache + cacheLength, buffer, bufferLength); @@ -824,258 +824,258 @@ - (OFString*)readTillDelimiter: (OFString*)delimiter { return [self readTillDelimiter: delimiter - withEncoding: OF_STRING_ENCODING_UTF_8]; + encoding: OF_STRING_ENCODING_UTF_8]; } - (OFString*)readTillDelimiter: (OFString*)delimiter - withEncoding: (of_string_encoding_t)encoding + encoding: (of_string_encoding_t)encoding { OFString *ret = nil; while ((ret = [self tryReadTillDelimiter: delimiter - withEncoding: encoding]) == nil) + encoding: encoding]) == nil) if ([self isAtEndOfStream]) return nil; return ret; } - (OFString*)tryReadTillDelimiter: (OFString*)delimiter { return [self tryReadTillDelimiter: delimiter - withEncoding: OF_STRING_ENCODING_UTF_8]; + encoding: OF_STRING_ENCODING_UTF_8]; } -- (BOOL)buffersWrites +- (BOOL)writeBufferEnabled { - return buffersWrites; + return writeBufferEnabled; } -- (void)setBuffersWrites: (BOOL)enable +- (void)setWriteBufferEnabled: (BOOL)enable { - buffersWrites = enable; + writeBufferEnabled = enable; } - (void)flushWriteBuffer { if (writeBuffer == NULL) return; - [self _writeNBytes: writeBufferLength - fromBuffer: writeBuffer]; + [self _writeBuffer: writeBuffer + length: writeBufferLength]; [self freeMemory: writeBuffer]; writeBuffer = NULL; writeBufferLength = 0; } -- (void)writeNBytes: (size_t)length - fromBuffer: (const void*)buffer +- (void)writeBuffer: (const void*)buffer + length: (size_t)length { - if (!buffersWrites) - [self _writeNBytes: length - fromBuffer: buffer]; + if (!writeBufferEnabled) + [self _writeBuffer: buffer + length: length]; else { writeBuffer = [self resizeMemory: writeBuffer - toSize: writeBufferLength + length]; + size: writeBufferLength + length]; memcpy(writeBuffer + writeBufferLength, buffer, length); writeBufferLength += length; } } - (void)writeInt8: (uint8_t)int8 { - [self writeNBytes: 1 - fromBuffer: (char*)&int8]; + [self writeBuffer: (char*)&int8 + length: 1]; } - (void)writeBigEndianInt16: (uint16_t)int16 { int16 = of_bswap16_if_le(int16); - [self writeNBytes: 2 - fromBuffer: (char*)&int16]; + [self writeBuffer: (char*)&int16 + length: 2]; } - (void)writeBigEndianInt32: (uint32_t)int32 { int32 = of_bswap32_if_le(int32); - [self writeNBytes: 4 - fromBuffer: (char*)&int32]; + [self writeBuffer: (char*)&int32 + length: 4]; } - (void)writeBigEndianInt64: (uint64_t)int64 { int64 = of_bswap64_if_le(int64); - [self writeNBytes: 8 - fromBuffer: (char*)&int64]; + [self writeBuffer: (char*)&int64 + length: 8]; } - (void)writeBigEndianFloat: (float)float_ { float_ = of_bswap_float_if_le(float_); - [self writeNBytes: 4 - fromBuffer: (char*)&float_]; + [self writeBuffer: (char*)&float_ + length: 4]; } - (void)writeBigEndianDouble: (double)double_ { double_ = of_bswap_double_if_le(double_); - [self writeNBytes: 8 - fromBuffer: (char*)&double_]; + [self writeBuffer: (char*)&double_ + length: 8]; } -- (size_t)writeNBigEndianInt16s: (size_t)nInt16s - fromBuffer: (const uint16_t*)buffer +- (size_t)writeBigEndianInt16s: (const uint16_t*)buffer + count: (size_t)nInt16s { size_t size = nInt16s * sizeof(uint16_t); #ifdef OF_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; + [self writeBuffer: buffer + length: size]; #else uint16_t *tmp; - tmp = [self allocMemoryWithItemSize: sizeof(uint16_t) - count: nInt16s]; + tmp = [self allocMemoryWithSize: sizeof(uint16_t) + count: nInt16s]; @try { size_t i; for (i = 0; i < nInt16s; i++) tmp[i] = of_bswap16(buffer[i]); - [self writeNBytes: size - fromBuffer: tmp]; + [self writeBuffer: tmp + length: size]; } @finally { [self freeMemory: tmp]; } #endif return size; } -- (size_t)writeNBigEndianInt32s: (size_t)nInt32s - fromBuffer: (const uint32_t*)buffer +- (size_t)writeBigEndianInt32s: (const uint32_t*)buffer + count: (size_t)nInt32s { size_t size = nInt32s * sizeof(uint32_t); #ifdef OF_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; + [self writeBuffer: buffer + length: size]; #else uint32_t *tmp; - tmp = [self allocMemoryWithItemSize: sizeof(uint32_t) - count: nInt32s]; + tmp = [self allocMemoryWithSize: sizeof(uint32_t) + count: nInt32s]; @try { size_t i; for (i = 0; i < nInt32s; i++) tmp[i] = of_bswap32(buffer[i]); - [self writeNBytes: size - fromBuffer: tmp]; + [self writeBuffer: tmp + length: size]; } @finally { [self freeMemory: tmp]; } #endif return size; } -- (size_t)writeNBigEndianInt64s: (size_t)nInt64s - fromBuffer: (const uint64_t*)buffer +- (size_t)writeBigEndianInt64s: (const uint64_t*)buffer + count: (size_t)nInt64s { size_t size = nInt64s * sizeof(uint64_t); #ifdef OF_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; + [self writeBuffer: buffer + length: size]; #else uint64_t *tmp; - tmp = [self allocMemoryWithItemSize: sizeof(uint64_t) - count: nInt64s]; + tmp = [self allocMemoryWithSize: sizeof(uint64_t) + count: nInt64s]; @try { size_t i; for (i = 0; i < nInt64s; i++) tmp[i] = of_bswap64(buffer[i]); - [self writeNBytes: size - fromBuffer: tmp]; + [self writeBuffer: tmp + length: size]; } @finally { [self freeMemory: tmp]; } #endif return size; } -- (size_t)writeNBigEndianFloats: (size_t)nFloats - fromBuffer: (const float*)buffer +- (size_t)writeBigEndianFloats: (const float*)buffer + count: (size_t)nFloats { size_t size = nFloats * sizeof(float); #ifdef OF_FLOAT_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; + [self writeBuffer: buffer + length: size]; #else float *tmp; - tmp = [self allocMemoryWithItemSize: sizeof(float) - count: nFloats]; + tmp = [self allocMemoryWithSize: sizeof(float) + count: nFloats]; @try { size_t i; for (i = 0; i < nFloats; i++) tmp[i] = of_bswap_float(buffer[i]); - [self writeNBytes: size - fromBuffer: tmp]; + [self writeBuffer: tmp + length: size]; } @finally { [self freeMemory: tmp]; } #endif return size; } -- (size_t)writeNBigEndianDoubles: (size_t)nDoubles - fromBuffer: (const double*)buffer +- (size_t)writeBigEndianDoubles: (const double*)buffer + count: (size_t)nDoubles { size_t size = nDoubles * sizeof(double); #ifdef OF_FLOAT_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; + [self writeBuffer: buffer + length: size]; #else double *tmp; - tmp = [self allocMemoryWithItemSize: sizeof(double) - count: nDoubles]; + tmp = [self allocMemoryWithSize: sizeof(double) + count: nDoubles]; @try { size_t i; for (i = 0; i < nDoubles; i++) tmp[i] = of_bswap_double(buffer[i]); - [self writeNBytes: size - fromBuffer: tmp]; + [self writeBuffer: tmp + length: size]; } @finally { [self freeMemory: tmp]; } #endif @@ -1084,188 +1084,188 @@ - (void)writeLittleEndianInt16: (uint16_t)int16 { int16 = of_bswap16_if_be(int16); - [self writeNBytes: 2 - fromBuffer: (char*)&int16]; + [self writeBuffer: (char*)&int16 + length: 2]; } - (void)writeLittleEndianInt32: (uint32_t)int32 { int32 = of_bswap32_if_be(int32); - [self writeNBytes: 4 - fromBuffer: (char*)&int32]; + [self writeBuffer: (char*)&int32 + length: 4]; } - (void)writeLittleEndianInt64: (uint64_t)int64 { int64 = of_bswap64_if_be(int64); - [self writeNBytes: 8 - fromBuffer: (char*)&int64]; + [self writeBuffer: (char*)&int64 + length: 8]; } - (void)writeLittleEndianFloat: (float)float_ { float_ = of_bswap_float_if_be(float_); - [self writeNBytes: 4 - fromBuffer: (char*)&float_]; + [self writeBuffer: (char*)&float_ + length: 4]; } - (void)writeLittleEndianDouble: (double)double_ { double_ = of_bswap_double_if_be(double_); - [self writeNBytes: 8 - fromBuffer: (char*)&double_]; + [self writeBuffer: (char*)&double_ + length: 8]; } -- (size_t)writeNLittleEndianInt16s: (size_t)nInt16s - fromBuffer: (const uint16_t*)buffer +- (size_t)writeLittleEndianInt16s: (const uint16_t*)buffer + count: (size_t)nInt16s { size_t size = nInt16s * sizeof(uint16_t); #ifndef OF_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; + [self writeBuffer: buffer + length: size]; #else uint16_t *tmp; - tmp = [self allocMemoryWithItemSize: sizeof(uint16_t) - count: nInt16s]; + tmp = [self allocMemoryWithSize: sizeof(uint16_t) + count: nInt16s]; @try { size_t i; for (i = 0; i < nInt16s; i++) tmp[i] = of_bswap16(buffer[i]); - [self writeNBytes: size - fromBuffer: tmp]; + [self writeBuffer: tmp + length: size]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeLittleEndianInt32s: (const uint32_t*)buffer + count: (size_t)nInt32s +{ + size_t size = nInt32s * sizeof(uint32_t); + +#ifndef OF_BIG_ENDIAN + [self writeBuffer: buffer + length: size]; +#else + uint32_t *tmp; + + tmp = [self allocMemoryWithSize: sizeof(uint32_t) + count: nInt32s]; + + @try { + size_t i; + + for (i = 0; i < nInt32s; i++) + tmp[i] = of_bswap32(buffer[i]); + + [self writeBuffer: tmp + length: size]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeLittleEndianInt64s: (const uint64_t*)buffer + count: (size_t)nInt64s +{ + size_t size = nInt64s * sizeof(uint64_t); + +#ifndef OF_BIG_ENDIAN + [self writeBuffer: buffer + length: size]; +#else + uint64_t *tmp; + + tmp = [self allocMemoryWithSize: sizeof(uint64_t) + count: nInt64s]; + + @try { + size_t i; + + for (i = 0; i < nInt64s; i++) + tmp[i] = of_bswap64(buffer[i]); + + [self writeBuffer: tmp + length: size]; } @finally { [self freeMemory: tmp]; } #endif return size; } -- (size_t)writeNLittleEndianInt32s: (size_t)nInt32s - fromBuffer: (const uint32_t*)buffer -{ - size_t size = nInt32s * sizeof(uint32_t); - -#ifndef OF_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; +- (size_t)writeLittleEndianFloats: (const float*)buffer + count: (size_t)nFloats +{ + size_t size = nFloats * sizeof(float); + +#ifndef OF_FLOAT_BIG_ENDIAN + [self writeBuffer: buffer + length: size]; #else - uint32_t *tmp; + float *tmp; - tmp = [self allocMemoryWithItemSize: sizeof(uint32_t) - count: nInt32s]; + tmp = [self allocMemoryWithSize: sizeof(float) + count: nFloats]; @try { size_t i; - for (i = 0; i < nInt32s; i++) - tmp[i] = of_bswap32(buffer[i]); + for (i = 0; i < nFloats; i++) + tmp[i] = of_bswap_float(buffer[i]); - [self writeNBytes: size - fromBuffer: tmp]; + [self writeBuffer: tmp + length: size]; } @finally { [self freeMemory: tmp]; } #endif return size; } -- (size_t)writeNLittleEndianInt64s: (size_t)nInt64s - fromBuffer: (const uint64_t*)buffer -{ - size_t size = nInt64s * sizeof(uint64_t); - -#ifndef OF_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; -#else - uint64_t *tmp; - - tmp = [self allocMemoryWithItemSize: sizeof(uint64_t) - count: nInt64s]; - - @try { - size_t i; - - for (i = 0; i < nInt64s; i++) - tmp[i] = of_bswap64(buffer[i]); - - [self writeNBytes: size - fromBuffer: tmp]; - } @finally { - [self freeMemory: tmp]; - } -#endif - - return size; -} - -- (size_t)writeNLittleEndianFloats: (size_t)nFloats - fromBuffer: (const float*)buffer -{ - size_t size = nFloats * sizeof(float); - -#ifndef OF_FLOAT_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; -#else - float *tmp; - - tmp = [self allocMemoryWithItemSize: sizeof(float) - count: nFloats]; - - @try { - size_t i; - - for (i = 0; i < nFloats; i++) - tmp[i] = of_bswap_float(buffer[i]); - - [self writeNBytes: size - fromBuffer: tmp]; - } @finally { - [self freeMemory: tmp]; - } -#endif - - return size; -} - -- (size_t)writeNLittleEndianDoubles: (size_t)nDoubles - fromBuffer: (const double*)buffer +- (size_t)writeLittleEndianDoubles: (const double*)buffer + count: (size_t)nDoubles { size_t size = nDoubles * sizeof(double); #ifndef OF_FLOAT_BIG_ENDIAN - [self writeNBytes: size - fromBuffer: buffer]; + [self writeBuffer: buffer + length: size]; #else double *tmp; - tmp = [self allocMemoryWithItemSize: sizeof(double) - count: nDoubles]; + tmp = [self allocMemoryWithSize: sizeof(double) + count: nDoubles]; @try { size_t i; for (i = 0; i < nDoubles; i++) tmp[i] = of_bswap_double(buffer[i]); - [self writeNBytes: size - fromBuffer: tmp]; + [self writeBuffer: tmp + length: size]; } @finally { [self freeMemory: tmp]; } #endif @@ -1274,22 +1274,22 @@ - (size_t)writeDataArray: (OFDataArray*)dataArray { size_t length = [dataArray count] * [dataArray itemSize]; - [self writeNBytes: length - fromBuffer: [dataArray cArray]]; + [self writeBuffer: [dataArray cArray] + length: length]; return [dataArray count] * [dataArray itemSize]; } - (size_t)writeString: (OFString*)string { size_t length = [string UTF8StringLength]; - [self writeNBytes: length - fromBuffer: [string UTF8String]]; + [self writeBuffer: [string UTF8String] + length: length]; return length; } - (size_t)writeLine: (OFString*)string @@ -1301,12 +1301,12 @@ @try { memcpy(buffer, [string UTF8String], stringLength); buffer[stringLength] = '\n'; - [self writeNBytes: stringLength + 1 - fromBuffer: buffer]; + [self writeBuffer: buffer + length: stringLength + 1]; } @finally { [self freeMemory: buffer]; } return stringLength + 1; @@ -1317,18 +1317,18 @@ va_list arguments; size_t ret; va_start(arguments, format); ret = [self writeFormat: format - withArguments: arguments]; + arguments: arguments]; va_end(arguments); return ret; } - (size_t)writeFormat: (OFConstantString*)format - withArguments: (va_list)arguments + arguments: (va_list)arguments { char *UTF8String; int length; if (format == nil) @@ -1338,12 +1338,12 @@ if ((length = of_vasprintf(&UTF8String, [format UTF8String], arguments)) == -1) @throw [OFInvalidFormatException exceptionWithClass: isa]; @try { - [self writeNBytes: length - fromBuffer: UTF8String]; + [self writeBuffer: UTF8String + length: length]; } @finally { free(UTF8String); } return length; Index: src/OFStreamObserver.m ================================================================== --- src/OFStreamObserver.m +++ src/OFStreamObserver.m @@ -137,12 +137,12 @@ @throw [OFInitializationFailedException exceptionWithClass: isa]; #endif maxFD = cancelFD[0]; - FDToStream = [self allocMemoryWithItemSize: sizeof(OFStream*) - count: maxFD + 1]; + FDToStream = [self allocMemoryWithSize: sizeof(OFStream*) + count: maxFD + 1]; FDToStream[cancelFD[0]] = nil; #ifdef OF_THREADS mutex = [[OFMutex alloc] init]; #endif @@ -310,11 +310,11 @@ if ((action & QUEUE_ACTION) == QUEUE_ADD) { if (fd > maxFD) { maxFD = fd; FDToStream = [self resizeMemory: FDToStream - itemSize: sizeof(OFStream*) + size: sizeof(OFStream*) count: maxFD + 1]; } FDToStream[fd] = stream; } Index: src/OFStreamSocket.m ================================================================== --- src/OFStreamSocket.m +++ src/OFStreamSocket.m @@ -68,12 +68,12 @@ - (BOOL)_isAtEndOfStream { return atEndOfStream; } -- (size_t)_readNBytes: (size_t)length - intoBuffer: (void*)buffer +- (size_t)_readIntoBuffer: (void*)buffer + length: (size_t)length { ssize_t ret; if (sock == INVALID_SOCKET) @throw [OFNotConnectedException exceptionWithClass: isa @@ -103,12 +103,12 @@ atEndOfStream = YES; return ret; } -- (void)_writeNBytes: (size_t)length - fromBuffer: (const void*)buffer +- (void)_writeBuffer: (const void*)buffer + length: (size_t)length { if (sock == INVALID_SOCKET) @throw [OFNotConnectedException exceptionWithClass: isa socket: self]; Index: src/OFString+JSONValue.m ================================================================== --- src/OFString+JSONValue.m +++ src/OFString+JSONValue.m @@ -25,28 +25,35 @@ #import "OFArray.h" #import "OFDictionary.h" #import "OFNumber.h" #import "OFNull.h" -#import "OFInvalidEncodingException.h" +#import "OFInvalidJSONException.h" #import "macros.h" int _OFString_JSONValue_reference; -static id nextObject(const char *restrict *, const char*); +static id nextObject(const char *restrict *, const char*, + size_t *restrict line); static void -skipWhitespaces(const char *restrict *pointer, const char *stop) +skipWhitespaces(const char *restrict *pointer, const char *stop, + size_t *restrict line) { while (*pointer < stop && (**pointer == ' ' || **pointer == '\t' || - **pointer == '\r' || **pointer == '\n')) + **pointer == '\r' || **pointer == '\n')) { + if (**pointer == '\n') + (*line)++; + (*pointer)++; + } } static void -skipComment(const char *restrict *pointer, const char *stop) +skipComment(const char *restrict *pointer, const char *stop, + size_t *restrict line) { if (**pointer != '/') return; if (*pointer + 1 >= stop) @@ -64,37 +71,43 @@ (*pointer)++; return; } lastIsAsterisk = (**pointer == '*'); + + if (**pointer == '\n') + (*line)++; (*pointer)++; } - } else { + } else if (**pointer == '/') { (*pointer)++; while (*pointer < stop) { if (**pointer == '\r' || **pointer == '\n') { (*pointer)++; + (*line)++; return; } (*pointer)++; } - } + } else + (*pointer)--; } static void -skipWhitespacesAndComments(const char *restrict *pointer, const char *stop) +skipWhitespacesAndComments(const char *restrict *pointer, const char *stop, + size_t *restrict line) { const char *old = NULL; while (old != *pointer) { old = *pointer; - skipWhitespaces(pointer, stop); - skipComment(pointer, stop); + skipWhitespaces(pointer, stop, line); + skipComment(pointer, stop, line); } } static inline uint16_t parseUnicodeEscape(const char *pointer, const char *stop) @@ -124,11 +137,12 @@ return ret; } static inline OFString* -parseString(const char *restrict *pointer, const char *stop) +parseString(const char *restrict *pointer, const char *stop, + size_t *restrict line) { char *buffer; size_t i = 0; char delimiter = **pointer; @@ -231,16 +245,19 @@ break; case '\r': (*pointer)++; - if (*pointer < stop && **pointer == '\n') + if (*pointer < stop && **pointer == '\n') { (*pointer)++; + (*line)++; + } break; case '\n': (*pointer)++; + (*line)++; break; default: free(buffer); return nil; } @@ -258,10 +275,11 @@ (*pointer)++; return ret; /* Newlines in strings are disallowed */ } else if (**pointer == '\n' || **pointer == '\r') { + (*line)++; free(buffer); return nil; } else { buffer[i++] = **pointer; (*pointer)++; @@ -371,49 +389,50 @@ */ return nil; } static inline OFMutableArray* -parseArray(const char *restrict *pointer, const char *stop) +parseArray(const char *restrict *pointer, const char *stop, + size_t *restrict line) { OFMutableArray *array = [OFMutableArray array]; if (++(*pointer) >= stop) return nil; while (**pointer != ']') { id object; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; if (**pointer == ']') break; if (**pointer == ',') { (*pointer)++; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop || **pointer != ']') return nil; break; } - if ((object = nextObject(pointer, stop)) == nil) + if ((object = nextObject(pointer, stop, line)) == nil) return nil; [array addObject: object]; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; if (**pointer == ',') { (*pointer)++; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; } else if (**pointer != ']') return nil; @@ -423,70 +442,71 @@ return array; } static inline OFMutableDictionary* -parseDictionary(const char *restrict *pointer, const char *stop) +parseDictionary(const char *restrict *pointer, const char *stop, + size_t *restrict line) { OFMutableDictionary *dictionary = [OFMutableDictionary dictionary]; if (++(*pointer) >= stop) return nil; while (**pointer != '}') { id key, object; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; if (**pointer == '}') break; if (**pointer == ',') { (*pointer)++; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop || **pointer != '}') return nil; break; } - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer + 1 >= stop) return nil; if ((**pointer >= 'a' && **pointer <= 'z') || (**pointer >= 'A' && **pointer <= 'Z') || **pointer == '_' || **pointer == '$' || **pointer == '\\') key = parseIdentifier(pointer, stop); else - key = nextObject(pointer, stop); + key = nextObject(pointer, stop, line); if (key == nil) return nil; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer + 1 >= stop || **pointer != ':') return nil; (*pointer)++; - if ((object = nextObject(pointer, stop)) == nil) + if ((object = nextObject(pointer, stop, line)) == nil) return nil; [dictionary setObject: object forKey: key]; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; if (**pointer == ',') { (*pointer)++; - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; } else if (**pointer != '}') return nil; @@ -496,11 +516,12 @@ return dictionary; } static inline OFNumber* -parseNumber(const char *restrict *pointer, const char *stop) +parseNumber(const char *restrict *pointer, const char *stop, + size_t *restrict line) { BOOL isHex = (*pointer + 1 < stop && (*pointer)[1] == 'x'); BOOL hasDecimal = NO; size_t i; OFString *string; @@ -511,12 +532,16 @@ hasDecimal = YES; if ((*pointer)[i] == ' ' || (*pointer)[i] == '\t' || (*pointer)[i] == '\r' || (*pointer)[i] == '\n' || (*pointer)[i] == ',' || (*pointer)[i] == ']' || - (*pointer)[i] == '}') + (*pointer)[i] == '}') { + if ((*pointer)[i] == '\n') + (*line)++; + break; + } } string = [[OFString alloc] initWithUTF8String: *pointer length: i]; *pointer += i; @@ -537,25 +562,26 @@ return number; } static id -nextObject(const char *restrict *pointer, const char *stop) +nextObject(const char *restrict *pointer, const char *stop, + size_t *restrict line) { - skipWhitespacesAndComments(pointer, stop); + skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; switch (**pointer) { case '"': case '\'': - return parseString(pointer, stop); + return parseString(pointer, stop, line); case '[': - return parseArray(pointer, stop); + return parseArray(pointer, stop, line); case '{': - return parseDictionary(pointer, stop); + return parseDictionary(pointer, stop, line); case 't': if (*pointer + 3 >= stop) return nil; if (memcmp(*pointer, "true", 4)) @@ -594,11 +620,11 @@ case '7': case '8': case '9': case '-': case '.': - return parseNumber(pointer, stop); + return parseNumber(pointer, stop, line); default: return nil; } } @@ -606,15 +632,17 @@ - (id)JSONValue { const char *pointer = [self UTF8String]; const char *stop = pointer + [self UTF8StringLength]; id object; + size_t line = 1; - object = nextObject(&pointer, stop); - skipWhitespacesAndComments(&pointer, stop); + object = nextObject(&pointer, stop, &line); + skipWhitespacesAndComments(&pointer, stop, &line); if (pointer < stop || object == nil) - @throw [OFInvalidEncodingException exceptionWithClass: isa]; + @throw [OFInvalidJSONException exceptionWithClass: isa + line: line]; return object; } @end Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -830,12 +830,12 @@ mode: @"rb"]; @try { tmp = [self allocMemoryWithSize: (size_t)st.st_size]; - [file readExactlyNBytes: (size_t)st.st_size - intoBuffer: tmp]; + [file readIntoBuffer: tmp + exactLength: (size_t)st.st_size]; } @finally { [file release]; } } @catch (id e) { [self release]; @@ -997,11 +997,11 @@ UTF8String[j] = '\0'; @try { UTF8String = [object resizeMemory: UTF8String - toSize: UTF8StringLength + 1]; + size: UTF8StringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } return UTF8String; @@ -1521,12 +1521,12 @@ int compare; if ((prefixLength = [prefix length]) > [self length]) return NO; - tmp = [self allocMemoryWithItemSize: sizeof(of_unichar_t) - count: prefixLength]; + tmp = [self allocMemoryWithSize: sizeof(of_unichar_t) + count: prefixLength]; @try { OFAutoreleasePool *pool; [self getCharacters: tmp inRange: of_range(0, prefixLength)]; @@ -1555,12 +1555,12 @@ if ((suffixLength = [suffix length]) > [self length]) return NO; length = [self length]; - tmp = [self allocMemoryWithItemSize: sizeof(of_unichar_t) - count: suffixLength]; + tmp = [self allocMemoryWithSize: sizeof(of_unichar_t) + count: suffixLength]; @try { OFAutoreleasePool *pool; [self getCharacters: tmp inRange: of_range(length - suffixLength, @@ -1957,12 +1957,12 @@ { OFObject *object = [[[OFObject alloc] init] autorelease]; size_t length = [self length]; of_unichar_t *ret; - ret = [object allocMemoryWithItemSize: sizeof(of_unichar_t) - count: length + 1]; + ret = [object allocMemoryWithSize: sizeof(of_unichar_t) + count: length + 1]; [self getCharacters: ret inRange: of_range(0, length)]; ret[length] = 0; return ret; @@ -1976,12 +1976,12 @@ size_t length = [self length]; uint16_t *ret; size_t i, j; /* Allocate memory for the worst case */ - ret = [object allocMemoryWithItemSize: sizeof(uint16_t) - count: length * 2 + 1]; + ret = [object allocMemoryWithSize: sizeof(uint16_t) + count: length * 2 + 1]; j = 0; for (i = 0; i < length; i++) { of_unichar_t c = unicodeString[i]; @@ -2000,11 +2000,11 @@ ret[j] = 0; @try { ret = [object resizeMemory: ret - itemSize: sizeof(uint16_t) + size: sizeof(uint16_t) count: j + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } Index: src/OFString_UTF8.m ================================================================== --- src/OFString_UTF8.m +++ src/OFString_UTF8.m @@ -179,11 +179,11 @@ exceptionWithClass: isa]; s->cStringLength += bytes - 1; s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; memcpy(s->cString + j, buffer, bytes); j += bytes; } @@ -228,11 +228,11 @@ @throw [OFInvalidEncodingException exceptionWithClass: isa]; s->cStringLength += characterBytes - 1; s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; memcpy(s->cString + j, buffer, characterBytes); j += characterBytes; } @@ -340,11 +340,11 @@ s->cString[j] = '\0'; @try { s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } } @catch (id e) { [self release]; @@ -448,11 +448,11 @@ s->cString[j] = '\0'; @try { s->cString = [self resizeMemory: s->cString - toSize: s->cStringLength + 1]; + size: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } } @catch (id e) { [self release]; @@ -1092,12 +1092,12 @@ { OFObject *object = [[[OFObject alloc] init] autorelease]; of_unichar_t *ret; size_t i, j; - ret = [object allocMemoryWithItemSize: sizeof(of_unichar_t) - count: s->length + 1]; + ret = [object allocMemoryWithSize: sizeof(of_unichar_t) + count: s->length + 1]; i = 0; j = 0; while (i < s->cStringLength) { Index: src/OFTCPSocket+SOCKS5.m ================================================================== --- src/OFTCPSocket+SOCKS5.m +++ src/OFTCPSocket+SOCKS5.m @@ -27,18 +27,18 @@ - (void)_SOCKS5ConnectToHost: (OFString*)host port: (uint16_t)port { const char request[] = { 5, 1, 0, 3 }; char reply[256]; - BOOL oldBuffersWrites; + BOOL oldWriteBufferEnabled; /* 5 1 0 -> no authentication */ - [self writeNBytes: 3 - fromBuffer: request]; + [self writeBuffer: request + length: 3]; - [self readExactlyNBytes: 2 - intoBuffer: reply]; + [self readIntoBuffer: reply + exactLength: 2]; if (reply[0] != 5 || reply[1] != 0) { [self close]; @throw [OFConnectionFailedException exceptionWithClass: isa @@ -45,29 +45,29 @@ socket: self host: host port: port]; } - oldBuffersWrites = [self buffersWrites]; - [self setBuffersWrites: YES]; + oldWriteBufferEnabled = [self writeBufferEnabled]; + [self setWriteBufferEnabled: YES]; /* CONNECT request */ - [self writeNBytes: 4 - fromBuffer: request]; + [self writeBuffer: request + length: 4]; [self writeInt8: [host cStringLengthWithEncoding: OF_STRING_ENCODING_NATIVE]]; - [self writeNBytes: [host cStringLengthWithEncoding: + [self writeBuffer: [host cStringWithEncoding: OF_STRING_ENCODING_NATIVE] - fromBuffer: [host cStringWithEncoding: + length: [host cStringLengthWithEncoding: OF_STRING_ENCODING_NATIVE]]; [self writeBigEndianInt16: port]; [self flushWriteBuffer]; - [self setBuffersWrites: oldBuffersWrites]; + [self setWriteBufferEnabled: oldWriteBufferEnabled]; - [self readExactlyNBytes: 4 - intoBuffer: reply]; + [self readIntoBuffer: reply + exactLength: 4]; if (reply[0] != 5 || reply[1] != 0 || reply[2] != 0) { [self close]; @throw [OFConnectionFailedException exceptionWithClass: isa socket: self @@ -76,20 +76,20 @@ } /* Skip the rest of the reply */ switch (reply[3]) { case 1: /* IPv4 */ - [self readExactlyNBytes: 4 - intoBuffer: reply]; + [self readIntoBuffer: reply + exactLength: 4]; break; case 3: /* Domainname */ - [self readExactlyNBytes: [self readInt8] - intoBuffer: reply]; + [self readIntoBuffer: reply + exactLength: [self readInt8]]; break; case 4: /* IPv6 */ - [self readExactlyNBytes: 16 - intoBuffer: reply]; + [self readIntoBuffer: reply + exactLength: 16]; break; default: [self close]; @throw [OFConnectionFailedException exceptionWithClass: isa socket: self Index: src/OFXMLElement.m ================================================================== --- src/OFXMLElement.m +++ src/OFXMLElement.m @@ -513,11 +513,11 @@ if (prefix != nil && ![ns isEqual: defaultNS]) { length += [prefix UTF8StringLength] + 1; @try { cString = [self resizeMemory: cString - toSize: length]; + size: length]; } @catch (id e) { [self freeMemory: cString]; @throw e; } @@ -534,11 +534,11 @@ if (prefix == nil && ((ns != nil && ![ns isEqual: defaultNS]) || (ns == nil && defaultNS != nil))) { length += [ns UTF8StringLength] + 9; @try { cString = [self resizeMemory: cString - toSize: length]; + size: length]; } @catch (id e) { [self freeMemory: cString]; @throw e; } @@ -573,11 +573,11 @@ [attributePrefix UTF8StringLength] + 1 : 0) + [tmp UTF8StringLength] + 4; @try { cString = [self resizeMemory: cString - toSize: length]; + size: length]; } @catch (id e) { [self freeMemory: cString]; @throw e; } @@ -649,11 +649,11 @@ length += [tmp count] + [name UTF8StringLength] + 2 + (indent ? level * indentation : 0); @try { cString = [self resizeMemory: cString - toSize: length]; + size: length]; } @catch (id e) { [self freeMemory: cString]; @throw e; } @@ -671,11 +671,11 @@ cString[i++] = '/'; if (prefix != nil) { length += [prefix UTF8StringLength] + 1; @try { cString = [self resizeMemory: cString - toSize: length]; + size: length]; } @catch (id e) { [self freeMemory: cString]; @throw e; } Index: src/OFXMLParser.h ================================================================== --- src/OFXMLParser.h +++ src/OFXMLParser.h @@ -199,11 +199,11 @@ * * \param buffer The buffer to parse * \param length The length of the buffer */ - (void)parseBuffer: (const char*)buffer - withLength: (size_t)length; + length: (size_t)length; /** * \brief Parses the specified string. * * \param string The string to parse Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -233,11 +233,11 @@ { delegate = delegate_; } - (void)parseBuffer: (const char*)buffer - withLength: (size_t)length + length: (size_t)length { size_t i, last = 0; for (i = 0; i < length; i++) { size_t j = i; @@ -261,24 +261,24 @@ } - (void)parseString: (OFString*)string { [self parseBuffer: [string UTF8String] - withLength: [string UTF8StringLength]]; + length: [string UTF8StringLength]]; } - (void)parseStream: (OFStream*)stream { char *buffer = [self allocMemoryWithSize: of_pagesize]; @try { while (![stream isAtEndOfStream]) { - size_t length = [stream readNBytes: of_pagesize - intoBuffer: buffer]; + size_t length = [stream readIntoBuffer: buffer + length: of_pagesize]; [self parseBuffer: buffer - withLength: length]; + length: length]; } } @finally { [self freeMemory: buffer]; } } Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -93,10 +93,11 @@ #import "OFHTTPRequestFailedException.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidEncodingException.h" #import "OFInvalidFormatException.h" +#import "OFInvalidJSONException.h" #import "OFInvalidServerReplyException.h" #import "OFLinkFailedException.h" #import "OFListenFailedException.h" #import "OFMalformedXMLException.h" #import "OFMemoryNotPartOfObjectException.h" Index: src/exceptions/Makefile ================================================================== --- src/exceptions/Makefile +++ src/exceptions/Makefile @@ -26,10 +26,11 @@ OFHashAlreadyCalculatedException.m \ OFInitializationFailedException.m \ OFInvalidArgumentException.m \ OFInvalidEncodingException.m \ OFInvalidFormatException.m \ + OFInvalidJSONException.m \ OFInvalidServerReplyException.m \ OFLinkFailedException.m \ OFListenFailedException.m \ OFMalformedXMLException.m \ OFMemoryNotPartOfObjectException.m \ ADDED src/exceptions/OFInvalidJSONException.h Index: src/exceptions/OFInvalidJSONException.h ================================================================== --- src/exceptions/OFInvalidJSONException.h +++ src/exceptions/OFInvalidJSONException.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFException.h" + +/** + * \brief An exception indicating a JSON representation is invalid. + */ +@interface OFInvalidJSONException: OFException +{ + size_t line; +} + +#ifdef OF_HAVE_PROPERTIES +@property (readonly) size_t line; +#endif + +/** + * \param class_ The class of the object which caused the exception + * \param line The line in which the parsing error encountered + * \return A new invalid JSON exception + */ ++ exceptionWithClass: (Class)class_ + line: (size_t)line; + +/** + * Initializes an already allocated invalid JSON exception. + * + * \param class_ The class of the object which caused the exception + * \param line The line in which the parsing error encountered + * \return An initialized invalid JSON exception + */ +- initWithClass: (Class)class_ + line: (size_t)line; + +/** + * \return The line in which parsing the JSON representation failed + */ +- (size_t)line; +@end ADDED src/exceptions/OFInvalidJSONException.m Index: src/exceptions/OFInvalidJSONException.m ================================================================== --- src/exceptions/OFInvalidJSONException.m +++ src/exceptions/OFInvalidJSONException.m @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFInvalidJSONException.h" +#import "OFString.h" + +#import "OFNotImplementedException.h" + +@implementation OFInvalidJSONException ++ exceptionWithClass: (Class)class_ + line: (size_t)line +{ + return [[[self alloc] initWithClass: class_ + line: line] autorelease]; +} + +- init +{ + Class c = isa; + [self release]; + @throw [OFNotImplementedException exceptionWithClass: c + selector: _cmd]; +} + +- initWithClass: (Class)class_ + line: (size_t)line_ +{ + self = [super initWithClass: class_]; + + line = line_; + + return self; +} + +- (OFString*)description +{ + if (description != nil) + return description; + + description = [[OFString alloc] initWithFormat: + @"The JSON representation class %@ tried to parse is invalid in " + @"line %zd!", inClass, line]; + + return description; +} + +- (size_t)line +{ + return line; +} +@end Index: tests/OFJSONTests.m ================================================================== --- tests/OFJSONTests.m +++ tests/OFJSONTests.m @@ -21,11 +21,11 @@ #import "OFDictionary.h" #import "OFNumber.h" #import "OFNull.h" #import "OFAutoreleasePool.h" -#import "OFInvalidEncodingException.h" +#import "OFInvalidJSONException.h" #import "TestsAppDelegate.h" static OFString *module = @"OFJSON"; @@ -49,17 +49,17 @@ TEST(@"-[JSONValue #1]", [[s JSONValue] isEqual: d]) TEST(@"-[JSONRepresentation]", [[d JSONRepresentation] isEqual: @"{\"foo\":\"ba\\r\",\"x\":[0.5,15,null,\"foo\",false]}"]) - EXPECT_EXCEPTION(@"-[JSONValue #2]", OFInvalidEncodingException, + EXPECT_EXCEPTION(@"-[JSONValue #2]", OFInvalidJSONException, [@"{" JSONValue]) - EXPECT_EXCEPTION(@"-[JSONValue #3]", OFInvalidEncodingException, + EXPECT_EXCEPTION(@"-[JSONValue #3]", OFInvalidJSONException, [@"]" JSONValue]) - EXPECT_EXCEPTION(@"-[JSONValue #4]", OFInvalidEncodingException, + EXPECT_EXCEPTION(@"-[JSONValue #4]", OFInvalidJSONException, [@"bar" JSONValue]) - EXPECT_EXCEPTION(@"-[JSONValue #5]", OFInvalidEncodingException, + EXPECT_EXCEPTION(@"-[JSONValue #5]", OFInvalidJSONException, [@"[\"a\" \"b\"]" JSONValue]) [pool drain]; } @end Index: tests/OFMD5HashTests.m ================================================================== --- tests/OFMD5HashTests.m +++ tests/OFMD5HashTests.m @@ -42,12 +42,12 @@ TEST(@"+[MD5Hash]", (md5 = [OFMD5Hash MD5Hash])) while (![f isAtEndOfStream]) { char buf[64]; - size_t len = [f readNBytes: 64 - intoBuffer: buf]; + size_t len = [f readIntoBuffer: buf + length: 64]; [md5 updateWithBuffer: buf length: len]; } [f close]; Index: tests/OFObjectTests.m ================================================================== --- tests/OFObjectTests.m +++ tests/OFObjectTests.m @@ -64,22 +64,22 @@ EXPECT_EXCEPTION(@"Detect out of memory on resize", OFOutOfMemoryException, { p = [obj allocMemoryWithSize: 1]; [obj resizeMemory: p - toSize: SIZE_MAX - 128]; + size: SIZE_MAX - 128]; }) [obj freeMemory: p]; TEST(@"Allocate when trying to resize NULL", (p = [obj resizeMemory: NULL - toSize: 1024]) != NULL) + size: 1024]) != NULL) [obj freeMemory: p]; EXPECT_EXCEPTION(@"Detect resizing of memory not allocated by object", OFMemoryNotPartOfObjectException, [obj resizeMemory: tmp - toSize: 2048]) + size: 2048]) [self freeMemory: tmp]; TEST(@"+[description]", [[OFObject description] isEqual: @"OFObject"] && [[MyObj description] isEqual: @"MyObj"]) Index: tests/OFSHA1HashTests.m ================================================================== --- tests/OFSHA1HashTests.m +++ tests/OFSHA1HashTests.m @@ -43,12 +43,12 @@ TEST(@"+[SHA1Hash]", (sha1 = [OFSHA1Hash SHA1Hash])) while (![f isAtEndOfStream]) { char buf[64]; - size_t len = [f readNBytes: 64 - intoBuffer: buf]; + size_t len = [f readIntoBuffer: buf + length: 64]; [sha1 updateWithBuffer: buf length: len]; } [f close]; Index: tests/OFStreamTests.m ================================================================== --- tests/OFStreamTests.m +++ tests/OFStreamTests.m @@ -36,12 +36,12 @@ - (BOOL)_isAtEndOfStream { return (state > 1 ? YES : NO); } -- (size_t)_readNBytes: (size_t)size - intoBuffer: (void*)buffer +- (size_t)_readIntoBuffer: (void*)buffer + length: (size_t)size { switch (state) { case 0: if (size < 1) return 0; Index: tests/OFTCPSocketTests.m ================================================================== --- tests/OFTCPSocketTests.m +++ tests/OFTCPSocketTests.m @@ -54,12 +54,12 @@ TEST(@"-[remoteAddress]", [[accepted remoteAddress] isEqual: @"127.0.0.1"]) TEST(@"-[writeString:]", [client writeString: @"Hello!"]) - TEST(@"-[readNBytes:intoBuffer:]", [accepted readNBytes: 6 - intoBuffer: buf] && + TEST(@"-[readIntoBuffer:length:]", [accepted readIntoBuffer: buf + length: 6] && !memcmp(buf, "Hello!", 6)) [pool drain]; } @end Index: tests/OFXMLParserTests.m ================================================================== --- tests/OFXMLParserTests.m +++ tests/OFXMLParserTests.m @@ -351,14 +351,14 @@ if ([parser finishedParsing]) abort(); if (j + 2 > len) [parser parseBuffer: str + j - withLength: 1]; + length: 1]; else [parser parseBuffer: str + j - withLength: 2]; + length: 2]; } TEST(@"Checking if everything was parsed", i == 32 && [parser lineNumber] == 18)