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