sbVariantUtilsLib.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 //------------------------------------------------------------------------------
26 //------------------------------------------------------------------------------
27 //
28 // Songbird variant utilities.
29 //
30 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 
38 //------------------------------------------------------------------------------
39 //
40 // Songbird variant utilities imported services.
41 //
42 //------------------------------------------------------------------------------
43 
44 // Self imports.
45 #include "sbVariantUtilsLib.h"
46 
47 // Mozilla imports.
48 #include <nsCOMPtr.h>
49 #include <nsStringGlue.h>
50 
51 // C++ STL imports.
52 #include <iomanip>
53 #include <sstream>
54 
55 
56 //------------------------------------------------------------------------------
57 //
58 // Internal Songbird variant utilities.
59 //
60 //------------------------------------------------------------------------------
61 
70 static nsresult
71 sbNonStringVariantToInt(nsIVariant* aVariant,
72  PRInt64* aInt)
73 {
74  NS_ENSURE_ARG_POINTER(aVariant);
75  NS_ENSURE_ARG_POINTER(aInt);
76  nsresult rv = aVariant->GetAsInt64(aInt);
77  NS_ENSURE_SUCCESS(rv, rv);
78  return NS_OK;
79 }
80 
81 static nsresult
82 sbNonStringVariantToInt(nsIVariant* aVariant,
83  PRUint64* aInt)
84 {
85  NS_ENSURE_ARG_POINTER(aVariant);
86  NS_ENSURE_ARG_POINTER(aInt);
87  nsresult rv = aVariant->GetAsUint64(aInt);
88  NS_ENSURE_SUCCESS(rv, rv);
89  return NS_OK;
90 }
91 
92 
102 template <class T>
103 static nsresult
104 sbVariantToInt(nsIVariant* aVariant,
105  T* aInt)
106 {
107  // Validate arguments.
108  NS_ENSURE_ARG_POINTER(aVariant);
109  NS_ENSURE_ARG_POINTER(aInt);
110 
111  // Function variables.
112  nsresult rv;
113 
114  // Convert the variant.
115  PRUint16 dataType;
116  rv = aVariant->GetDataType(&dataType);
117  NS_ENSURE_SUCCESS(rv, rv);
118  switch (dataType) {
119  case nsIDataType::VTYPE_DOMSTRING :
120  case nsIDataType::VTYPE_CHAR_STR :
121  case nsIDataType::VTYPE_WCHAR_STR :
122  case nsIDataType::VTYPE_UTF8STRING :
123  case nsIDataType::VTYPE_CSTRING :
124  case nsIDataType::VTYPE_ASTRING :
125  case nsIDataType::VTYPE_STRING_SIZE_IS :
126  case nsIDataType::VTYPE_WSTRING_SIZE_IS :
127  {
128  // Convert from string with support for hexadecimal strings.
129  nsCAutoString variantString;
130  rv = aVariant->GetAsACString(variantString);
131  NS_ENSURE_SUCCESS(rv, rv);
132  std::string s(variantString.get());
133  std::istringstream iss(s);
134  iss >> std::setbase(0) >> *aInt;
135  break;
136  }
137 
138  default :
139  rv = sbNonStringVariantToInt(aVariant, aInt);
140  NS_ENSURE_SUCCESS(rv, rv);
141  break;
142  }
143 
144  return NS_OK;
145 }
146 
147 
148 //------------------------------------------------------------------------------
149 //
150 // Songbird variant utilities.
151 //
152 //------------------------------------------------------------------------------
153 
154 //-------------------------------------
155 //
156 // sbVariantsEqual
157 //
158 
159 nsresult
160 sbVariantsEqual(nsIVariant* aVariant1,
161  nsIVariant* aVariant2,
162  PRBool* aEqual)
163 {
164  // Validate arguments.
165  NS_ENSURE_ARG_POINTER(aEqual);
166 
167  // Function variables.
168  nsresult rv;
169 
170  // If either variant is null, they're only equal if they're both null.
171  if (!aVariant1 || !aVariant2) {
172  *aEqual = (!aVariant1 && !aVariant2);
173  return NS_OK;
174  }
175 
176  // Get the variant data types.
177  PRUint16 dataType1;
178  PRUint16 dataType2;
179  rv = aVariant1->GetDataType(&dataType1);
180  NS_ENSURE_SUCCESS(rv, rv);
181  rv = aVariant2->GetDataType(&dataType2);
182  NS_ENSURE_SUCCESS(rv, rv);
183 
184  // Compare the variants.
185  switch (dataType1) {
186  case nsIDataType::VTYPE_INT8 :
187  case nsIDataType::VTYPE_INT16 :
188  case nsIDataType::VTYPE_INT32 :
189  case nsIDataType::VTYPE_INT64 :
190  case nsIDataType::VTYPE_UINT8 :
191  case nsIDataType::VTYPE_UINT16 :
192  case nsIDataType::VTYPE_UINT32 :
193  {
194  PRInt64 value1;
195  PRInt64 value2;
196  rv = aVariant1->GetAsInt64(&value1);
197  NS_ENSURE_SUCCESS(rv, rv);
198  rv = sbVariantToInt(aVariant2, &value2);
199  NS_ENSURE_SUCCESS(rv, rv);
200  *aEqual = (value1 == value2);
201  break;
202  }
203 
204  case nsIDataType::VTYPE_UINT64 :
205  {
206  PRUint64 value1;
207  PRUint64 value2;
208  rv = aVariant1->GetAsUint64(&value1);
209  NS_ENSURE_SUCCESS(rv, rv);
210  rv = sbVariantToInt(aVariant2, &value2);
211  NS_ENSURE_SUCCESS(rv, rv);
212  *aEqual = (value1 == value2);
213  break;
214  }
215 
216  case nsIDataType::VTYPE_FLOAT :
217  case nsIDataType::VTYPE_DOUBLE :
218  {
219  double value1;
220  double value2;
221  rv = aVariant1->GetAsDouble(&value1);
222  NS_ENSURE_SUCCESS(rv, rv);
223  rv = aVariant2->GetAsDouble(&value2);
224  NS_ENSURE_SUCCESS(rv, rv);
225  *aEqual = (value1 == value2);
226  break;
227  }
228 
229  case nsIDataType::VTYPE_BOOL :
230  {
231  PRBool value1;
232  PRBool value2;
233  rv = aVariant1->GetAsBool(&value1);
234  NS_ENSURE_SUCCESS(rv, rv);
235  rv = aVariant2->GetAsBool(&value2);
236  NS_ENSURE_SUCCESS(rv, rv);
237  *aEqual = (value1 == value2);
238  break;
239  }
240 
241  case nsIDataType::VTYPE_CHAR :
242  {
243  char value1;
244  char value2;
245  rv = aVariant1->GetAsChar(&value1);
246  NS_ENSURE_SUCCESS(rv, rv);
247  rv = aVariant2->GetAsChar(&value2);
248  NS_ENSURE_SUCCESS(rv, rv);
249  *aEqual = (value1 == value2);
250  break;
251  }
252 
253  case nsIDataType::VTYPE_WCHAR :
254  {
255  PRUnichar value1;
256  PRUnichar value2;
257  rv = aVariant1->GetAsWChar(&value1);
258  NS_ENSURE_SUCCESS(rv, rv);
259  rv = aVariant2->GetAsWChar(&value2);
260  NS_ENSURE_SUCCESS(rv, rv);
261  *aEqual = (value1 == value2);
262  break;
263  }
264 
265  case nsIDataType::VTYPE_VOID :
266  case nsIDataType::VTYPE_EMPTY :
267  {
268  *aEqual = (dataType1 == dataType2);
269  break;
270  }
271 
272  case nsIDataType::VTYPE_ID :
273  {
274  nsID value1;
275  nsID value2;
276  rv = aVariant1->GetAsID(&value1);
277  NS_ENSURE_SUCCESS(rv, rv);
278  rv = aVariant2->GetAsID(&value2);
279  NS_ENSURE_SUCCESS(rv, rv);
280  *aEqual = value1.Equals(value2);
281  break;
282  }
283 
284  case nsIDataType::VTYPE_DOMSTRING :
285  case nsIDataType::VTYPE_CHAR_STR :
286  case nsIDataType::VTYPE_WCHAR_STR :
287  case nsIDataType::VTYPE_UTF8STRING :
288  case nsIDataType::VTYPE_CSTRING :
289  case nsIDataType::VTYPE_ASTRING :
290  case nsIDataType::VTYPE_STRING_SIZE_IS :
291  case nsIDataType::VTYPE_WSTRING_SIZE_IS :
292  {
293  nsAutoString value1;
294  nsAutoString value2;
295  rv = aVariant1->GetAsAString(value1);
296  NS_ENSURE_SUCCESS(rv, rv);
297  rv = aVariant2->GetAsAString(value2);
298  NS_ENSURE_SUCCESS(rv, rv);
299  *aEqual = value1.Equals(value2);
300  break;
301  }
302 
303  case nsIDataType::VTYPE_INTERFACE :
304  case nsIDataType::VTYPE_INTERFACE_IS :
305  {
306  nsCOMPtr<nsISupports> value1;
307  nsCOMPtr<nsISupports> value2;
308  rv = aVariant1->GetAsISupports(getter_AddRefs(value1));
309  NS_ENSURE_SUCCESS(rv, rv);
310  rv = aVariant2->GetAsISupports(getter_AddRefs(value2));
311  NS_ENSURE_SUCCESS(rv, rv);
312  *aEqual = (value1 == value2);
313  break;
314  }
315 
316  case nsIDataType::VTYPE_ARRAY :
317  case nsIDataType::VTYPE_EMPTY_ARRAY :
318  default :
319  return NS_ERROR_NOT_IMPLEMENTED;
320  }
321 
322  return NS_OK;
323 }
324 
nsresult sbVariantsEqual(nsIVariant *aVariant1, nsIVariant *aVariant2, PRBool *aEqual)
return NS_OK
Songbird Variant Utility Definitions.
static nsresult sbVariantToInt(nsIVariant *aVariant, T *aInt)
static nsresult sbNonStringVariantToInt(nsIVariant *aVariant, PRInt64 *aInt)