summaryrefslogtreecommitdiffstats
path: root/src/kile/convert.h
blob: 651d33077d263e83e5a130e4173c232fe9bba329 (plain)
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
/***************************************************************************
    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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef CONVERT_H
#define CONVERT_H

#include <qstring.h>
#include <qstringlist.h>
#include <qmap.h>

#include <kurl.h>

namespace Kate { class Document; }

class ConvertMap
{
protected:
	ConvertMap(const QString & encoding);

public:
	const QString & encoding() const { return m_aliases[0]; }
	const QString & isoName() const { return m_aliases[1]; }

	QChar toEncoding(const QString & enc) { return m_toEncoding[enc]; }
	QString toASCII(const QChar & c) { return m_toASCII[c]; }

	void addPair(QChar c, const QString & enc);

	bool canDecode(const QChar & c) { return ( m_toASCII.contains(c) > 0 ); }
	bool canEncode(const QString & enc) { return ( m_toEncoding.contains(enc) > 0 ); }

	bool load();

private:
	bool commandIsTerminated(const QString &);

private:
	QStringList				m_aliases;
	QMap<QChar, QString>		m_toASCII;
	QMap<QString, QChar>		m_toEncoding;

//static members
public:
	static bool create(const QString & encoding);
	static QString encodingNameFor(const QString &);
	static QString isoNameFor(const QString &);
	static ConvertMap * mapFor(const QString & enc) { return g_maps[enc]; }

private:
	static QMap<QString, ConvertMap*>	g_maps;
};

class ConvertIO
{
public:
	ConvertIO(Kate::Document *doc);
	virtual ~ConvertIO() {}

	virtual void nextLine(); //read next line
	virtual QString & currentLine();
	virtual QString & text() { return m_text; }
	virtual void writeText();
	virtual uint current(); //current line number
	virtual bool done();

protected:
	Kate::Document	*m_doc;
	QString			m_text, m_line;
	uint				m_nLine;
};

class ConvertIOFile : public ConvertIO
{
public:
	ConvertIOFile(Kate::Document *doc, const KURL & url);

	void writeText();

private:
	KURL	m_url;
};

class ConvertBase
{
public:
	ConvertBase(const QString & encoding, ConvertIO * io);
	virtual ~ConvertBase() {};

public:
	virtual bool convert();

protected:
	virtual bool setMap();

	virtual QString mapNext(uint &);

	ConvertIO		*m_io;
	QString			m_encoding;
	ConvertMap		*m_map;
};

class ConvertEncToASCII : public ConvertBase
{
public:
	ConvertEncToASCII(const QString & encoding, ConvertIO * io) : ConvertBase(encoding, io) {}

protected:
	QString mapNext(uint &);
};

class ConvertASCIIToEnc : public ConvertBase
{
public:
	ConvertASCIIToEnc(const QString & encoding, ConvertIO * io) : ConvertBase(encoding, io) {}

protected:
	QString getSequence(uint &);
	QString nextSequence(uint &);
	bool isModifier(const QString &);
	QString mapNext(uint &);
};

#endif