ObjFW  Artifact [b821122f3b]

Artifact b821122f3b8a834e7ae16430e29316b652b22a592a38ffc699cbfe0a844c2592:


/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <stdlib.h>
#import <string.h>
#import <wchar.h>
#import "OFWideString.h"

@implementation OFWideString
+ new:(const wchar_t*)wstr
{
	return [[OFWideString alloc] init:wstr];
}

- init
{
	return [self init:NULL];
}

- init:(const wchar_t*)wstr
{
	if ((self = [super init])) {
		if (wstr == NULL) {
			length = 0;
			wstring = NULL;
		} else {
			length = wcslen(wstr);
			if ((wstring =
			    [self getMem:length * sizeof(wchar_t)]) == NULL)
				return NULL;
			memcpy(wstring, wstr, length * sizeof(wchar_t));
		}
	}
	return self;
}

- (wchar_t*)wcString
{
	return wstring;
}

- (size_t)length
{
	return length;
}

- (OFWideString*)setTo:(const wchar_t*)wstr
{
	wchar_t *newstr;
	size_t  newlen;
	
	if (wstr == NULL) {
		[self freeMem:wstring];

		length = 0;
		wstring = NULL;

		return self;
	}

	newlen = wcslen(wstr);
	if ((newstr = [self getMem:newlen * sizeof(wchar_t)]) == NULL)
		return nil;
	memcpy(newstr, wstr, newlen * sizeof(wchar_t));

	if (wstring != NULL)
		[self freeMem:wstring];

	length = newlen;
	wstring = newstr;

	return self;
}

- (OFWideString*)clone
{
	return [OFWideString new:wstring];
}

- (OFWideString*)append: (const wchar_t*)wstr
{
	wchar_t	*newstr;
	size_t	newlen, strlength;

	if (wstr == NULL)
		return [self setTo:wstr];

	strlength = wcslen(wstr);
	newlen = length + strlength;

	if ((newstr = [self resizeMem:wstring toSize:(newlen + 1) * 
				      sizeof(wchar_t)]) == NULL) {
		/* FIXME: Add error handling */
		return nil;
	}

	wstring = newstr;

	memcpy(wstring + length, wstr, strlength);
	wstring[newlen] = '\0';

	length = newlen;

	return self;
}
@end