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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • localization
  • probers
nsMBCSGroupProber.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 "nsMBCSGroupProber.h"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 namespace kencodingprober {
32 #ifdef DEBUG_PROBE
33 static const char* const ProberName[] =
34 {
35  "Unicode",
36  "SJIS",
37  "EUCJP",
38  "GB18030",
39  "EUCKR",
40  "Big5",
41 };
42 
43 #endif
44 
45 nsMBCSGroupProber::nsMBCSGroupProber()
46 {
47  mProbers[0] = new UnicodeGroupProber();
48  mProbers[1] = new nsSJISProber();
49  mProbers[2] = new nsEUCJPProber();
50  mProbers[3] = new nsGB18030Prober();
51  mProbers[4] = new nsEUCKRProber();
52  mProbers[5] = new nsBig5Prober();
53  Reset();
54 }
55 
56 nsMBCSGroupProber::~nsMBCSGroupProber()
57 {
58  for (unsigned int i = 0; i < NUM_OF_PROBERS; i++)
59  {
60  delete mProbers[i];
61  }
62 }
63 
64 const char* nsMBCSGroupProber::GetCharSetName()
65 {
66  if (mBestGuess == -1)
67  {
68  GetConfidence();
69  if (mBestGuess == -1)
70  mBestGuess = 0;
71  }
72  return mProbers[mBestGuess]->GetCharSetName();
73 }
74 
75 void nsMBCSGroupProber::Reset(void)
76 {
77  mActiveNum = 0;
78  for (unsigned int i = 0; i < NUM_OF_PROBERS; i++)
79  {
80  if (mProbers[i])
81  {
82  mProbers[i]->Reset();
83  mIsActive[i] = true;
84  ++mActiveNum;
85  }
86  else
87  mIsActive[i] = false;
88  }
89  mBestGuess = -1;
90  mState = eDetecting;
91 }
92 
93 nsProbingState nsMBCSGroupProber::HandleData(const char* aBuf, unsigned int aLen)
94 {
95  nsProbingState st;
96  unsigned int i;
97 
98  //do filtering to reduce load to probers
99  char *highbyteBuf;
100  char *hptr;
101  bool keepNext = true; //assume previous is not ascii, it will do no harm except add some noise
102  hptr = highbyteBuf = (char*)malloc(aLen);
103  if (!hptr)
104  return mState;
105  for (i = 0; i < aLen; ++i)
106  {
107  if (aBuf[i] & 0x80)
108  {
109  *hptr++ = aBuf[i];
110  keepNext = true;
111  }
112  else
113  {
114  //if previous is highbyte, keep this even it is a ASCII
115  if (keepNext)
116  {
117  *hptr++ = aBuf[i];
118  keepNext = false;
119  }
120  }
121  }
122 
123  for (i = 0; i < NUM_OF_PROBERS; ++i)
124  {
125  if (!mIsActive[i])
126  continue;
127  st = mProbers[i]->HandleData(highbyteBuf, hptr - highbyteBuf);
128  if (st == eFoundIt)
129  {
130  mBestGuess = i;
131  mState = eFoundIt;
132  break;
133  }
134  else if (st == eNotMe)
135  {
136  mIsActive[i] = false;
137  mActiveNum--;
138  if (mActiveNum <= 0)
139  {
140  mState = eNotMe;
141  break;
142  }
143  }
144  }
145 
146  free(highbyteBuf);
147 
148  return mState;
149 }
150 
151 float nsMBCSGroupProber::GetConfidence(void)
152 {
153  unsigned int i;
154  float bestConf = 0.0, cf;
155 
156  switch (mState)
157  {
158  case eFoundIt:
159  return (float)0.99;
160  case eNotMe:
161  return (float)0.01;
162  default:
163  for (i = 0; i < NUM_OF_PROBERS; ++i)
164  {
165  if (!mIsActive[i])
166  continue;
167  cf = mProbers[i]->GetConfidence();
168  if (bestConf < cf)
169  {
170  bestConf = cf;
171  mBestGuess = i;
172  }
173  }
174  }
175  return bestConf;
176 }
177 
178 #ifdef DEBUG_PROBE
179 void nsMBCSGroupProber::DumpStatus()
180 {
181  unsigned int i;
182  float cf;
183 
184  GetConfidence();
185  for (i = 0; i < NUM_OF_PROBERS; i++)
186  {
187  if (!mIsActive[i])
188  printf(" MBCS inactive: [%s] (confidence is too low).\r\n", ProberName[i]);
189  else
190  {
191  cf = mProbers[i]->GetConfidence();
192  printf(" MBCS %1.3f: [%s]\r\n", cf, ProberName[i]);
193  }
194  }
195 }
196 #endif
197 }
198 
199 
kencodingprober::nsCharSetProber::GetConfidence
virtual float GetConfidence(void)=0
NUM_OF_PROBERS
#define NUM_OF_PROBERS
Definition: nsMBCSGroupProber.h:36
kencodingprober::nsMBCSGroupProber::mBestGuess
int mBestGuess
Definition: nsMBCSGroupProber.h:57
kencodingprober::nsMBCSGroupProber::HandleData
nsProbingState HandleData(const char *aBuf, unsigned int aLen)
Definition: nsMBCSGroupProber.cpp:93
kencodingprober::nsEUCJPProber
Definition: nsEUCJPProber.h:39
kencodingprober::nsMBCSGroupProber::mIsActive
bool mIsActive[NUM_OF_PROBERS]
Definition: nsMBCSGroupProber.h:56
kencodingprober::nsMBCSGroupProber::GetCharSetName
const char * GetCharSetName()
Definition: nsMBCSGroupProber.cpp:64
kencodingprober::nsCharSetProber::Reset
virtual void Reset(void)=0
kencodingprober::nsMBCSGroupProber::nsMBCSGroupProber
nsMBCSGroupProber()
Definition: nsMBCSGroupProber.cpp:45
kencodingprober::nsMBCSGroupProber::mState
nsProbingState mState
Definition: nsMBCSGroupProber.h:47
kencodingprober::nsCharSetProber::HandleData
virtual nsProbingState HandleData(const char *aBuf, unsigned int aLen)=0
kencodingprober::eFoundIt
Definition: nsCharSetProber.h:36
kencodingprober::UnicodeGroupProber
Definition: UnicodeGroupProber.h:34
kencodingprober::nsEUCKRProber
Definition: nsEUCKRProber.h:33
kencodingprober::nsSJISProber
Definition: nsSJISProber.h:52
nsMBCSGroupProber.h
kencodingprober::nsProbingState
nsProbingState
Definition: nsCharSetProber.h:34
kencodingprober::nsMBCSGroupProber::GetConfidence
float GetConfidence(void)
Definition: nsMBCSGroupProber.cpp:151
kencodingprober::nsBig5Prober
Definition: nsBig5Prober.h:33
kencodingprober::nsGB18030Prober
Definition: nsGB2312Prober.h:35
kencodingprober::eNotMe
Definition: nsCharSetProber.h:37
kencodingprober::nsMBCSGroupProber::mProbers
nsCharSetProber * mProbers[NUM_OF_PROBERS]
Definition: nsMBCSGroupProber.h:55
kencodingprober::nsCharSetProber::GetCharSetName
virtual const char * GetCharSetName()=0
kencodingprober::eDetecting
Definition: nsCharSetProber.h:35
kencodingprober::nsMBCSGroupProber::Reset
void Reset(void)
Definition: nsMBCSGroupProber.cpp:75
kencodingprober::nsMBCSGroupProber::~nsMBCSGroupProber
virtual ~nsMBCSGroupProber()
Definition: nsMBCSGroupProber.cpp:56
kencodingprober::nsMBCSGroupProber::mActiveNum
unsigned int mActiveNum
Definition: nsMBCSGroupProber.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:12 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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