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 "KURL.h" 30#include <windows.h> 31#include <wtf/text/AtomicString.h> 32#include <wtf/text/WTFString.h> 33 34#if USE(CF) 35#include <CoreFoundation/CoreFoundation.h> 36#endif 37 38using namespace JSC; 39 40namespace WebCore { 41 42BString::BString() 43 : m_bstr(0) 44{ 45} 46 47BString::BString(const wchar_t* characters) 48{ 49 if (!characters) 50 m_bstr = 0; 51 else 52 m_bstr = SysAllocString(characters); 53} 54 55BString::BString(const wchar_t* characters, size_t length) 56{ 57 if (!characters) 58 m_bstr = 0; 59 else 60 m_bstr = SysAllocStringLen(characters, length); 61} 62 63BString::BString(const String& s) 64{ 65 if (s.isNull()) 66 m_bstr = 0; 67 else 68 m_bstr = SysAllocStringLen(s.characters(), s.length()); 69} 70 71BString::BString(const KURL& url) 72{ 73 if (url.isNull()) 74 m_bstr = 0; 75 else 76 m_bstr = SysAllocStringLen(url.string().characters(), url.string().length()); 77} 78 79BString::BString(const AtomicString& s) 80{ 81 if (s.isNull()) 82 m_bstr = 0; 83 else 84 m_bstr = SysAllocStringLen(s.characters(), s.length()); 85} 86 87#if USE(CF) 88BString::BString(CFStringRef cfstr) 89 : m_bstr(0) 90{ 91 if (!cfstr) 92 return; 93 94 const UniChar* uniChars = CFStringGetCharactersPtr(cfstr); 95 if (uniChars) { 96 m_bstr = SysAllocStringLen((LPCWSTR)uniChars, CFStringGetLength(cfstr)); 97 return; 98 } 99 100 CFIndex length = CFStringGetLength(cfstr); 101 m_bstr = SysAllocStringLen(0, length); 102 CFStringGetCharacters(cfstr, CFRangeMake(0, length), (UniChar*)m_bstr); 103 m_bstr[length] = 0; 104} 105#endif 106 107BString::~BString() 108{ 109 SysFreeString(m_bstr); 110} 111 112BString::BString(const BString& other) 113{ 114 if (!other.m_bstr) 115 m_bstr = 0; 116 else 117 m_bstr = SysAllocString(other.m_bstr); 118} 119 120void BString::adoptBSTR(BSTR bstr) 121{ 122 SysFreeString(m_bstr); 123 m_bstr = bstr; 124} 125 126void BString::clear() 127{ 128 SysFreeString(m_bstr); 129 m_bstr = 0; 130} 131 132BString& BString::operator=(const BString& other) 133{ 134 if (this != &other) 135 *this = other.m_bstr; 136 return *this; 137} 138 139BString& BString::operator=(const BSTR& other) 140{ 141 if (other != m_bstr) { 142 SysFreeString(m_bstr); 143 m_bstr = other ? SysAllocString(other) : 0; 144 } 145 146 return *this; 147} 148 149bool operator ==(const BString& a, const BString& b) 150{ 151 if (SysStringLen((BSTR)a) != SysStringLen((BSTR)b)) 152 return false; 153 if (!(BSTR)a && !(BSTR)b) 154 return true; 155 if (!(BSTR)a || !(BSTR)b) 156 return false; 157 return !wcscmp((BSTR)a, (BSTR)b); 158} 159 160bool operator !=(const BString& a, const BString& b) 161{ 162 return !(a==b); 163} 164 165bool operator ==(const BString& a, BSTR b) 166{ 167 if (SysStringLen((BSTR)a) != SysStringLen(b)) 168 return false; 169 if (!(BSTR)a && !b) 170 return true; 171 if (!(BSTR)a || !b) 172 return false; 173 return !wcscmp((BSTR)a, b); 174} 175 176bool operator !=(const BString& a, BSTR b) 177{ 178 return !(a==b); 179} 180 181bool operator ==(BSTR a, const BString& b) 182{ 183 if (SysStringLen(a) != SysStringLen((BSTR)b)) 184 return false; 185 if (!a && !(BSTR)b) 186 return true; 187 if (!a || !(BSTR)b) 188 return false; 189 return !wcscmp(a, (BSTR)b); 190} 191 192bool operator !=(BSTR a, const BString& b) 193{ 194 return !(a==b); 195} 196 197} 198