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

KDECore

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