@@ -9,10 +9,11 @@ * the packaging of this file. */ #import "config.h" +#import #import #import #import #ifdef HAVE_SYS_MMAN_H @@ -103,10 +104,30 @@ + newFromCString: (const char*)str { return [[self alloc] initFromCString: str]; } + ++ newFromFormatCString: (const char*)fmt, ... +{ + id ret; + va_list args; + + va_start(args, fmt); + ret = [[self alloc] initFromFormatCString: fmt + withArguments: args]; + va_end(args); + + return ret; +} + ++ newFromFormatCString: (const char*)fmt + withArguments: (va_list)args +{ + return [[self alloc] initFromFormatCString: fmt + withArguments: args]; +} - init { if ((self = [super init])) { length = 0; @@ -135,10 +156,63 @@ string = [self getMemWithSize: length + 1]; memcpy(string, str, length + 1); } } + + return self; +} + +- initFromFormatCString: (const char*)fmt, ... +{ + id ret; + va_list args; + + va_start(args, fmt); + ret = [self initFromFormatCString: fmt + withArguments: args]; + va_end(args); + + return ret; +} + +- initFromFormatCString: (const char*)fmt + withArguments: (va_list)args +{ + int t; + + if ((self = [super init])) { + if (fmt == NULL) + @throw [OFInvalidFormatException newWithObject: self]; + + if ((t = vasprintf(&string, fmt, args)) == -1) + /* + * This is only the most likely error to happen. + * Unfortunately, as errno isn't always thread-safe, + * there's no good way for us to find out what really + * happened. + */ + @throw [OFNoMemException newWithObject: self]; + length = t; + + switch (check_utf8(string, length)) { + case 1: + is_utf8 = YES; + break; + case -1: + free(string); + [super free]; + @throw [OFInvalidEncodingException newWithObject: self]; + } + + @try { + [self addToMemoryPool: string]; + } @catch (OFException *e) { + free(string); + @throw e; + } + } return self; } - (const char*)cString @@ -196,10 +270,45 @@ memcpy(newstr + length, str, strlength + 1); length = newlen; string = newstr; + + return self; +} + +- appendWithFormatCString: (const char*)fmt, ... +{ + id ret; + va_list args; + + va_start(args, fmt); + ret = [self appendWithFormatCString: fmt + andArguments: args]; + va_end(args); + + return ret; +} + +- appendWithFormatCString: (const char*)fmt + andArguments: (va_list)args +{ + char *t; + + if (fmt == NULL) + @throw [OFInvalidFormatException newWithObject: self]; + + if ((vasprintf(&t, fmt, args)) == -1) + /* + * This is only the most likely error to happen. + * Unfortunately, as errno isn't always thread-safe, there's + * no good way for us to find out what really happened. + */ + @throw [OFNoMemException newWithObject: self]; + + [self appendCString: t]; + free(t); return self; } - reverse