]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSString.cpp
98d2aa95228a8160b1648ad365aa4ad73f612a6b
[apple/xnu.git] / libkern / c++ / OSString.cpp
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* IOString.m created by rsulack on Wed 17-Sep-1997 */
24 /* IOString.cpp converted to C++ on Tue 1998-9-22 */
25
26 #include <string.h>
27
28 #include <libkern/c++/OSString.h>
29 #include <libkern/c++/OSSerialize.h>
30 #include <libkern/c++/OSLib.h>
31 #include <libkern/c++/OSData.h>
32 #include <string.h>
33
34 #define super OSObject
35
36 OSDefineMetaClassAndStructors(OSString, OSObject)
37 OSMetaClassDefineReservedUnused(OSString, 0);
38 OSMetaClassDefineReservedUnused(OSString, 1);
39 OSMetaClassDefineReservedUnused(OSString, 2);
40 OSMetaClassDefineReservedUnused(OSString, 3);
41 OSMetaClassDefineReservedUnused(OSString, 4);
42 OSMetaClassDefineReservedUnused(OSString, 5);
43 OSMetaClassDefineReservedUnused(OSString, 6);
44 OSMetaClassDefineReservedUnused(OSString, 7);
45 OSMetaClassDefineReservedUnused(OSString, 8);
46 OSMetaClassDefineReservedUnused(OSString, 9);
47 OSMetaClassDefineReservedUnused(OSString, 10);
48 OSMetaClassDefineReservedUnused(OSString, 11);
49 OSMetaClassDefineReservedUnused(OSString, 12);
50 OSMetaClassDefineReservedUnused(OSString, 13);
51 OSMetaClassDefineReservedUnused(OSString, 14);
52 OSMetaClassDefineReservedUnused(OSString, 15);
53
54 #if OSALLOCDEBUG
55 extern "C" {
56 extern int debug_container_malloc_size;
57 };
58 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
59 #else
60 #define ACCUMSIZE(s)
61 #endif
62
63 bool OSString::initWithString(const OSString *aString)
64 {
65 return initWithCString(aString->string);
66 }
67
68 bool OSString::initWithCString(const char *cString)
69 {
70 if (!cString || !super::init())
71 return false;
72
73 length = strlen(cString) + 1;
74 string = (char *) kalloc(length);
75 if (!string)
76 return false;
77
78 bcopy(cString, string, length);
79
80 ACCUMSIZE(length);
81
82 return true;
83 }
84
85 bool OSString::initWithCStringNoCopy(const char *cString)
86 {
87 if (!cString || !super::init())
88 return false;
89
90 length = strlen(cString) + 1;
91 flags |= kOSStringNoCopy;
92 string = (char *) cString;
93
94 return true;
95 }
96
97 OSString *OSString::withString(const OSString *aString)
98 {
99 OSString *me = new OSString;
100
101 if (me && !me->initWithString(aString)) {
102 me->release();
103 return 0;
104 }
105
106 return me;
107 }
108
109 OSString *OSString::withCString(const char *cString)
110 {
111 OSString *me = new OSString;
112
113 if (me && !me->initWithCString(cString)) {
114 me->release();
115 return 0;
116 }
117
118 return me;
119 }
120
121 OSString *OSString::withCStringNoCopy(const char *cString)
122 {
123 OSString *me = new OSString;
124
125 if (me && !me->initWithCStringNoCopy(cString)) {
126 me->release();
127 return 0;
128 }
129
130 return me;
131 }
132
133 /* @@@ gvdl */
134 #if 0
135 OSString *OSString::stringWithFormat(const char *format, ...)
136 {
137 #ifndef KERNEL // mach3xxx
138 OSString *me;
139 va_list argList;
140
141 if (!format)
142 return 0;
143
144 va_start(argList, format);
145 me = stringWithCapacity(256);
146 me->length = vsnprintf(me->string, 256, format, argList);
147 me->length++; // we include the null in the length
148 if (me->Length > 256)
149 me->Length = 256;
150 va_end (argList);
151
152 return me;
153 #else
154 return 0;
155 #endif
156 }
157 #endif /* 0 */
158
159 void OSString::free()
160 {
161 if ( !(flags & kOSStringNoCopy) && string) {
162 kfree((vm_offset_t)string, (vm_size_t)length);
163 ACCUMSIZE(-length);
164 }
165
166 super::free();
167 }
168
169 unsigned int OSString::getLength() const { return length - 1; }
170
171 const char *OSString::getCStringNoCopy() const
172 {
173 return string;
174 }
175
176 bool OSString::setChar(char aChar, unsigned int index)
177 {
178 if ( !(flags & kOSStringNoCopy) && index < length - 1) {
179 string[index] = aChar;
180
181 return true;
182 }
183 else
184 return false;
185 }
186
187 char OSString::getChar(unsigned int index) const
188 {
189 if (index < length)
190 return string[index];
191 else
192 return '\0';
193 }
194
195
196 bool OSString::isEqualTo(const OSString *aString) const
197 {
198 if (length != aString->length)
199 return false;
200 else
201 return isEqualTo((const char *) aString->string);
202 }
203
204 bool OSString::isEqualTo(const char *aCString) const
205 {
206 return strcmp(string, aCString) == 0;
207 }
208
209 bool OSString::isEqualTo(const OSMetaClassBase *obj) const
210 {
211 OSString * str;
212 OSData * data;
213
214 if ((str = OSDynamicCast(OSString, obj)))
215 return isEqualTo(str);
216 else if ((data = OSDynamicCast (OSData, obj)))
217 return isEqualTo(data);
218 else
219 return false;
220 }
221
222 bool OSString::isEqualTo(const OSData *obj) const
223 {
224 if (NULL == obj)
225 return false;
226
227 unsigned int dataLen = obj->getLength ();;
228 char * dataPtr = (char *) obj->getBytesNoCopy ();
229
230 if (dataLen != length) {
231
232 // check for the fact that OSData may be a buffer that
233 // that includes a termination byte and will thus have
234 // a length of the actual string length PLUS 1. In this
235 // case we verify that the additional byte is a terminator
236 // and if so count the two lengths as being the same.
237
238 if ( (dataLen - length) == 1 ) {
239 if (dataPtr[dataLen-1] != 0)
240 return false;
241 dataLen--;
242 }
243 else
244 return false;
245 }
246
247 for ( unsigned int i=0; i < dataLen; i++ ) {
248 if ( *dataPtr++ != string[i] )
249 return false;
250 }
251
252 return true;
253 }
254
255 bool OSString::serialize(OSSerialize *s) const
256 {
257 char *c = string;
258
259 if (s->previouslySerialized(this)) return true;
260
261 if (!s->addXMLStartTag(this, "string")) return false;
262 while (*c) {
263 if (*c == '<') {
264 if (!s->addString("&lt;")) return false;
265 } else if (*c == '>') {
266 if (!s->addString("&gt;")) return false;
267 } else if (*c == '&') {
268 if (!s->addString("&amp;")) return false;
269 } else {
270 if (!s->addChar(*c)) return false;
271 }
272 c++;
273 }
274
275 return s->addXMLEndTag("string");
276 }