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