1 /* 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "BString.h" 28 29 #include "AtomicString.h" 30 #include "KURL.h" 31 #include "PlatformString.h" 32 #include <tchar.h> 33 #include <windows.h> 34 35 #if PLATFORM(CF) 36 #include <CoreFoundation/CoreFoundation.h> 37 #endif 38 39 using namespace JSC; 40 41 namespace WebCore { 42 43 BString::BString() 44 : m_bstr(0) 45 { 46 } 47 48 BString::BString(const wchar_t* characters) 49 { 50 if (!characters) 51 m_bstr = 0; 52 else 53 m_bstr = SysAllocString(characters); 54 } 55 56 BString::BString(const wchar_t* characters, size_t length) 57 { 58 if (!characters) 59 m_bstr = 0; 60 else 61 m_bstr = SysAllocStringLen(characters, length); 62 } 63 64 BString::BString(const String& s) 65 { 66 if (s.isNull()) 67 m_bstr = 0; 68 else 69 m_bstr = SysAllocStringLen(s.characters(), s.length()); 70 } 71 72 BString::BString(const KURL& url) 73 { 74 if (url.isNull()) 75 m_bstr = 0; 76 else 77 m_bstr = SysAllocStringLen(url.string().characters(), url.string().length()); 78 } 79 80 BString::BString(const AtomicString& s) 81 { 82 if (s.isNull()) 83 m_bstr = 0; 84 else 85 m_bstr = SysAllocStringLen(s.characters(), s.length()); 86 } 87 88 BString::BString(const UString& s) 89 { 90 if (s.isNull()) 91 m_bstr = 0; 92 else 93 m_bstr = SysAllocStringLen(s.data(), s.size()); 94 } 95 96 #if PLATFORM(CF) 97 BString::BString(CFStringRef cfstr) 98 : m_bstr(0) 99 { 100 if (!cfstr) 101 return; 102 103 const UniChar* uniChars = CFStringGetCharactersPtr(cfstr); 104 if (uniChars) { 105 m_bstr = SysAllocStringLen((LPCTSTR)uniChars, CFStringGetLength(cfstr)); 106 return; 107 } 108 109 CFIndex length = CFStringGetLength(cfstr); 110 m_bstr = SysAllocStringLen(0, length); 111 CFStringGetCharacters(cfstr, CFRangeMake(0, length), (UniChar*)m_bstr); 112 m_bstr[length] = 0; 113 } 114 #endif 115 116 BString::~BString() 117 { 118 SysFreeString(m_bstr); 119 } 120 121 BString::BString(const BString& other) 122 { 123 if (!other.m_bstr) 124 m_bstr = 0; 125 else 126 m_bstr = SysAllocString(other.m_bstr); 127 } 128 129 void BString::adoptBSTR(BSTR bstr) 130 { 131 if (m_bstr) 132 SysFreeString(m_bstr); 133 m_bstr = bstr; 134 } 135 136 BString& BString::operator=(const BString& other) 137 { 138 if (this != &other) 139 *this = other.m_bstr; 140 return *this; 141 } 142 143 BString& BString::operator=(const BSTR& other) 144 { 145 if (other != m_bstr) { 146 SysFreeString(m_bstr); 147 m_bstr = other ? SysAllocString(other) : 0; 148 } 149 150 return *this; 151 } 152 153 bool operator ==(const BString& a, const BString& b) 154 { 155 if (SysStringLen((BSTR)a) != SysStringLen((BSTR)b)) 156 return false; 157 if (!(BSTR)a && !(BSTR)b) 158 return true; 159 if (!(BSTR)a || !(BSTR)b) 160 return false; 161 return !_tcscmp((BSTR)a, (BSTR)b); 162 } 163 164 bool operator !=(const BString& a, const BString& b) 165 { 166 return !(a==b); 167 } 168 169 bool operator ==(const BString& a, BSTR b) 170 { 171 if (SysStringLen((BSTR)a) != SysStringLen(b)) 172 return false; 173 if (!(BSTR)a && !b) 174 return true; 175 if (!(BSTR)a || !b) 176 return false; 177 return !_tcscmp((BSTR)a, b); 178 } 179 180 bool operator !=(const BString& a, BSTR b) 181 { 182 return !(a==b); 183 } 184 185 bool operator ==(BSTR a, const BString& b) 186 { 187 if (SysStringLen(a) != SysStringLen((BSTR)b)) 188 return false; 189 if (!a && !(BSTR)b) 190 return true; 191 if (!a || !(BSTR)b) 192 return false; 193 return !_tcscmp(a, (BSTR)b); 194 } 195 196 bool operator !=(BSTR a, const BString& b) 197 { 198 return !(a==b); 199 } 200 201 } 202