1 : /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 : #ifndef nsCyrillicDetector_h__
38 : #define nsCyrillicDetector_h__
39 :
40 : #include "nsCyrillicClass.h"
41 :
42 :
43 :
44 :
45 : // {2002F781-3960-11d3-B3C3-00805F8A6670}
46 : #define NS_RU_PROBDETECTOR_CID \
47 : { 0x2002f781, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
48 :
49 :
50 : // {2002F782-3960-11d3-B3C3-00805F8A6670}
51 : #define NS_UK_PROBDETECTOR_CID \
52 : { 0x2002f782, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
53 :
54 : // {2002F783-3960-11d3-B3C3-00805F8A6670}
55 : #define NS_RU_STRING_PROBDETECTOR_CID \
56 : { 0x2002f783, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
57 :
58 : // {2002F784-3960-11d3-B3C3-00805F8A6670}
59 : #define NS_UK_STRING_PROBDETECTOR_CID \
60 : { 0x2002f784, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
61 :
62 : static const PRUint8 *gCyrillicCls[5] =
63 : {
64 : CP1251Map,
65 : KOI8Map,
66 : ISO88595Map,
67 : MacCyrillicMap,
68 : IBM866Map
69 : };
70 :
71 : static const char * gRussian[5] = {
72 : "windows-1251",
73 : "KOI8-R",
74 : "ISO-8859-5",
75 : "x-mac-cyrillic",
76 : "IBM866"
77 : };
78 :
79 : static const char * gUkrainian[5] = {
80 : "windows-1251",
81 : "KOI8-U",
82 : "ISO-8859-5",
83 : "x-mac-cyrillic",
84 : "IBM866"
85 : };
86 :
87 : #define NUM_CYR_CHARSET 5
88 :
89 : class nsCyrillicDetector
90 : {
91 : public:
92 0 : nsCyrillicDetector(PRUint8 aItems,
93 : const PRUint8 ** aCyrillicClass,
94 0 : const char **aCharsets) {
95 0 : mItems = aItems;
96 0 : mCyrillicClass = aCyrillicClass;
97 0 : mCharsets = aCharsets;
98 0 : for(PRUintn i=0;i<mItems;i++)
99 0 : mProb[i] = mLastCls[i] =0;
100 0 : mDone = false;
101 0 : }
102 0 : virtual ~nsCyrillicDetector() {}
103 : virtual void HandleData(const char* aBuf, PRUint32 aLen);
104 : virtual void DataEnd();
105 : protected:
106 : virtual void Report(const char* aCharset) = 0;
107 : bool mDone;
108 :
109 : private:
110 : PRUint8 mItems;
111 : const PRUint8 ** mCyrillicClass;
112 : const char** mCharsets;
113 : PRUint32 mProb[NUM_CYR_CHARSET];
114 : PRUint8 mLastCls[NUM_CYR_CHARSET];
115 : };
116 :
117 : class nsCyrXPCOMDetector :
118 : public nsCyrillicDetector,
119 : public nsICharsetDetector
120 : {
121 : public:
122 : // nsISupports interface
123 : NS_DECL_ISUPPORTS
124 : nsCyrXPCOMDetector(PRUint8 aItems,
125 : const PRUint8 ** aCyrillicClass,
126 : const char **aCharsets);
127 : virtual ~nsCyrXPCOMDetector();
128 : NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
129 : NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, bool *oDontFeedMe);
130 : NS_IMETHOD Done();
131 : protected:
132 : virtual void Report(const char* aCharset);
133 : private:
134 : nsCOMPtr<nsICharsetDetectionObserver> mObserver;
135 : };
136 :
137 : class nsCyrXPCOMStringDetector :
138 : public nsCyrillicDetector,
139 : public nsIStringCharsetDetector
140 : {
141 : public:
142 : // nsISupports interface
143 : NS_DECL_ISUPPORTS
144 : nsCyrXPCOMStringDetector(PRUint8 aItems,
145 : const PRUint8 ** aCyrillicClass,
146 : const char **aCharsets);
147 : virtual ~nsCyrXPCOMStringDetector();
148 : NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen,
149 : const char** oCharset, nsDetectionConfident &oConf);
150 : protected:
151 : virtual void Report(const char* aCharset);
152 : private:
153 : nsCOMPtr<nsICharsetDetectionObserver> mObserver;
154 : const char* mResult;
155 : };
156 :
157 : class nsRUProbDetector : public nsCyrXPCOMDetector
158 0 : {
159 : public:
160 0 : nsRUProbDetector()
161 0 : : nsCyrXPCOMDetector(5, gCyrillicCls, gRussian) {}
162 : };
163 :
164 : class nsRUStringProbDetector : public nsCyrXPCOMStringDetector
165 0 : {
166 : public:
167 0 : nsRUStringProbDetector()
168 0 : : nsCyrXPCOMStringDetector(5, gCyrillicCls, gRussian) {}
169 : };
170 :
171 : class nsUKProbDetector : public nsCyrXPCOMDetector
172 0 : {
173 : public:
174 0 : nsUKProbDetector()
175 0 : : nsCyrXPCOMDetector(5, gCyrillicCls, gUkrainian) {}
176 : };
177 :
178 : class nsUKStringProbDetector : public nsCyrXPCOMStringDetector
179 0 : {
180 : public:
181 0 : nsUKStringProbDetector()
182 0 : : nsCyrXPCOMStringDetector(5, gCyrillicCls, gUkrainian) {}
183 : };
184 :
185 : #endif
|