File: qencodingprober.cpp

package info (click to toggle)
texmaker 6.0.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,964 kB
  • sloc: cpp: 417,556; ansic: 102,526; python: 1,935; xml: 1,739; sh: 42; makefile: 36
file content (321 lines) | stat: -rw-r--r-- 10,280 bytes parent folder | download | duplicates (7)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
    This file is part of the KDE libraries

    Copyright (C) 2008 Wang Hoi (zealot.hoi@gmail.com)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "qencodingprober.h"

#include "nsCharSetProber.h"
#include "nsUniversalDetector.h"
#include "ChineseGroupProber.h"
#include "JapaneseGroupProber.h"
#include "UnicodeGroupProber.h"
#include "nsSBCSGroupProber.h"
#include "nsMBCSGroupProber.h"

#include <string.h>

class QEncodingProberPrivate
{
public:
    QEncodingProberPrivate(): prober(NULL), mStart(true) {};
    ~QEncodingProberPrivate()
    {
        delete prober;
    }
    void setProberType(QEncodingProber::ProberType pType)
    {
        proberType = pType;
        /* handle multi-byte encodings carefully , because they're hard to detect,
        *   and have to use some Stastics methods.
        * for single-byte encodings (most western encodings), nsSBCSGroupProber is ok,
        *   because encoding state machine can detect many such encodings.
        */

        delete prober;

        switch (proberType) {
            case QEncodingProber::None:
                prober = NULL;
                break;
            case QEncodingProber::Arabic:
            case QEncodingProber::Baltic:
            case QEncodingProber::CentralEuropean:
            case QEncodingProber::Cyrillic:
            case QEncodingProber::Greek:
            case QEncodingProber::Hebrew:
            case QEncodingProber::NorthernSaami:
            case QEncodingProber::Other:
            case QEncodingProber::SouthEasternEurope:
            case QEncodingProber::Thai:
            case QEncodingProber::Turkish:
            case QEncodingProber::WesternEuropean:
                prober = new qencodingprober::nsSBCSGroupProber();
                break;
            case QEncodingProber::ChineseSimplified:
            case QEncodingProber::ChineseTraditional:
                prober = new qencodingprober::ChineseGroupProber();
                break;
            case QEncodingProber::Japanese:
                prober = new qencodingprober::JapaneseGroupProber();
                break;
            case QEncodingProber::Korean:
                prober = new qencodingprober::nsMBCSGroupProber();
                break;
            case QEncodingProber::Unicode:
                prober = new qencodingprober::UnicodeGroupProber();
                break;
            case QEncodingProber::Universal:
                prober = new qencodingprober::nsUniversalDetector();
                break;
            default:
                prober = NULL;
        }
    }
    void unicodeTest(const char *aBuf, int aLen)
    {
        if (mStart)
        {
            mStart = false;
            if (aLen > 3)
            switch (aBuf[0])
            {
                case '\xEF':
                    if (('\xBB' == aBuf[1]) && ('\xBF' == aBuf[2]))
                    // EF BB BF  UTF-8 encoded BOM
                    proberState = QEncodingProber::FoundIt;
                    break;
                case '\xFE':
                    if (('\xFF' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3]))
                        // FE FF 00 00  UCS-4, unusual octet order BOM (3412)
                        proberState = QEncodingProber::FoundIt;
                    else if ('\xFF' == aBuf[1])
                        // FE FF  UTF-16, big endian BOM
                        proberState = QEncodingProber::FoundIt;
                        break;
                case '\x00':
                    if (('\x00' == aBuf[1]) && ('\xFE' == aBuf[2]) && ('\xFF' == aBuf[3]))
                        // 00 00 FE FF  UTF-32, big-endian BOM
                        proberState = QEncodingProber::FoundIt;
                    else if (('\x00' == aBuf[1]) && ('\xFF' == aBuf[2]) && ('\xFE' == aBuf[3]))
                        // 00 00 FF FE  UCS-4, unusual octet order BOM (2143)
                        proberState = QEncodingProber::FoundIt;
                        break;
                case '\xFF':
                    if (('\xFE' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3]))
                        // FF FE 00 00  UTF-32, little-endian BOM
                        proberState = QEncodingProber::FoundIt;
                    else if ('\xFE' == aBuf[1])
                        // FF FE  UTF-16, little endian BOM
                        proberState = QEncodingProber::FoundIt;
                        break;
            }  // switch

        }
    }
    QEncodingProber::ProberType proberType;
    QEncodingProber::ProberState proberState;
    qencodingprober::nsCharSetProber *prober;
    bool mStart;
};

QEncodingProber::QEncodingProber(QEncodingProber::ProberType proberType): d(new QEncodingProberPrivate())
{
    setProberType(proberType);
}

QEncodingProber::~QEncodingProber()
{
    delete d;
}

void QEncodingProber::reset()
{
    d->proberState = QEncodingProber::Probing;
    d->mStart = true;
}

QEncodingProber::ProberState QEncodingProber::feed(const QByteArray &data)
{
    return feed(data.data(), data.size());
}

QEncodingProber::ProberState QEncodingProber::feed(const char* data, int len)
{
    if (!d->prober)
        return d->proberState;
    if (d->proberState == Probing) {
        if (d->mStart) {
            d->unicodeTest(data, len);
            if (d->proberState == FoundIt)
                return d->proberState;
        }
        d->prober->HandleData(data, len);
        switch (d->prober->GetState())
        {
            case qencodingprober::eNotMe:
                d->proberState = NotMe;
                break;
            case qencodingprober::eFoundIt:
                d->proberState = FoundIt;
                break;
            default:
                d->proberState = Probing;
                break;
        }
    }
#ifdef DEBUG_PROBE
    d->prober->DumpStatus();
#endif
    return d->proberState;
}

QEncodingProber::ProberState QEncodingProber::state() const
{
    return d->proberState;
}

//DEPRECATED, do *not* use
//#ifndef KDE_NO_DEPRECATED
//const char* QEncodingProber::encodingName() const
//{
//    return qstrdup(encoding().constData());
//}
//#endif

QByteArray QEncodingProber::encoding() const
{
    if (!d->prober)
        return QByteArray("UTF-8");

    return QByteArray(d->prober->GetCharSetName());
}

float QEncodingProber::confidence() const
{
    if (!d->prober)
        return 0.0;

    return d->prober->GetConfidence();
}

QEncodingProber::ProberType QEncodingProber::proberType() const
{
    return d->proberType;
}

void QEncodingProber::setProberType(QEncodingProber::ProberType proberType)
{
    d->setProberType(proberType);
    reset();
}

QEncodingProber::ProberType QEncodingProber::proberTypeForName(const QString& lang)
{
    if (lang.isEmpty())
        return QEncodingProber::Universal;
    else if (lang=="Disabled")
        return QEncodingProber::None;
    else if (lang=="Universal")
        return QEncodingProber::Universal;
    else if (lang=="Unicode")
        return QEncodingProber::Unicode;
    else if (lang=="Cyrillic")
        return QEncodingProber::Cyrillic;
    else if (lang=="Western European")
        return QEncodingProber::WesternEuropean;
    else if (lang=="Central European")
        return QEncodingProber::CentralEuropean;
    else if (lang=="Greek")
        return QEncodingProber::Greek;
    else if (lang=="Hebrew")
        return QEncodingProber::Hebrew;
    else if (lang=="Turkish")
        return QEncodingProber::Turkish;
    else if (lang=="Japanese")
        return QEncodingProber::Japanese;
    else if (lang=="Baltic")
        return QEncodingProber::Baltic;
    else if (lang=="Chinese Traditional")
        return QEncodingProber::ChineseTraditional;
    else if (lang=="Chinese Simplified")
        return QEncodingProber::ChineseSimplified;
    else if (lang=="Arabic")
        return QEncodingProber::Arabic;

    return QEncodingProber::Universal;
}

QString QEncodingProber::nameForProberType(QEncodingProber::ProberType proberType)
{
    switch (proberType)
    {
        case QEncodingProber::None:
            return "Disabled";
            break;
        case QEncodingProber::Universal:
            return "Universal";
            break;
        case QEncodingProber::Arabic:
            return "Arabic";
            break;
        case QEncodingProber::Baltic:
            return "Baltic";
            break;
        case QEncodingProber::CentralEuropean:
            return "Central European";
            break;
        case QEncodingProber::Cyrillic:
            return "Cyrillic";
            break;
        case QEncodingProber::Greek:
            return "Greek";
            break;
        case QEncodingProber::Hebrew:
            return "Hebrew";
            break;
        case QEncodingProber::Japanese:
            return "Japanese";
            break;
        case QEncodingProber::Turkish:
            return "Turkish";
            break;
        case QEncodingProber::WesternEuropean:
            return "Western European";
            break;
        case QEncodingProber::ChineseTraditional:
            return "Chinese Traditional";
            break;
        case QEncodingProber::ChineseSimplified:
            return "Chinese Simplified";
            break;
        case QEncodingProber::Korean:
            return "Korean";
            break;
        case QEncodingProber::Thai:
            return "Thai";
            break;
        case QEncodingProber::Unicode:
            return "Unicode";
            break;
        default:
            return QString();
        }
}