Back to index

lightning-sunbird  0.9+nobinonly
nsStringEnumerator.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is String Enumerator.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corp.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alec Flett <alecf@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 #include "nsStringEnumerator.h"
00041 #include "prtypes.h"
00042 #include "nsCRT.h"
00043 #include "nsString.h"
00044 #include "nsReadableUtils.h"
00045 #include "nsISimpleEnumerator.h"
00046 #include "nsSupportsPrimitives.h"
00047 
00048 //
00049 // nsStringEnumerator
00050 //
00051 
00052 class nsStringEnumerator : public nsIStringEnumerator,
00053                            public nsIUTF8StringEnumerator,
00054                            public nsISimpleEnumerator
00055 {
00056 public:
00057     nsStringEnumerator(const nsStringArray* aArray, PRBool aOwnsArray) :
00058         mArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(PR_TRUE)
00059     {}
00060     
00061     nsStringEnumerator(const nsCStringArray* aArray, PRBool aOwnsArray) :
00062         mCArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(PR_FALSE)
00063     {}
00064 
00065     nsStringEnumerator(const nsStringArray* aArray, nsISupports* aOwner) :
00066         mArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(PR_FALSE), mIsUnicode(PR_TRUE)
00067     {}
00068     
00069     nsStringEnumerator(const nsCStringArray* aArray, nsISupports* aOwner) :
00070         mCArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(PR_FALSE), mIsUnicode(PR_FALSE)
00071     {}
00072 
00073     NS_DECL_ISUPPORTS
00074     NS_DECL_NSIUTF8STRINGENUMERATOR
00075 
00076     // have to declare nsIStringEnumerator manually, because of
00077     // overlapping method names
00078     NS_IMETHOD GetNext(nsAString& aResult);
00079     NS_DECL_NSISIMPLEENUMERATOR
00080 
00081 private:
00082     ~nsStringEnumerator() {
00083         if (mOwnsArray) {
00084             // const-casting is safe here, because the NS_New*
00085             // constructors make sure mOwnsArray is consistent with
00086             // the constness of the objects
00087             if (mIsUnicode)
00088                 delete NS_CONST_CAST(nsStringArray*,mArray);
00089             else
00090                 delete NS_CONST_CAST(nsCStringArray*,mCArray);
00091         }
00092     }
00093 
00094     union {
00095         const nsStringArray* mArray;
00096         const nsCStringArray* mCArray;
00097     };
00098 
00099     inline PRUint32 Count() {
00100         return mIsUnicode ? mArray->Count() : mCArray->Count();
00101     }
00102     
00103     PRUint32 mIndex;
00104 
00105     // the owner allows us to hold a strong reference to the object
00106     // that owns the array. Having a non-null value in mOwner implies
00107     // that mOwnsArray is PR_FALSE, because we rely on the real owner
00108     // to release the array
00109     nsCOMPtr<nsISupports> mOwner;
00110     PRPackedBool mOwnsArray;
00111     PRPackedBool mIsUnicode;
00112 };
00113 
00114 NS_IMPL_ISUPPORTS3(nsStringEnumerator,
00115                    nsIStringEnumerator,
00116                    nsIUTF8StringEnumerator,
00117                    nsISimpleEnumerator)
00118 
00119 NS_IMETHODIMP
00120 nsStringEnumerator::HasMore(PRBool* aResult)
00121 {
00122     NS_ENSURE_ARG_POINTER(aResult);
00123     *aResult = mIndex < Count();
00124     return NS_OK;
00125 }
00126 
00127 NS_IMETHODIMP
00128 nsStringEnumerator::HasMoreElements(PRBool* aResult)
00129 {
00130     return HasMore(aResult);
00131 }
00132 
00133 NS_IMETHODIMP
00134 nsStringEnumerator::GetNext(nsISupports** aResult)
00135 {
00136     if (mIsUnicode) {
00137         nsSupportsStringImpl* stringImpl = new nsSupportsStringImpl();
00138         if (!stringImpl) return NS_ERROR_OUT_OF_MEMORY;
00139         
00140         stringImpl->SetData(*mArray->StringAt(mIndex++));
00141         *aResult = stringImpl;
00142     }
00143     else {
00144         nsSupportsCStringImpl* cstringImpl = new nsSupportsCStringImpl();
00145         if (!cstringImpl) return NS_ERROR_OUT_OF_MEMORY;
00146 
00147         cstringImpl->SetData(*mCArray->CStringAt(mIndex++));
00148         *aResult = cstringImpl;
00149     }
00150     NS_ADDREF(*aResult);
00151     return NS_OK;
00152 }
00153 
00154 NS_IMETHODIMP
00155 nsStringEnumerator::GetNext(nsAString& aResult)
00156 {
00157     NS_ENSURE_TRUE(mIndex < Count(), NS_ERROR_UNEXPECTED);
00158 
00159     if (mIsUnicode)
00160         aResult = *mArray->StringAt(mIndex++);
00161     else
00162         CopyUTF8toUTF16(*mCArray->CStringAt(mIndex++), aResult);
00163     
00164     return NS_OK;
00165 }
00166 
00167 NS_IMETHODIMP
00168 nsStringEnumerator::GetNext(nsACString& aResult)
00169 {
00170     NS_ENSURE_TRUE(mIndex < Count(), NS_ERROR_UNEXPECTED);
00171     
00172     if (mIsUnicode)
00173         CopyUTF16toUTF8(*mArray->StringAt(mIndex++), aResult);
00174     else
00175         aResult = *mCArray->CStringAt(mIndex++);
00176     
00177     return NS_OK;
00178 }
00179 
00180 template<class T>
00181 static inline nsresult
00182 StringEnumeratorTail(T** aResult)
00183 {
00184     if (!*aResult)
00185         return NS_ERROR_OUT_OF_MEMORY;
00186     NS_ADDREF(*aResult);
00187     return NS_OK;
00188 }
00189 
00190 //
00191 // constructors
00192 //
00193 
00194 NS_COM nsresult
00195 NS_NewStringEnumerator(nsIStringEnumerator** aResult,
00196                        const nsStringArray* aArray, nsISupports* aOwner)
00197 {
00198     NS_ENSURE_ARG_POINTER(aResult);
00199     NS_ENSURE_ARG_POINTER(aArray);
00200     
00201     *aResult = new nsStringEnumerator(aArray, aOwner);
00202     return StringEnumeratorTail(aResult);
00203 }
00204 
00205 
00206 NS_COM nsresult
00207 NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
00208                            const nsCStringArray* aArray, nsISupports* aOwner)
00209 {
00210     NS_ENSURE_ARG_POINTER(aResult);
00211     NS_ENSURE_ARG_POINTER(aArray);
00212     
00213     *aResult = new nsStringEnumerator(aArray, aOwner);
00214     return StringEnumeratorTail(aResult);
00215 }
00216 
00217 NS_COM nsresult
00218 NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult,
00219                                nsStringArray* aArray)
00220 {
00221     NS_ENSURE_ARG_POINTER(aResult);
00222     NS_ENSURE_ARG_POINTER(aArray);
00223     
00224     *aResult = new nsStringEnumerator(aArray, PR_TRUE);
00225     return StringEnumeratorTail(aResult);
00226 }
00227 
00228 NS_COM nsresult
00229 NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
00230                                    nsCStringArray* aArray)
00231 {
00232     NS_ENSURE_ARG_POINTER(aResult);
00233     NS_ENSURE_ARG_POINTER(aArray);
00234     
00235     *aResult = new nsStringEnumerator(aArray, PR_TRUE);
00236     return StringEnumeratorTail(aResult);
00237 }
00238 
00239 // const ones internally just forward to the non-const equivalents
00240 NS_COM nsresult
00241 NS_NewStringEnumerator(nsIStringEnumerator** aResult,
00242                        const nsStringArray* aArray)
00243 {
00244     NS_ENSURE_ARG_POINTER(aResult);
00245     NS_ENSURE_ARG_POINTER(aArray);
00246     
00247     *aResult = new nsStringEnumerator(aArray, PR_FALSE);
00248     return StringEnumeratorTail(aResult);
00249 }
00250 
00251 NS_COM nsresult
00252 NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
00253                            const nsCStringArray* aArray)
00254 {
00255     NS_ENSURE_ARG_POINTER(aResult);
00256     NS_ENSURE_ARG_POINTER(aArray);
00257     
00258     *aResult = new nsStringEnumerator(aArray, PR_FALSE);
00259     return StringEnumeratorTail(aResult);
00260 }
00261