]> err.no Git - sope/blob - Recycler/Snippets/NGCString.m
removed NGCString
[sope] / Recycler / Snippets / NGCString.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #import "common.h"
24 #import "NGCString.h"
25 #import "NGMemoryAllocation.h"
26
27 @implementation NGCString
28
29 + (id)stringWithCString:(const char *)_value length:(unsigned)_len {
30   return [[[self alloc] initWithCString:_value length:_len] autorelease];
31 }
32 + (id)stringWithCString:(const char *)_value {
33   return [[[self alloc] initWithCString:_value] autorelease];
34 }
35
36 - (id)initWithCString:(const char *)_value length:(unsigned)_len {
37   if ((self = [super init])) {
38     value = NGMallocAtomic(_len + 1);
39     memcpy(value, _value, _len);
40     value[_len] = '\0';
41     len = _len;
42   }
43   return self;
44 }
45 - (id)initWithCString:(const char *)_value {
46   return [self initWithCString:_value length:strlen(_value)];
47 }
48
49 - (void)dealloc {
50   NGFree(self->value); self->value = NULL;
51   len = 0;
52   [super dealloc];
53 }
54
55 // unicode
56
57 - (unsigned int)length {
58   return [self cStringLength];
59 }
60
61 - (unichar)characterAtIndex:(unsigned int)_idx {
62   return [self charAtIndex:_idx];
63 }
64
65 // comparing
66
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];
72   else
73     return NO;
74 }
75 - (BOOL)isEqualToCString:(NGCString *)_cstr {
76   return (strcmp([_cstr cString], value) == 0);
77 }
78 - (BOOL)isEqualToString:(NSString *)_str {
79   return (strcmp([_str cString], value) == 0);
80 }
81
82 - (unsigned)hash {
83   unsigned hash = 0, hash2;
84   unsigned i;
85
86   for(i = 0; i < len; i++) {
87     hash <<= 4;
88     hash += value[i];
89     if((hash2 = hash & 0xf0000000))
90       hash ^= (hash2 >> 24) ^ hash2;
91   }
92   return hash;
93 }
94
95 /* copying */
96
97 - (id)copyWithZone:(NSZone *)_zone {
98   return [[NGCString alloc] initWithCString:value length:len];
99 }
100 - (id)mutableCopyWithZone:(NSZone *)_zone {
101   return [[NGMutableCString alloc] initWithCString:value length:len];
102 }
103
104 /* coding */
105
106 - (void)encodeWithCoder:(NSCoder *)_coder {
107   [_coder encodeValueOfObjCType:@encode(unsigned int) at:&len];
108   [_coder encodeArrayOfObjCType:@encode(char) count:len at:value];
109 }
110
111 - (id)initWithCoder:(NSCoder *)_decoder {
112   char         *buffer = NULL;
113   unsigned int length;
114   id           cstr = nil;
115
116   [_decoder decodeValueOfObjCType:@encode(unsigned int) at:&length];
117   buffer = NGMallocAtomic(sizeof(unsigned char) * length);
118   [_decoder decodeArrayOfObjCType:@encode(char) count:length at:buffer];
119
120   cstr = [self initWithCString:buffer length:length];
121   
122   NGFree(buffer); buffer = NULL;
123   return cstr;
124 }
125
126 /* getting C strings */
127
128 - (const char *)cString {
129   return value;
130 }
131 - (unsigned int)cStringLength {
132   return len;
133 }
134
135 - (void)getCString:(char *)_buffer {
136   strcpy(_buffer, value);
137 }
138 - (void)getCString:(char *)_buffer maxLength:(unsigned int)_maxLength {
139   unsigned int size = (_maxLength > len) ? len : _maxLength;
140
141   strncpy(_buffer, value, size);
142   _buffer[size] = '\0';
143 }
144
145 - (char)charAtIndex:(unsigned int)_idx {
146   if (_idx >= len) {
147 #if LIB_FOUNDATION_LIBRARY
148     NSException *exc =
149       [[IndexOutOfRangeException alloc]
150           initWithFormat:@"index %d out of range in string %x of length %d",
151             _idx, self, len];
152     [exc raise];
153 #else
154     [NSException raise:NSRangeException
155                  format:@"index %d out of range in string %x of length %d",
156                    _idx, self, len];
157 #endif
158   }
159   return value[_idx];
160 }
161
162 // getting numeric values
163
164 - (double)doubleValue {
165   if (len == 0)
166     return 0.0;
167   else
168     return atof(value);
169 }
170 - (float)floatValue {
171   return [self doubleValue];
172 }
173 - (int)intValue {
174   if (len == 0)
175     return 0;
176   else
177     return atoi(value);
178 }
179
180 - (NSString *)stringValue {
181   return (len > 0) ? [NSString stringWithCString:value] : @"";
182 }
183
184 // description
185
186 - (NSString *)description {
187   return [self stringValue];
188 }
189
190 @end
191
192 @implementation NGMutableCString
193
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;
198
199     if (newCapacity < (self->len + _add))
200       newCapacity = self->len + _add;
201
202     self->value = NGMallocAtomic(newCapacity + 1);
203     if (old) {
204       memcpy(self->value, old, self->len);
205       NGFree(old);
206       old = NULL;
207     }
208     self->value[self->len] = '\0';
209   }
210 }
211
212 // init
213
214 - (id)initWithCString:(const char *)_value length:(unsigned)_len {
215   if ((self = [super initWithCString:_value length:_len])) {
216     capacity = _len;
217   }
218   return self;
219 }
220
221 // appending
222
223 - (void)appendString:(id)_str {
224   _checkCapacity(self, [_str cStringLength]);
225   strcat(value, [_str cString]);
226 }
227
228 - (void)appendCString:(const char *)_cstr {
229   int l = strlen(_cstr);
230   _checkCapacity(self, l);
231   strcat(value, _cstr);
232 }
233
234 - (void)appendCString:(const char *)_cstr length:(unsigned)_len {
235   _checkCapacity(self, _len);
236   memcpy(&(value[len]), _cstr, _len);
237   len += _len;
238   value[len] = '\0';
239 }
240
241 // removing
242
243 - (void)removeAllContents {
244   len = 0;
245   value[len] = '\0';
246 }
247
248 @end