• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • localization
  • probers
nsSBCSGroupProber.cpp
Go to the documentation of this file.
1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* -*- C++ -*-
3* Copyright (C) 1998 <developer@mozilla.org>
4*
5*
6* Permission is hereby granted, free of charge, to any person obtaining
7* a copy of this software and associated documentation files (the
8* "Software"), to deal in the Software without restriction, including
9* without limitation the rights to use, copy, modify, merge, publish,
10* distribute, sublicense, and/or sell copies of the Software, and to
11* permit persons to whom the Software is furnished to do so, subject to
12* the following conditions:
13*
14* The above copyright notice and this permission notice shall be included
15* in all copies or substantial portions of the Software.
16*
17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24*/
25
26#include "nsSBCSGroupProber.h"
27
28#include "nsSBCharSetProber.h"
29#include "nsHebrewProber.h"
30#include "UnicodeGroupProber.h"
31
32#include <stdio.h>
33#include <stdlib.h>
34
35namespace kencodingprober {
36nsSBCSGroupProber::nsSBCSGroupProber()
37{
38 mProbers[0] = new nsSingleByteCharSetProber(&Win1251Model);
39 mProbers[1] = new nsSingleByteCharSetProber(&Koi8rModel);
40 mProbers[2] = new nsSingleByteCharSetProber(&Latin5Model);
41 mProbers[3] = new nsSingleByteCharSetProber(&MacCyrillicModel);
42 mProbers[4] = new nsSingleByteCharSetProber(&Ibm866Model);
43 mProbers[5] = new nsSingleByteCharSetProber(&Ibm855Model);
44 mProbers[6] = new nsSingleByteCharSetProber(&Latin7Model);
45 mProbers[7] = new nsSingleByteCharSetProber(&Win1253Model);
46 mProbers[8] = new nsSingleByteCharSetProber(&Latin5BulgarianModel);
47 mProbers[9] = new nsSingleByteCharSetProber(&Win1251BulgarianModel);
48
49 nsHebrewProber *hebprober = new nsHebrewProber();
50 // Notice: Any change in these indexes - 10,11,12 must be reflected
51 // in the code below as well.
52 mProbers[10] = hebprober;
53 mProbers[11] = new nsSingleByteCharSetProber(&Win1255Model, false, hebprober); // Logical Hebrew
54 mProbers[12] = new nsSingleByteCharSetProber(&Win1255Model, true, hebprober); // Visual Hebrew
55 mProbers[13] = new UnicodeGroupProber();
56
57 // Tell the Hebrew prober about the logical and visual probers
58 if (mProbers[10] && mProbers[11] && mProbers[12]) // all are not null
59 {
60 hebprober->SetModelProbers(mProbers[11], mProbers[12]);
61 }
62 else // One or more is null. avoid any Hebrew probing, null them all
63 {
64 for (unsigned int i = 10; i <= 12; ++i)
65 {
66 delete mProbers[i];
67 mProbers[i] = 0;
68 }
69 }
70
71 // disable latin2 before latin1 is available, otherwise all latin1
72 // will be detected as latin2 because of their similarity.
73 //mProbers[10] = new nsSingleByteCharSetProber(&Latin2HungarianModel);
74 //mProbers[11] = new nsSingleByteCharSetProber(&Win1250HungarianModel);
75
76 Reset();
77}
78
79nsSBCSGroupProber::~nsSBCSGroupProber()
80{
81 for (unsigned int i = 0; i < NUM_OF_SBCS_PROBERS; i++)
82 {
83 delete mProbers[i];
84 }
85}
86
87
88const char* nsSBCSGroupProber::GetCharSetName()
89{
90 //if we have no answer yet
91 if (mBestGuess == -1)
92 {
93 GetConfidence();
94 //no charset seems positive
95 if (mBestGuess == -1)
96 //we will use default.
97 mBestGuess = 0;
98 }
99 return mProbers[mBestGuess]->GetCharSetName();
100}
101
102void nsSBCSGroupProber::Reset(void)
103{
104 mActiveNum = 0;
105 for (unsigned int i = 0; i < NUM_OF_SBCS_PROBERS; i++)
106 {
107 if (mProbers[i]) // not null
108 {
109 mProbers[i]->Reset();
110 mIsActive[i] = true;
111 ++mActiveNum;
112 }
113 else
114 mIsActive[i] = false;
115 }
116 mBestGuess = -1;
117 mState = eDetecting;
118}
119
120
121nsProbingState nsSBCSGroupProber::HandleData(const char* aBuf, unsigned int aLen)
122{
123 nsProbingState st;
124 unsigned int i;
125 char *newBuf1 = 0;
126 unsigned int newLen1 = 0;
127
128 //apply filter to original buffer, and we got new buffer back
129 //depend on what script it is, we will feed them the new buffer
130 //we got after applying proper filter
131 //this is done without any consideration to KeepEnglishLetters
132 //of each prober since as of now, there are no probers here which
133 //recognize languages with English characters.
134 if (!FilterWithoutEnglishLetters(aBuf, aLen, &newBuf1, newLen1))
135 goto done;
136
137 if (newLen1 == 0)
138 goto done; // Nothing to see here, move on.
139
140 for (i = 0; i < NUM_OF_SBCS_PROBERS; ++i)
141 {
142 if (!mIsActive[i])
143 continue;
144 st = mProbers[i]->HandleData(newBuf1, newLen1);
145 if (st == eFoundIt)
146 {
147 mBestGuess = i;
148 mState = eFoundIt;
149 break;
150 }
151 else if (st == eNotMe)
152 {
153 mIsActive[i] = false;
154 mActiveNum--;
155 if (mActiveNum <= 0)
156 {
157 mState = eNotMe;
158 break;
159 }
160 }
161 }
162
163done:
164 free(newBuf1);
165
166 return mState;
167}
168
169float nsSBCSGroupProber::GetConfidence(void)
170{
171 unsigned int i;
172 float bestConf = 0.0, cf;
173
174 switch (mState)
175 {
176 case eFoundIt:
177 return (float)0.99; //sure yes
178 case eNotMe:
179 return (float)0.01; //sure no
180 default:
181 for (i = 0; i < NUM_OF_SBCS_PROBERS; ++i)
182 {
183 if (!mIsActive[i])
184 continue;
185 cf = mProbers[i]->GetConfidence();
186 if (bestConf < cf)
187 {
188 bestConf = cf;
189 mBestGuess = i;
190 }
191 }
192 }
193 return bestConf;
194}
195
196#ifdef DEBUG_PROBE
197void nsSBCSGroupProber::DumpStatus()
198{
199 unsigned int i;
200 float cf;
201
202 cf = GetConfidence();
203 printf(" SBCS Group Prober --------begin status \r\n");
204 for (i = 0; i < NUM_OF_SBCS_PROBERS; i++)
205 {
206 if (!mIsActive[i])
207 printf(" inactive: [%s] (i.e. confidence is too low).\r\n", mProbers[i]->GetCharSetName());
208 else
209 mProbers[i]->DumpStatus();
210 }
211 printf(" SBCS Group found best match [%s] confidence %f.\r\n",
212 mProbers[mBestGuess]->GetCharSetName(), cf);
213}
214#endif
215}
216
217
UnicodeGroupProber.h
kencodingprober::UnicodeGroupProber
Definition: UnicodeGroupProber.h:34
kencodingprober::nsCharSetProber::Reset
virtual void Reset(void)=0
kencodingprober::nsCharSetProber::HandleData
virtual nsProbingState HandleData(const char *aBuf, unsigned int aLen)=0
kencodingprober::nsCharSetProber::GetConfidence
virtual float GetConfidence(void)=0
kencodingprober::nsCharSetProber::GetCharSetName
virtual const char * GetCharSetName()=0
kencodingprober::nsCharSetProber::FilterWithoutEnglishLetters
static bool FilterWithoutEnglishLetters(const char *aBuf, unsigned int aLen, char **newBuf, unsigned int &newLen)
Definition: nsCharSetProber.cpp:32
kencodingprober::nsHebrewProber
Definition: nsHebrewProber.h:34
kencodingprober::nsHebrewProber::SetModelProbers
void SetModelProbers(nsCharSetProber *logicalPrb, nsCharSetProber *visualPrb)
Definition: nsHebrewProber.h:48
kencodingprober::nsSBCSGroupProber::mIsActive
bool mIsActive[NUM_OF_SBCS_PROBERS]
Definition: nsSBCSGroupProber.h:65
kencodingprober::nsSBCSGroupProber::GetConfidence
float GetConfidence(void)
Definition: nsSBCSGroupProber.cpp:169
kencodingprober::nsSBCSGroupProber::mState
nsProbingState mState
Definition: nsSBCSGroupProber.h:63
kencodingprober::nsSBCSGroupProber::Reset
void Reset(void)
Definition: nsSBCSGroupProber.cpp:102
kencodingprober::nsSBCSGroupProber::GetCharSetName
const char * GetCharSetName()
Definition: nsSBCSGroupProber.cpp:88
kencodingprober::nsSBCSGroupProber::mProbers
nsCharSetProber * mProbers[NUM_OF_SBCS_PROBERS]
Definition: nsSBCSGroupProber.h:64
kencodingprober::nsSBCSGroupProber::mActiveNum
unsigned int mActiveNum
Definition: nsSBCSGroupProber.h:67
kencodingprober::nsSBCSGroupProber::nsSBCSGroupProber
nsSBCSGroupProber()
Definition: nsSBCSGroupProber.cpp:36
kencodingprober::nsSBCSGroupProber::HandleData
nsProbingState HandleData(const char *aBuf, unsigned int aLen)
Definition: nsSBCSGroupProber.cpp:121
kencodingprober::nsSBCSGroupProber::~nsSBCSGroupProber
virtual ~nsSBCSGroupProber()
Definition: nsSBCSGroupProber.cpp:79
kencodingprober::nsSBCSGroupProber::mBestGuess
int mBestGuess
Definition: nsSBCSGroupProber.h:66
kencodingprober::nsSingleByteCharSetProber
Definition: nsSBCharSetProber.h:51
kencodingprober
Definition: CharDistribution.cpp:37
kencodingprober::Ibm855Model
SequenceModel Ibm855Model
Definition: LangCyrillicModel.cpp:336
kencodingprober::Win1251Model
SequenceModel Win1251Model
Definition: LangCyrillicModel.cpp:300
kencodingprober::Latin7Model
SequenceModel Latin7Model
Definition: LangGreekModel.cpp:217
kencodingprober::MacCyrillicModel
SequenceModel MacCyrillicModel
Definition: LangCyrillicModel.cpp:318
kencodingprober::nsProbingState
nsProbingState
Definition: nsCharSetProber.h:34
kencodingprober::eNotMe
@ eNotMe
Definition: nsCharSetProber.h:37
kencodingprober::eFoundIt
@ eFoundIt
Definition: nsCharSetProber.h:36
kencodingprober::eDetecting
@ eDetecting
Definition: nsCharSetProber.h:35
kencodingprober::Win1253Model
SequenceModel Win1253Model
Definition: LangGreekModel.cpp:226
kencodingprober::Latin5Model
SequenceModel Latin5Model
Definition: LangCyrillicModel.cpp:309
kencodingprober::Win1251BulgarianModel
SequenceModel KDE_NO_EXPORT Win1251BulgarianModel
Definition: LangBulgarianModel.cpp:227
kencodingprober::Win1255Model
SequenceModel Win1255Model
Definition: LangHebrewModel.cpp:198
kencodingprober::Koi8rModel
SequenceModel Koi8rModel
Definition: LangCyrillicModel.cpp:291
kencodingprober::Latin5BulgarianModel
SequenceModel KDE_NO_EXPORT Latin5BulgarianModel
Definition: LangBulgarianModel.cpp:218
kencodingprober::Ibm866Model
SequenceModel Ibm866Model
Definition: LangCyrillicModel.cpp:327
nsHebrewProber.h
nsSBCSGroupProber.h
NUM_OF_SBCS_PROBERS
#define NUM_OF_SBCS_PROBERS
Definition: nsSBCSGroupProber.h:44
nsSBCharSetProber.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal