Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -151,17 +151,33 @@ * \param nitem The number of items to write * \return The number of bytes written */ - (size_t)writeNItems: (size_t)nitems ofSize: (size_t)size - fromBuffer: (uint8_t*)buf; + fromBuffer: (const uint8_t*)buf; /** * Writes from a buffer into the file. * * \param buf The buffer from which the data is written to the file * \param size The size of the data that should be written * \return The number of bytes written */ - (size_t)writeNBytes: (size_t)size - fromBuffer: (uint8_t*)buf; + fromBuffer: (const uint8_t*)buf; + +/** + * Writes a C string into the file, without the trailing zero. + * + * \param str The C string from which the data is written to the file + * \return The number of bytes written + */ +- (size_t)writeCString: (const char*)str; + +/** + * Writes a C string into the file, without the trailing zero. + * + * \param str The wide C string from which the data is written to the file + * \return The number of bytes written + */ +- (size_t)writeWideCString: (const wchar_t*)str; @end Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -11,10 +11,12 @@ #import "config.h" #import #import +#import +#import #import #import #import "OFFile.h" @@ -134,11 +136,11 @@ ofSize: 1]; } - (size_t)writeNItems: (size_t)nitems ofSize: (size_t)size - fromBuffer: (uint8_t*)buf + fromBuffer: (const uint8_t*)buf { size_t ret; if ((ret = fwrite(buf, size, nitems, fp)) == 0 && size != 0 && nitems != 0) @@ -148,12 +150,26 @@ return ret; } - (size_t)writeNBytes: (size_t)size - fromBuffer: (uint8_t*)buf + fromBuffer: (const uint8_t*)buf { return [self writeNItems: size ofSize: 1 fromBuffer: buf]; } + +- (size_t)writeCString: (const char*)str +{ + return [self writeNItems: strlen(str) + ofSize: 1 + fromBuffer: (const uint8_t*)str]; +} + +- (size_t)writeWideCString: (const wchar_t*)str +{ + return [self writeNItems: wcslen(str) + ofSize: sizeof(wchar_t) + fromBuffer: (const uint8_t*)str]; +} @end Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -81,20 +81,17 @@ } - (void*)getMemForNItems: (size_t)nitems ofSize: (size_t)size { - size_t memsize; - if (nitems == 0 || size == 0) return NULL; if (nitems > SIZE_MAX / size) [[OFOutOfRangeException newWithObject: self] raise]; - memsize = nitems * size; - return [self getMemWithSize: memsize]; + return [self getMemWithSize: nitems * size]; } - (void*)resizeMem: (void*)ptr toSize: (size_t)size { Index: src/OFSocket.h ================================================================== --- src/OFSocket.h +++ src/OFSocket.h @@ -108,7 +108,7 @@ * \param buf The buffer from which the data is written to the file * \param size The size of the data that should be written * \return The number of bytes written */ - (size_t)writeNBytes: (size_t)size - fromBuffer: (uint8_t*)buf; + fromBuffer: (const uint8_t*)buf; @end Index: src/OFSocket.m ================================================================== --- src/OFSocket.m +++ src/OFSocket.m @@ -12,12 +12,14 @@ #import "config.h" #import #import #import +#import #import "OFSocket.h" +#import "OFExceptions.h" @implementation OFSocketAddress + newWithHost: (const char*)host andPort: (uint16_t)port andFamily: (int)family @@ -148,11 +150,11 @@ return ret; } - (size_t)writeNBytes: (size_t)size - fromBuffer: (uint8_t*)buf + fromBuffer: (const uint8_t*)buf { ssize_t ret; if ((ret = send(sock, buf, size, 0)) < 0) { /* FIXME: Throw exception */ @@ -160,6 +162,23 @@ } /* This is safe, as we already checked < 0 */ return ret; } + +- (size_t)writeCString: (const char*)str +{ + return [self writeNBytes: strlen(str) + fromBuffer: (const uint8_t*)str]; +} + +- (size_t)writeWideCString: (const wchar_t*)str +{ + size_t len = wcslen(str); + + if (len > SIZE_MAX / sizeof(wchar_t)) + [[OFOutOfRangeException newWithObject: self] raise]; + + return [self writeNBytes: len * sizeof(wchar_t) + fromBuffer: (const uint8_t*)str]; +} @end Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -32,14 +32,30 @@ * It is part of the memory pool of the OFFile. */ - (uint8_t*)readNBytes: (size_t)size; /** - * Writes from a buffer into the file. + * Writes from a buffer into the stream. * - * \param buf The buffer from which the data is written to the file + * \param buf The buffer from which the data is written to the stream * \param size The size of the data that should be written * \return The number of bytes written */ - (size_t)writeNBytes: (size_t)size - fromBuffer: (uint8_t*)buf; + fromBuffer: (const uint8_t*)buf; + +/** + * Writes a C string into the stream, without the trailing zero. + * + * \param str The C string from which the data is written to the stream + * \return The number of bytes written + */ +- (size_t)writeCString: (const char*)str; + +/** + * Writes a C string into the stream, without the trailing zero. + * + * \param str The wide C string from which the data is written to the stream + * \return The number of bytes written + */ +- (size_t)writeWideCString: (const wchar_t*)str; @end Index: tests/OFSocket/OFSocket.m ================================================================== --- tests/OFSocket/OFSocket.m +++ tests/OFSocket/OFSocket.m @@ -14,12 +14,10 @@ #import #import "OFSocket.h" #import "OFExceptions.h" -const char *sendstr = "GET / HTTP/1.1\r\nHost: webkeks.org\r\n\r\n"; - int main() { OFSocketAddress *addr; OFSocket *sock; @@ -32,17 +30,16 @@ andProtocol: 0]; sock = [OFSocket new]; [sock connect: addr]; [addr free]; - [sock writeNBytes: strlen(sendstr) - fromBuffer: (uint8_t*)sendstr]; - + [sock writeCString: "GET / HTTP/1.1\r\n" + "Host: webkeks.org\r\n\r\n"]; puts((char*)[sock readNBytes: 1024]); [sock free]; } @catch(OFException *e) { printf("EXCEPTION: %s\n", [e cString]); } return 0; }