Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -142,10 +142,18 @@ * \param str The string from which the data is written to the stream * \return The number of bytes written */ - (size_t)writeLine: (OFString*)str; +/** + * Writes a formatted string into the stream. + * + * \param fmt A string used as format + * \return The number of bytes written + */ +- (size_t)writeFormat: (OFString*)fmt, ...; + /** * Closes the stream. */ - close; @end Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -9,11 +9,13 @@ * the packaging of this file. */ #include "config.h" +#include #include + #include #import "OFStream.h" #import "OFString.h" #import "OFDataArray.h" @@ -406,10 +408,41 @@ return [self writeNBytes: len + 1 fromBuffer: tmp]; } @finally { [self freeMemory: tmp]; } + + /* Get rid of a warning, never reached anyway */ + assert(0); +} + +- (size_t)writeFormat: (OFString*)fmt, ... +{ + va_list args; + char *t; + size_t len; + + if (fmt == nil) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + va_start(args, fmt); + if ((len = vasprintf(&t, [fmt cString], args)) == -1) { + /* + * This is only the most likely error to happen. Unfortunately, + * there is no good way to check what really happened. + */ + @throw [OFOutOfMemoryException newWithClass: isa]; + } + va_end(args); + + @try { + return [self writeNBytes: len + fromBuffer: t]; + } @finally { + free(t); + } /* Get rid of a warning, never reached anyway */ assert(0); }