Back to index

lightning-sunbird  0.9+nobinonly
nsCharsOrderIdFormater.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "pratom.h"
00039 #include "nsUUDll.h"
00040 #include "nsCharsOrderIdFormater.h"
00041 
00042 
00043 nsCharsOrderIdFormater::nsCharsOrderIdFormater( nsCharsList* aList)
00044 {
00045   mList = aList;
00046   mBase = aList->Length();
00047 }
00048 
00049 nsCharsOrderIdFormater::~nsCharsOrderIdFormater()
00050 {
00051   delete mList;
00052 }
00053 
00054 NS_IMETHOD ToString( PRUint32 aOrder, nsString& aResult) 
00055 {
00056   aResult = "";
00057 
00058   PRUint32 current;
00059   PRUint32 remain = aOrder;
00060 
00061   do {
00062     current = aOrder % mBase;
00063     remain = aOrder / mBase;
00064     aResult.Insert(mList->Get(current),0); 
00065    
00066   } while( remain != 0);
00067   return NS_OK;
00068 }
00069 
00070 NS_IMPL_ISUPPORTS1(nsCharsOrderIdFormater, nsIOrderIdFormater)
00071 
00072 class nsCharsOrderIdFormaterFactory : public nsIFactory {
00073   NS_DECL_ISUPPORTS
00074 public:
00075   nsCharsOrderIdFormaterFactory(const nsCID &aCID) {
00076     mCID = aCID;
00077   };
00078   virtual ~nsCharsOrderIdFormaterFactory() {
00079   };
00080   NS_IMETHOD CreateInstance(nsISupports *aDelegate,
00081                             const nsIID &aIID,
00082                             void **aResult);
00083   NS_IMETHOD LockFactory(PRBool aLock) {
00084     return NS_OK;
00085   };
00086 
00087 private:
00088   nsCID mCID;
00089 }
00090 
00091 NS_IMPL_ISUPPORTS1(nsCharsOrderIdFormaterFactory, nsIFactory)
00092 
00093 NS_IMETHODIMP nsCharsOrderIdFormaterFactory::CreateInstance(
00094    nsISupports *aDelegate,
00095    const nsIID &aIID,
00096    void **aResult)
00097 {
00098    if(NULL == aResult)
00099      return NS_ERROR_NULL_POINTER;
00100    *aResult = NULL;
00101 
00102    nsISupports *inst = nsnull;
00103    if(mCID.Equals(kLowerAToZOrderIdCID)) {
00104       inst = new nsCharOrderIdFormater(new nsRangeCharsList('a', 'z'));
00105    } else if(mCID.Equals(kUpperAToZOrderIdCID)) {
00106       inst = new nsCharOrderIdFormater(new nsRangeCharsList('A', 'Z'));
00107    } else if(mCID.Equals(k0To9OrderIdCID)) {
00108       inst = new nsCharOrderIdFormater(new nsRangeCharsList('0', '9'));
00109    } else if(mCID.Equals(kHeavenlyStemOrderIdCID)) {
00110       static PRUnichar gHeavenlyStemList[] = {
00111           0x7532, 0x4e59, 0x4e19, 0x4e01, 0x620a,
00112           0x5df1, 0x5e9a, 0x8f9b, 0x58ce, 0x7678        
00113       };
00114       nsAutoString tmp(gHeavenlyStemList, 
00115                        sizeof(gHeavenlyStemList)/sizeof(PRUnichar));
00116       inst = new nsCharOrderIdFormater(new nsStringCharsList(tmp));
00117    } else if(mCID.Equals(kEarthlyBranchOrderIdCID)) {
00118       static PRUnichar gEarthlyBranchList[] = {
00119           0x5b50, 0x4e11, 0x5bc5, 0x536f, 0x8fb0, 0x5df3,
00120           0x5348, 0x672a, 0x7533, 0x9149, 0x620c, 0x4ea5
00121       };
00122       nsAutoString tmp(gEarthlyBranchList, 
00123                        sizeof(gEarthlyBranchList)/sizeof(PRUnichar));
00124       inst = new nsCharOrderIdFormater(new nsStringCharsList(tmp));
00125    } else if(mCID.Equals(kBoPoMoFoOrderIdCID)) {
00126       //
00127       // Note:  Although Unicode standard encode 0x312A-0x312C as BoPoMoFo,
00128       //        these three are origional designed for Mid China dialet
00129       //        and have never been used for Maderine. Therefore, we
00130       //        do not use them for order id.
00131       inst = new nsCharOrderIdFormater(new nsRangeCharsList(0x3105, 0x3129));
00132    } else if(mCID.Equals(kKatakanaOrderIdCID)) {
00133       // XXX fake list- need to verify with momoi about the real seq.
00134       static PRUnichar gKatakanaList[] = {
00135          0x3042, 0x3044, 0x3046, 0x3048, 0x304a
00136       };
00137       nsAutoString tmp(gKatakanaClassList, sizeof(gKatakanaList)/sizeof(PRUnichar));
00138       inst = new nsCharOrderIdFormater(new nsStringCharsList(tmp));
00139    } else if(mCID.Equals(kHiraganaOrderIdCID)) {
00140       // XXX fake list- need to verify with momoi about the real seq.
00141       static PRUnichar gHiraganaList[] = {
00142          0x30a2, 0x30a4, 0x30a6, 0x30a8, 0x30aa
00143       };
00144       nsAutoString tmp(gHiraganaList, sizeof(gHiraganaList)/sizeof(PRUnichar));
00145       inst = new nsCharOrderIdFormater(new nsStringCharsList(tmp));
00146    } else {
00147       return NS_ERROR_ILLEGAL_VALUE;
00148    }
00149 
00150 
00151    if(NULL == inst )
00152      return NS_ERROR_OUT_OF_MEMORY;
00153    nsresult res = inst->QueryInterface(aIID, aResult);
00154    if(NS_FAILED(res)) {
00155      delete inst;
00156    }
00157    return res;
00158 }
00159