Debian Codegraph @

libqnapi/src/utils/encodingutils.cpp

raw symbols SCIP
1 /*****************************************************************************
2 ** QNapi
3 ** Copyright (C) 2008-2017 Piotr Krzemiński <[email protected]>
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 *****************************************************************************/
14
15 #include "encodingutils.h"
16
17 #include <QFile>
18 #include <QTextCodec>
19 #include <QTextStream>
20
21 EncodingUtils::EncodingUtils() {
22 diacritics = QString::fromUtf8(
23 "ąćęłńśżźĄĆĘŁŃŚŻŹŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïð"
24 "ñòóôõöøùúûüýÿ");
25 replacements << "a"
26 << "c"
27 << "e"
28 << "l"
29 << "n"
30 << "s"
31 << "z"
32 << "z"
33 << "A"
34 << "C"
35 << "E"
36 << "L"
37 << "N"
38 << "S"
39 << "Z"
40 << "Z"
41 << "S"
42 << "OE"
43 << "Z"
44 << "s"
45 << "oe"
46 << "z"
47 << "Y"
48 << "Y"
49 << "u"
50 << "A"
51 << "A"
52 << "A"
53 << "A"
54 << "A"
55 << "A"
56 << "AE"
57 << "C"
58 << "E"
59 << "E"
60 << "E"
61 << "E"
62 << "I"
63 << "I"
64 << "I"
65 << "I"
66 << "D"
67 << "N"
68 << "O"
69 << "O"
70 << "O"
71 << "O"
72 << "O"
73 << "O"
74 << "U"
75 << "U"
76 << "U"
77 << "U"
78 << "Y"
79 << "s"
80 << "a"
81 << "a"
82 << "a"
83 << "a"
84 << "a"
85 << "a"
86 << "ae"
87 << "c"
88 << "e"
89 << "e"
90 << "e"
91 << "e"
92 << "i"
93 << "i"
94 << "i"
95 << "i"
96 << "o"
97 << "n"
98 << "o"
99 << "o"
100 << "o"
101 << "o"
102 << "o"
103 << "o"
104 << "u"
105 << "u"
106 << "u"
107 << "u"
108 << "y"
109 << "y";
110
111 codecs << "windows-1257"
112 << "ISO-8859-13"
113 << "ISO-8859-16"
114 << "ISO-8859-2"
115 << "windows-1250"
116 << "UTF-8";
117 }
118
119 QString EncodingUtils::replaceDiacriticsWithASCII(const QString &str) const {
120 QString output;
121 for (int i = 0; i < str.length(); i++) {
122 QChar c = str[i];
123 int dIndex = diacritics.indexOf(c);
124 if (dIndex < 0) {
125 output.append(c);
126 } else {
127 QString replacement = replacements[dIndex];
128 output.append(replacement);
129 }
130 }
131 return output;
132 }
133
134 QString EncodingUtils::detectBufferEncoding(const QByteArray &buffer) const {
135 QTextCodec::ConverterState state;
136 QTextCodec::codecForName("UTF-8")->toUnicode(buffer.constData(),
137 buffer.length(), &state);
138
139 if (state.invalidChars == 0) return "UTF-8";
140
141 int bestMatch = 0;
142 QString from;
143
144 foreach (QString codec, codecs) {
145 QTextCodec *tc = QTextCodec::codecForName(qPrintable(codec));
146
147 const QString text = tc->toUnicode(buffer.constData(), buffer.size());
148
149 QStringList chars = QString::fromUtf8("ą/ś/ż/ć/ń/ł/ó/ę").split("/");
150
151 int found = 0;
152
153 foreach (QString c, chars) {
154 if (text.contains(c, Qt::CaseInsensitive)) {
155 ++found;
156 }
157 }
158
159 if (found >= bestMatch) {
160 bestMatch = found;
161 from = codec;
162 }
163 }
164
165 return from;
166 }
167
168 QString EncodingUtils::detectFileEncoding(const QString &filename) const {
169 QFile f(filename);
170 if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
171 QString enc = detectBufferEncoding(f.readAll());
172 f.close();
173 return enc;
174 }
175 return "";
176 }