-
Notifications
You must be signed in to change notification settings - Fork 17
/
WrappedUniversalDetector.cpp
133 lines (109 loc) · 3.1 KB
/
WrappedUniversalDetector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* WrappedUniversalDetector.cpp
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "WrappedUniversalDetector.h"
#include "universalchardet/nscore.h"
#include "universalchardet/nsUniversalDetector.h"
#include "universalchardet/nsCharSetProber.h"
class wrappedUniversalDetector:public nsUniversalDetector
{
public:
wrappedUniversalDetector():nsUniversalDetector(NS_FILTER_ALL) {}
void Report(const char* aCharset) {}
const char *charset(float &confidence)
{
if(!mGotData)
{
confidence=0;
return 0;
}
if(mDetectedCharset)
{
confidence=1;
return mDetectedCharset;
}
switch(mInputState)
{
case eHighbyte:
{
float proberConfidence;
float maxProberConfidence = (float)0.0;
PRInt32 maxProber = 0;
for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
{
proberConfidence = mCharSetProbers[i]->GetConfidence();
if (proberConfidence > maxProberConfidence)
{
maxProberConfidence = proberConfidence;
maxProber = i;
}
}
confidence=maxProberConfidence;
return mCharSetProbers[maxProber]->GetCharSetName();
}
break;
case ePureAscii:
confidence=0;
return "US-ASCII";
default:
break;
}
confidence=0;
return 0;
}
bool done()
{
if(mDetectedCharset) return true;
return false;
}
void reset() { Reset(); }
};
extern "C" {
void *AllocUniversalDetector()
{
return (void *)new wrappedUniversalDetector;
}
void FreeUniversalDetector(void *detectorptr)
{
delete (wrappedUniversalDetector *)detectorptr;
}
void UniversalDetectorHandleData(void *detectorptr,const char *data,int length)
{
if(length==0) return; // There seems to be a bug in UniversalDetector that accesses beyond the end of 0-length buffers.
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorptr;
if(detector->done()) return;
detector->HandleData(data,length);
}
void UniversalDetectorReset(void *detectorptr)
{
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorptr;
detector->reset();
}
int UniversalDetectorDone(void *detectorptr)
{
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorptr;
return detector->done()?1:0;
}
const char *UniversalDetectorCharset(void *detectorptr,float *confidence)
{
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorptr;
return detector->charset(*confidence);
}
}