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
322
323
324
325
326
|
/***************************************************************************
begin : Sun Feb 29 2004
copyright : (C) 2004 by Jeroen Wijnhout
email : Jeroen.Wijnhout@kdemail.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "convert.h"
#include <qregexp.h>
#include <qtextcodec.h>
#include <qfile.h>
#include <kmessagebox.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include "kiledebug.h"
#include <kate/document.h>
QMap<QString, ConvertMap*> ConvertMap::g_maps;
bool ConvertMap::create(const QString & encoding)
{
KILE_DEBUG() << "\tlooking for map for " << encoding << endl;
ConvertMap * map = g_maps[encoding];
if ( map == 0 )
{
KILE_DEBUG() << "\tcreating a map for " << encoding << endl;
map = new ConvertMap(encoding); // FIXME This will never be deleted if load() succeeds...
if ( map->load() )
g_maps[encoding] = map;
else {
delete map;
map = 0L;
}
map = g_maps[encoding];
}
return ( map != 0L );
}
QString ConvertMap::encodingNameFor(const QString & name)
{
QString std;
for ( uint i = 0; i < name.length(); ++i )
if ( !name[i].isSpace() )
std += name[i];
std = std.lower();
if ( std.startsWith("iso8859-") )
return "latin" + std.right(1);
if ( std.startsWith("cp") )
return "cp" + std.right(4);
return name;
}
QString ConvertMap::isoNameFor(const QString & name)
{
QString std;
for ( uint i = 0; i < name.length(); ++i )
if ( !name[i].isSpace() )
std += name[i];
std = std.lower();
if ( std.startsWith("latin") )
return "ISO 8859-" + std.right(1);
if ( std.startsWith("cp" ) )
return "cp " + std.right(4);
return name;
}
ConvertMap::ConvertMap(const QString & enc )
{
m_aliases.append(encodingNameFor(enc));
m_aliases.append(isoNameFor(enc));
}
void ConvertMap::addPair(QChar c, const QString & enc)
{
m_toASCII[c] = commandIsTerminated(enc) ? enc : enc + "{}" ;
m_toEncoding[enc] = c;
}
bool ConvertMap::commandIsTerminated(const QString & command)
{
static QRegExp reCommandSequences("\\\\([a-zA-Z]+|\\\"|\\')$");
return (reCommandSequences.search(command) == -1);
}
bool ConvertMap::load()
{
static QRegExp reMap("^(.*):(.*)");
//makeMap(encoding());
//if map already exists, replace it
QFile qf(KGlobal::dirs()->findResource("appdata","encodings/" + encoding() + ".enc"));
if ( qf.open(IO_ReadOnly) )
{
QTextStream stream( &qf );
QTextCodec *codec = QTextCodec::codecForName(isoName().ascii());
if ( codec ) stream.setCodec(codec);
while ( !stream.atEnd() )
{
//parse the line
if ( stream.readLine().find(reMap) != -1)
addPair(reMap.cap(1)[0], reMap.cap(2));
}
qf.close();
return true;
}
return false;
}
//BEGIN ConvertIO classes
ConvertIO::ConvertIO(Kate::Document *doc) :
m_doc(doc),
m_text(QString::null),
m_line(QString::null),
m_nLine(0)
{
}
QString & ConvertIO::currentLine()
{
return m_line;
}
void ConvertIO::nextLine()
{
m_line = m_doc->textLine(m_nLine++);
}
void ConvertIO::writeText()
{
m_doc->setText(m_text);
}
uint ConvertIO::current()
{
return m_nLine;
}
bool ConvertIO::done()
{
return current() == m_doc->numLines();
}
ConvertIOFile::ConvertIOFile(Kate::Document *doc, const KURL & url) : ConvertIO(doc), m_url(url)
{
}
void ConvertIOFile::writeText()
{
QFile qf(m_url.path());
if ( qf.open(IO_WriteOnly) )
{
//read the file
QTextStream stream( &qf );
stream << m_text;
qf.close();
}
else
kdWarning() << "Could not open " << m_url.path() << endl;
}
ConvertBase::ConvertBase(const QString & encoding, ConvertIO * io) :
m_io(io),
m_encoding(encoding),
m_map(0L)
{
}
//END ConvertIO classes
//BEGIN ConvertBase
QString ConvertBase::mapNext(uint &i)
{
return (QString)m_io->currentLine()[i++];
}
bool ConvertBase::convert()
{
if ( ! setMap() ) return false;
m_io->text() = QString::null;
do
{
m_io->nextLine();
uint i = 0;
while ( i < m_io->currentLine().length() )
{
m_io->text() += mapNext(i);
}
if ( ! m_io->done() ) m_io->text() += '\n';
}
while ( ! m_io->done() );
m_io->writeText();
return true;
}
bool ConvertBase::setMap()
{
//create map (or use existing)
if (ConvertMap::create(m_encoding))
m_map = ConvertMap::mapFor(m_encoding);
else
m_map = 0L;
return ( m_map != 0L );
}
//END ConvertBase
//BEGIN ConvertEncToASCII
QString ConvertEncToASCII::mapNext(uint &i)
{
return m_map->canDecode(m_io->currentLine()[i]) ? m_map->toASCII(m_io->currentLine()[i++]) : (QString)m_io->currentLine()[i++];
}
//END ConvertEncToASCII
//BEGIN ConvertASCIIToEnc
//i is the position of the '\'
QString ConvertASCIIToEnc::nextSequence(uint &i)
{
//get first two characters
QString seq = (QString)m_io->currentLine()[i++];
if ( m_io->currentLine()[i].isLetter() )
{
while ( m_io->currentLine()[i].isLetter() )
seq += (QString)m_io->currentLine()[i++];
}
else
return seq + (QString)m_io->currentLine()[i++];
return seq;
}
bool ConvertASCIIToEnc::isModifier(const QString & seq)
{
static QRegExp reModifier("\\\\([cHkruv]|\"|\'|\\^|`|~|=|\\.)");
return reModifier.exactMatch(seq);
}
QString ConvertASCIIToEnc::getSequence(uint &i)
{
QString seq = nextSequence(i);
static QRegExp reBraces("\\{([a-zA-Z]?)\\}");
if ( isModifier(seq) )
{
KILE_DEBUG() << "\tisModifier true : " << seq << endl;
if ( seq[seq.length() - 1].isLetter() ) seq += ' ';
while ( m_io->currentLine()[i].isSpace() ) i++;
if ( m_io->currentLine().mid(i,2) == "{}" ) i = i + 2;
if ( m_io->currentLine()[i] == '\\' )
seq += nextSequence(i);
else
{
if ( reBraces.exactMatch(m_io->currentLine().mid(i,3)) )
{
KILE_DEBUG() << "\tbraces detected" << endl;
i = i + 3;
seq += reBraces.cap(1);
}
else
{
QChar nextChar = m_io->currentLine()[i++];
if ( !nextChar.isSpace() ) seq += (QString)nextChar;
}
}
}
else if ( m_map->canEncode(seq) )
{
if ( m_io->currentLine().mid(i,2) == "{}" ) i = i + 2;
else if ( m_io->currentLine()[i].isSpace() ) ++i;
}
return seq;
}
QString ConvertASCIIToEnc::mapNext(uint &i)
{
if ( m_io->currentLine()[i] == '\\' )
{
QString seq = getSequence(i);
KILE_DEBUG() << "'\tsequence: " << seq << endl;
if ( m_map->canEncode(seq) )
{
KILE_DEBUG() << "\tcan encode this" << endl;
//if ( m_io->currentLine().mid(i, 2) == "{}" ) i = i + 2;
return m_map->toEncoding(seq);
}
else
return seq;
}
return ConvertBase::mapNext(i);
}
//END ConvertASCIIToEnc
|