2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #import "NGMemoryAllocation.h"
27 @implementation NGCString
29 + (id)stringWithCString:(const char *)_value length:(unsigned)_len {
30 return [[[self alloc] initWithCString:_value length:_len] autorelease];
32 + (id)stringWithCString:(const char *)_value {
33 return [[[self alloc] initWithCString:_value] autorelease];
36 - (id)initWithCString:(const char *)_value length:(unsigned)_len {
37 if ((self = [super init])) {
38 value = NGMallocAtomic(_len + 1);
39 memcpy(value, _value, _len);
45 - (id)initWithCString:(const char *)_value {
46 return [self initWithCString:_value length:strlen(_value)];
50 NGFree(self->value); self->value = NULL;
57 - (unsigned int)length {
58 return [self cStringLength];
61 - (unichar)characterAtIndex:(unsigned int)_idx {
62 return [self charAtIndex:_idx];
67 - (BOOL)isEqual:(id)_object {
68 if ([_object isKindOfClass:[NGCString class]])
69 return [self isEqualToCString:_object];
70 else if ([_object isKindOfClass:[NSString class]])
71 return [self isEqualToString:_object];
75 - (BOOL)isEqualToCString:(NGCString *)_cstr {
76 return (strcmp([_cstr cString], value) == 0);
78 - (BOOL)isEqualToString:(NSString *)_str {
79 return (strcmp([_str cString], value) == 0);
83 unsigned hash = 0, hash2;
86 for(i = 0; i < len; i++) {
89 if((hash2 = hash & 0xf0000000))
90 hash ^= (hash2 >> 24) ^ hash2;
97 - (id)copyWithZone:(NSZone *)_zone {
98 return [[NGCString alloc] initWithCString:value length:len];
100 - (id)mutableCopyWithZone:(NSZone *)_zone {
101 return [[NGMutableCString alloc] initWithCString:value length:len];
106 - (void)encodeWithCoder:(NSCoder *)_coder {
107 [_coder encodeValueOfObjCType:@encode(unsigned int) at:&len];
108 [_coder encodeArrayOfObjCType:@encode(char) count:len at:value];
111 - (id)initWithCoder:(NSCoder *)_decoder {
116 [_decoder decodeValueOfObjCType:@encode(unsigned int) at:&length];
117 buffer = NGMallocAtomic(sizeof(unsigned char) * length);
118 [_decoder decodeArrayOfObjCType:@encode(char) count:length at:buffer];
120 cstr = [self initWithCString:buffer length:length];
122 NGFree(buffer); buffer = NULL;
126 /* getting C strings */
128 - (const char *)cString {
131 - (unsigned int)cStringLength {
135 - (void)getCString:(char *)_buffer {
136 strcpy(_buffer, value);
138 - (void)getCString:(char *)_buffer maxLength:(unsigned int)_maxLength {
139 unsigned int size = (_maxLength > len) ? len : _maxLength;
141 strncpy(_buffer, value, size);
142 _buffer[size] = '\0';
145 - (char)charAtIndex:(unsigned int)_idx {
147 #if LIB_FOUNDATION_LIBRARY
149 [[IndexOutOfRangeException alloc]
150 initWithFormat:@"index %d out of range in string %x of length %d",
154 [NSException raise:NSRangeException
155 format:@"index %d out of range in string %x of length %d",
162 // getting numeric values
164 - (double)doubleValue {
170 - (float)floatValue {
171 return [self doubleValue];
180 - (NSString *)stringValue {
181 return (len > 0) ? [NSString stringWithCString:value] : @"";
186 - (NSString *)description {
187 return [self stringValue];
192 @implementation NGMutableCString
194 static inline void _checkCapacity(NGMutableCString *self, unsigned int _add) {
195 if (self->capacity < (self->len + _add)) {
196 char *old = self->value;
197 unsigned int newCapacity = self->capacity * 2;
199 if (newCapacity < (self->len + _add))
200 newCapacity = self->len + _add;
202 self->value = NGMallocAtomic(newCapacity + 1);
204 memcpy(self->value, old, self->len);
208 self->value[self->len] = '\0';
214 - (id)initWithCString:(const char *)_value length:(unsigned)_len {
215 if ((self = [super initWithCString:_value length:_len])) {
223 - (void)appendString:(id)_str {
224 _checkCapacity(self, [_str cStringLength]);
225 strcat(value, [_str cString]);
228 - (void)appendCString:(const char *)_cstr {
229 int l = strlen(_cstr);
230 _checkCapacity(self, l);
231 strcat(value, _cstr);
234 - (void)appendCString:(const char *)_cstr length:(unsigned)_len {
235 _checkCapacity(self, _len);
236 memcpy(&(value[len]), _cstr, _len);
243 - (void)removeAllContents {