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
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.
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"
44 #include "nsReadableUtils.h"
45 #include "nsISimpleEnumerator.h"
46 #include "nsSupportsPrimitives.h"
52 class nsStringEnumerator
: public nsIStringEnumerator
,
53 public nsIUTF8StringEnumerator
,
54 public nsISimpleEnumerator
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
)
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
82 ~nsStringEnumerator() {
84 // const-casting is safe here, because the NS_New*
85 // constructors make sure mOwnsArray is consistent with
86 // the constness of the objects
88 delete const_cast<nsStringArray
*>(mArray
);
90 delete const_cast<nsCStringArray
*>(mCArray
);
95 const nsStringArray
* mArray
;
96 const nsCStringArray
* mCArray
;
99 inline PRUint32
Count() {
100 return mIsUnicode
? mArray
->Count() : mCArray
->Count();
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
,
116 nsIUTF8StringEnumerator
,
120 nsStringEnumerator::HasMore(PRBool
* aResult
)
122 NS_ENSURE_ARG_POINTER(aResult
);
123 *aResult
= mIndex
< Count();
128 nsStringEnumerator::HasMoreElements(PRBool
* aResult
)
130 return HasMore(aResult
);
134 nsStringEnumerator::GetNext(nsISupports
** aResult
)
137 nsSupportsStringImpl
* stringImpl
= new nsSupportsStringImpl();
138 if (!stringImpl
) return NS_ERROR_OUT_OF_MEMORY
;
140 stringImpl
->SetData(*mArray
->StringAt(mIndex
++));
141 *aResult
= stringImpl
;
144 nsSupportsCStringImpl
* cstringImpl
= new nsSupportsCStringImpl();
145 if (!cstringImpl
) return NS_ERROR_OUT_OF_MEMORY
;
147 cstringImpl
->SetData(*mCArray
->CStringAt(mIndex
++));
148 *aResult
= cstringImpl
;
155 nsStringEnumerator::GetNext(nsAString
& aResult
)
157 NS_ENSURE_TRUE(mIndex
< Count(), NS_ERROR_UNEXPECTED
);
160 aResult
= *mArray
->StringAt(mIndex
++);
162 CopyUTF8toUTF16(*mCArray
->CStringAt(mIndex
++), aResult
);
168 nsStringEnumerator::GetNext(nsACString
& aResult
)
170 NS_ENSURE_TRUE(mIndex
< Count(), NS_ERROR_UNEXPECTED
);
173 CopyUTF16toUTF8(*mArray
->StringAt(mIndex
++), aResult
);
175 aResult
= *mCArray
->CStringAt(mIndex
++);
181 static inline nsresult
182 StringEnumeratorTail(T
** aResult NS_INPARAM
)
185 return NS_ERROR_OUT_OF_MEMORY
;
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
);
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
);
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
);
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
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
);
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
);