Line data Source code
1 : /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 : #include <stdio.h>
6 :
7 : #include "nsMBCSGroupProber.h"
8 : #include "nsUniversalDetector.h"
9 :
10 : #if defined(DEBUG_chardet) || defined(DEBUG_jgmyers)
11 : const char *ProberName[] =
12 : {
13 : "UTF8",
14 : "SJIS",
15 : "EUCJP",
16 : };
17 :
18 : #endif
19 :
20 0 : nsMBCSGroupProber::nsMBCSGroupProber()
21 : {
22 0 : mProbers[0] = new nsUTF8Prober();
23 0 : mProbers[1] = new nsSJISProber();
24 0 : mProbers[2] = new nsEUCJPProber();
25 0 : Reset();
26 0 : }
27 :
28 0 : nsMBCSGroupProber::~nsMBCSGroupProber()
29 : {
30 0 : for (uint32_t i = 0; i < NUM_OF_PROBERS; i++)
31 : {
32 0 : delete mProbers[i];
33 : }
34 0 : }
35 :
36 0 : const char* nsMBCSGroupProber::GetCharSetName()
37 : {
38 0 : if (mBestGuess == -1)
39 : {
40 0 : GetConfidence();
41 0 : if (mBestGuess == -1)
42 0 : mBestGuess = 0;
43 : }
44 0 : return mProbers[mBestGuess]->GetCharSetName();
45 : }
46 :
47 0 : void nsMBCSGroupProber::Reset(void)
48 : {
49 0 : mActiveNum = 0;
50 0 : for (uint32_t i = 0; i < NUM_OF_PROBERS; i++)
51 : {
52 0 : if (mProbers[i])
53 : {
54 0 : mProbers[i]->Reset();
55 0 : mIsActive[i] = true;
56 0 : ++mActiveNum;
57 : }
58 : else
59 0 : mIsActive[i] = false;
60 : }
61 0 : mBestGuess = -1;
62 0 : mState = eDetecting;
63 0 : mKeepNext = 0;
64 0 : }
65 :
66 0 : nsProbingState nsMBCSGroupProber::HandleData(const char* aBuf, uint32_t aLen)
67 : {
68 : nsProbingState st;
69 0 : uint32_t start = 0;
70 0 : uint32_t keepNext = mKeepNext;
71 :
72 : //do filtering to reduce load to probers
73 0 : for (uint32_t pos = 0; pos < aLen; ++pos)
74 : {
75 0 : if (aBuf[pos] & 0x80)
76 : {
77 0 : if (!keepNext)
78 0 : start = pos;
79 0 : keepNext = 2;
80 : }
81 0 : else if (keepNext)
82 : {
83 0 : if (--keepNext == 0)
84 : {
85 0 : for (uint32_t i = 0; i < NUM_OF_PROBERS; i++)
86 : {
87 0 : if (!mIsActive[i])
88 0 : continue;
89 0 : st = mProbers[i]->HandleData(aBuf + start, pos + 1 - start);
90 0 : if (st == eFoundIt)
91 : {
92 0 : mBestGuess = i;
93 0 : mState = eFoundIt;
94 0 : return mState;
95 : }
96 : }
97 : }
98 : }
99 : }
100 :
101 0 : if (keepNext) {
102 0 : for (uint32_t i = 0; i < NUM_OF_PROBERS; i++)
103 : {
104 0 : if (!mIsActive[i])
105 0 : continue;
106 0 : st = mProbers[i]->HandleData(aBuf + start, aLen - start);
107 0 : if (st == eFoundIt)
108 : {
109 0 : mBestGuess = i;
110 0 : mState = eFoundIt;
111 0 : return mState;
112 : }
113 : }
114 : }
115 0 : mKeepNext = keepNext;
116 :
117 0 : return mState;
118 : }
119 :
120 0 : float nsMBCSGroupProber::GetConfidence(void)
121 : {
122 : uint32_t i;
123 0 : float bestConf = 0.0, cf;
124 :
125 0 : switch (mState)
126 : {
127 : case eFoundIt:
128 0 : return (float)0.99;
129 : case eNotMe:
130 0 : return (float)0.01;
131 : default:
132 0 : for (i = 0; i < NUM_OF_PROBERS; i++)
133 : {
134 0 : if (!mIsActive[i])
135 0 : continue;
136 0 : cf = mProbers[i]->GetConfidence();
137 0 : if (bestConf < cf)
138 : {
139 0 : bestConf = cf;
140 0 : mBestGuess = i;
141 : }
142 : }
143 : }
144 0 : return bestConf;
145 : }
146 :
147 : #ifdef DEBUG_chardet
148 : void nsMBCSGroupProber::DumpStatus()
149 : {
150 : uint32_t i;
151 : float cf;
152 :
153 : GetConfidence();
154 : for (i = 0; i < NUM_OF_PROBERS; i++)
155 : {
156 : if (!mIsActive[i])
157 : printf(" MBCS inactive: [%s] (confidence is too low).\r\n", ProberName[i]);
158 : else
159 : {
160 : cf = mProbers[i]->GetConfidence();
161 : printf(" MBCS %1.3f: [%s]\r\n", cf, ProberName[i]);
162 : }
163 : }
164 : }
165 : #endif
166 :
167 : #ifdef DEBUG_jgmyers
168 : void nsMBCSGroupProber::GetDetectorState(nsUniversalDetector::DetectorState (&states)[nsUniversalDetector::NumDetectors], uint32_t &offset)
169 : {
170 : for (uint32_t i = 0; i < NUM_OF_PROBERS; ++i) {
171 : states[offset].name = ProberName[i];
172 : states[offset].isActive = mIsActive[i];
173 : states[offset].confidence = mIsActive[i] ? mProbers[i]->GetConfidence() : 0.0;
174 : ++offset;
175 : }
176 : }
177 : #endif /* DEBUG_jgmyers */
|