Back to index

lightning-sunbird  0.9+nobinonly
nsCyrillicDetector.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; 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 #ifndef nsCyrillicDetector_h__
00038 #define nsCyrillicDetector_h__
00039 
00040 #include "nsCyrillicClass.h"
00041 
00042 
00043 
00044 
00045 // {2002F781-3960-11d3-B3C3-00805F8A6670}
00046 #define NS_RU_PROBDETECTOR_CID \
00047 { 0x2002f781, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
00048 
00049 
00050 // {2002F782-3960-11d3-B3C3-00805F8A6670}
00051 #define NS_UK_PROBDETECTOR_CID \
00052 { 0x2002f782, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
00053 
00054 // {2002F783-3960-11d3-B3C3-00805F8A6670}
00055 #define NS_RU_STRING_PROBDETECTOR_CID \
00056 { 0x2002f783, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
00057 
00058 // {2002F784-3960-11d3-B3C3-00805F8A6670}
00059 #define NS_UK_STRING_PROBDETECTOR_CID \
00060 { 0x2002f784, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
00061 
00062 static const PRUint8 *gCyrillicCls[5] =
00063 {
00064    CP1251Map,
00065    KOI8Map,
00066    ISO88595Map,
00067    MacCyrillicMap,
00068    IBM866Map
00069 };
00070 
00071 static const char * gRussian[5] = {
00072   "windows-1251", 
00073   "KOI8-R", 
00074   "ISO-8859-5", 
00075   "x-mac-cyrillic",
00076   "IBM866"
00077 };
00078 
00079 static const char * gUkrainian[5] = {
00080   "windows-1251", 
00081   "KOI8-U", 
00082   "ISO-8859-5", 
00083   "x-mac-ukrainian",
00084   "IBM866"
00085 };
00086 
00087 #define NUM_CYR_CHARSET 5
00088 
00089 class nsCyrillicDetector 
00090 {
00091   public:
00092     nsCyrillicDetector(PRUint8 aItems, 
00093                       const PRUint8 ** aCyrillicClass, 
00094                       const char **aCharsets) {
00095       mItems = aItems;
00096       mCyrillicClass = aCyrillicClass;
00097       mCharsets = aCharsets;
00098       for(PRUintn i=0;i<mItems;i++)
00099         mProb[i] = mLastCls[i] =0;
00100       mDone = PR_FALSE;
00101     };
00102     virtual ~nsCyrillicDetector() {};
00103     virtual void HandleData(const char* aBuf, PRUint32 aLen);
00104     virtual void   DataEnd();
00105   protected:
00106     virtual void Report(const char* aCharset) = 0;
00107     PRBool  mDone;
00108 
00109   private:
00110     PRUint8  mItems;
00111     const PRUint8 ** mCyrillicClass;
00112     const char** mCharsets;
00113     PRUint32 mProb[NUM_CYR_CHARSET];
00114     PRUint8 mLastCls[NUM_CYR_CHARSET];
00115 };
00116 
00117 class nsCyrXPCOMDetector :  
00118       public nsCyrillicDetector,
00119       public nsICharsetDetector
00120 {
00121   public:
00122     // nsISupports interface
00123     NS_DECL_ISUPPORTS
00124     nsCyrXPCOMDetector(PRUint8 aItems, 
00125                       const PRUint8 ** aCyrillicClass, 
00126                       const char **aCharsets);
00127     virtual ~nsCyrXPCOMDetector();
00128     NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
00129     NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, PRBool *oDontFeedMe);
00130     NS_IMETHOD Done();
00131   protected:
00132     virtual void Report(const char* aCharset);
00133   private:
00134     nsCOMPtr<nsICharsetDetectionObserver> mObserver;
00135 };
00136 
00137 class nsCyrXPCOMStringDetector :  
00138       public nsCyrillicDetector,
00139       public nsIStringCharsetDetector
00140 {
00141   public:
00142     // nsISupports interface
00143     NS_DECL_ISUPPORTS
00144     nsCyrXPCOMStringDetector(PRUint8 aItems, 
00145                       const PRUint8 ** aCyrillicClass, 
00146                       const char **aCharsets);
00147     virtual ~nsCyrXPCOMStringDetector();
00148     NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, 
00149                      const char** oCharset, nsDetectionConfident &oConf);
00150   protected:
00151     virtual void Report(const char* aCharset);
00152   private:
00153     nsCOMPtr<nsICharsetDetectionObserver> mObserver;
00154     const char* mResult;
00155 };
00156 
00157 class nsRUProbDetector : public nsCyrXPCOMDetector
00158 {
00159   public:
00160     nsRUProbDetector() 
00161       : nsCyrXPCOMDetector(5, gCyrillicCls, gRussian) {};
00162 };
00163 
00164 class nsRUStringProbDetector : public nsCyrXPCOMStringDetector
00165 {
00166   public:
00167     nsRUStringProbDetector() 
00168       : nsCyrXPCOMStringDetector(5, gCyrillicCls, gRussian) {};
00169 };
00170 
00171 class nsUKProbDetector : public nsCyrXPCOMDetector
00172 {
00173   public:
00174     nsUKProbDetector() 
00175       : nsCyrXPCOMDetector(5, gCyrillicCls, gUkrainian) {};
00176 };
00177 
00178 class nsUKStringProbDetector : public nsCyrXPCOMStringDetector
00179 {
00180   public:
00181     nsUKStringProbDetector() 
00182       : nsCyrXPCOMStringDetector(5, gCyrillicCls, gUkrainian) {};
00183 };
00184 
00185 #endif