Bug 455527 - In some cases Embedding startup crashes if gAppData == null. r=benjamin.
[mozilla-central.git] / xpcom / ds / nsStringEnumerator.cpp
blob3b6d7fd01c9606c56c9dc09e99c6fe28c0f01a50
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is String Enumerator.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corp.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Alec Flett <alecf@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #include "nsStringEnumerator.h"
41 #include "prtypes.h"
42 #include "nsCRT.h"
43 #include "nsString.h"
44 #include "nsReadableUtils.h"
45 #include "nsISimpleEnumerator.h"
46 #include "nsSupportsPrimitives.h"
49 // nsStringEnumerator
52 class nsStringEnumerator : public nsIStringEnumerator,
53 public nsIUTF8StringEnumerator,
54 public nsISimpleEnumerator
56 public:
57 nsStringEnumerator(const nsStringArray* aArray, PRBool aOwnsArray) :
58 mArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(PR_TRUE)
61 nsStringEnumerator(const nsCStringArray* aArray, PRBool aOwnsArray) :
62 mCArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(PR_FALSE)
65 nsStringEnumerator(const nsStringArray* aArray, nsISupports* aOwner) :
66 mArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(PR_FALSE), mIsUnicode(PR_TRUE)
69 nsStringEnumerator(const nsCStringArray* aArray, nsISupports* aOwner) :
70 mCArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(PR_FALSE), mIsUnicode(PR_FALSE)
73 NS_DECL_ISUPPORTS
74 NS_DECL_NSIUTF8STRINGENUMERATOR
76 // have to declare nsIStringEnumerator manually, because of
77 // overlapping method names
78 NS_IMETHOD GetNext(nsAString& aResult);
79 NS_DECL_NSISIMPLEENUMERATOR
81 private:
82 ~nsStringEnumerator() {
83 if (mOwnsArray) {
84 // const-casting is safe here, because the NS_New*
85 // constructors make sure mOwnsArray is consistent with
86 // the constness of the objects
87 if (mIsUnicode)
88 delete const_cast<nsStringArray*>(mArray);
89 else
90 delete const_cast<nsCStringArray*>(mCArray);
94 union {
95 const nsStringArray* mArray;
96 const nsCStringArray* mCArray;
99 inline PRUint32 Count() {
100 return mIsUnicode ? mArray->Count() : mCArray->Count();
103 PRUint32 mIndex;
105 // the owner allows us to hold a strong reference to the object
106 // that owns the array. Having a non-null value in mOwner implies
107 // that mOwnsArray is PR_FALSE, because we rely on the real owner
108 // to release the array
109 nsCOMPtr<nsISupports> mOwner;
110 PRPackedBool mOwnsArray;
111 PRPackedBool mIsUnicode;
114 NS_IMPL_ISUPPORTS3(nsStringEnumerator,
115 nsIStringEnumerator,
116 nsIUTF8StringEnumerator,
117 nsISimpleEnumerator)
119 NS_IMETHODIMP
120 nsStringEnumerator::HasMore(PRBool* aResult)
122 NS_ENSURE_ARG_POINTER(aResult);
123 *aResult = mIndex < Count();
124 return NS_OK;
127 NS_IMETHODIMP
128 nsStringEnumerator::HasMoreElements(PRBool* aResult)
130 return HasMore(aResult);
133 NS_IMETHODIMP
134 nsStringEnumerator::GetNext(nsISupports** aResult)
136 if (mIsUnicode) {
137 nsSupportsStringImpl* stringImpl = new nsSupportsStringImpl();
138 if (!stringImpl) return NS_ERROR_OUT_OF_MEMORY;
140 stringImpl->SetData(*mArray->StringAt(mIndex++));
141 *aResult = stringImpl;
143 else {
144 nsSupportsCStringImpl* cstringImpl = new nsSupportsCStringImpl();
145 if (!cstringImpl) return NS_ERROR_OUT_OF_MEMORY;
147 cstringImpl->SetData(*mCArray->CStringAt(mIndex++));
148 *aResult = cstringImpl;
150 NS_ADDREF(*aResult);
151 return NS_OK;
154 NS_IMETHODIMP
155 nsStringEnumerator::GetNext(nsAString& aResult)
157 NS_ENSURE_TRUE(mIndex < Count(), NS_ERROR_UNEXPECTED);
159 if (mIsUnicode)
160 aResult = *mArray->StringAt(mIndex++);
161 else
162 CopyUTF8toUTF16(*mCArray->CStringAt(mIndex++), aResult);
164 return NS_OK;
167 NS_IMETHODIMP
168 nsStringEnumerator::GetNext(nsACString& aResult)
170 NS_ENSURE_TRUE(mIndex < Count(), NS_ERROR_UNEXPECTED);
172 if (mIsUnicode)
173 CopyUTF16toUTF8(*mArray->StringAt(mIndex++), aResult);
174 else
175 aResult = *mCArray->CStringAt(mIndex++);
177 return NS_OK;
180 template<class T>
181 static inline nsresult
182 StringEnumeratorTail(T** aResult NS_INPARAM)
184 if (!*aResult)
185 return NS_ERROR_OUT_OF_MEMORY;
186 NS_ADDREF(*aResult);
187 return NS_OK;
191 // constructors
194 NS_COM nsresult
195 NS_NewStringEnumerator(nsIStringEnumerator** aResult,
196 const nsStringArray* aArray, nsISupports* aOwner)
198 NS_ENSURE_ARG_POINTER(aResult);
199 NS_ENSURE_ARG_POINTER(aArray);
201 *aResult = new nsStringEnumerator(aArray, aOwner);
202 return StringEnumeratorTail(aResult);
206 NS_COM nsresult
207 NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
208 const nsCStringArray* aArray, nsISupports* aOwner)
210 NS_ENSURE_ARG_POINTER(aResult);
211 NS_ENSURE_ARG_POINTER(aArray);
213 *aResult = new nsStringEnumerator(aArray, aOwner);
214 return StringEnumeratorTail(aResult);
217 NS_COM nsresult
218 NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult,
219 nsStringArray* aArray)
221 NS_ENSURE_ARG_POINTER(aResult);
222 NS_ENSURE_ARG_POINTER(aArray);
224 *aResult = new nsStringEnumerator(aArray, PR_TRUE);
225 return StringEnumeratorTail(aResult);
228 NS_COM nsresult
229 NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
230 nsCStringArray* aArray)
232 NS_ENSURE_ARG_POINTER(aResult);
233 NS_ENSURE_ARG_POINTER(aArray);
235 *aResult = new nsStringEnumerator(aArray, PR_TRUE);
236 return StringEnumeratorTail(aResult);
239 // const ones internally just forward to the non-const equivalents
240 NS_COM nsresult
241 NS_NewStringEnumerator(nsIStringEnumerator** aResult,
242 const nsStringArray* aArray)
244 NS_ENSURE_ARG_POINTER(aResult);
245 NS_ENSURE_ARG_POINTER(aArray);
247 *aResult = new nsStringEnumerator(aArray, PR_FALSE);
248 return StringEnumeratorTail(aResult);
251 NS_COM nsresult
252 NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
253 const nsCStringArray* aArray)
255 NS_ENSURE_ARG_POINTER(aResult);
256 NS_ENSURE_ARG_POINTER(aArray);
258 *aResult = new nsStringEnumerator(aArray, PR_FALSE);
259 return StringEnumeratorTail(aResult);