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