summaryrefslogtreecommitdiffstats
path: root/fbreader/src/formats/txt/TxtReader.cpp
blob: d2f5659893aaac6f51689adac54ac2ff8bed2b58 (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
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
/*
 * Copyright (C) 2004-2012 Geometer Plus <[email protected]>
 *
 * 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.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <cctype>

#include <ZLInputStream.h>

#include "TxtReader.h"

class TxtReaderCore {

public:
	TxtReaderCore(TxtReader &reader);
	virtual void readDocument(ZLInputStream &stream);

protected:
	TxtReader &myReader;
};

class TxtReaderCoreUtf16 : public TxtReaderCore {

public:
	TxtReaderCoreUtf16(TxtReader &reader);
	void readDocument(ZLInputStream &stream);

protected:
	virtual char getAscii(const char *ptr) = 0;
	virtual void setAscii(char *ptr, char ascii) = 0;
};

class TxtReaderCoreUtf16LE : public TxtReaderCoreUtf16 {

public:
	TxtReaderCoreUtf16LE(TxtReader &reader);

protected:
	char getAscii(const char *ptr);
	void setAscii(char *ptr, char ascii);
};

class TxtReaderCoreUtf16BE : public TxtReaderCoreUtf16 {

public:
	TxtReaderCoreUtf16BE(TxtReader &reader);

protected:
	char getAscii(const char *ptr);
	void setAscii(char *ptr, char ascii);
};

TxtReader::TxtReader(const std::string &encoding) : EncodedTextReader(encoding) {
	if (ZLEncodingConverter::UTF16 == encoding) {
		myCore = new TxtReaderCoreUtf16LE(*this);
	} else if (ZLEncodingConverter::UTF16BE == encoding) {
		myCore = new TxtReaderCoreUtf16BE(*this);
	} else {
		myCore = new TxtReaderCore(*this);
	}
}

TxtReader::~TxtReader() {
}

void TxtReader::readDocument(ZLInputStream &stream) {
	if (!stream.open()) {
		return;
	}
	startDocumentHandler();
	myCore->readDocument(stream);
	endDocumentHandler();
	stream.close();
}

TxtReaderCore::TxtReaderCore(TxtReader &reader) : myReader(reader) {
}

TxtReaderCoreUtf16::TxtReaderCoreUtf16(TxtReader &reader) : TxtReaderCore(reader) {
}

void TxtReaderCore::readDocument(ZLInputStream &stream) {
	const std::size_t BUFSIZE = 2048;
	char *buffer = new char[BUFSIZE];
	std::string str;
	std::size_t length;
	do {
		length = stream.read(buffer, BUFSIZE);
		char *start = buffer;
		const char *end = buffer + length;
		for (char *ptr = start; ptr != end; ++ptr) {
			if (*ptr == '\n' || *ptr == '\r') {
				bool skipNewLine = false;
				if (*ptr == '\r' && (ptr + 1) != end && *(ptr + 1) == '\n') {
					skipNewLine = true;
					*ptr = '\n';
				}
				if (start != ptr) {
					str.erase();
					myReader.myConverter->convert(str, start, ptr + 1);
					myReader.characterDataHandler(str);
				}
				if (skipNewLine) {
					++ptr;
				}
				start = ptr + 1;
				myReader.newLineHandler();
			} else if (((*ptr) & 0x80) == 0 && std::isspace((unsigned char)*ptr)) {
				if (*ptr != '\t') {
					*ptr = ' ';
				}
			} else {
			}
		}
		if (start != end) {
			str.erase();
			myReader.myConverter->convert(str, start, end);
			myReader.characterDataHandler(str);
		}
	} while (length == BUFSIZE);
	delete[] buffer;
}

void TxtReaderCoreUtf16::readDocument(ZLInputStream &stream) {
	const std::size_t BUFSIZE = 2048;
	char *buffer = new char[BUFSIZE];
	std::string str;
	std::size_t length;
	do {
		length = stream.read(buffer, BUFSIZE);
		char *start = buffer;
		const char *end = buffer + length;
		for (char *ptr = start; ptr < end; ptr += 2) {
			const char chr = getAscii(ptr);
			if (chr == '\n' || chr == '\r') {
				bool skipNewLine = false;
				if (chr == '\r' && ptr + 2 != end && getAscii(ptr + 2) == '\n') {
					skipNewLine = true;
					setAscii(ptr, '\n');
				}
				if (start != ptr) {
					str.erase();
					myReader.myConverter->convert(str, start, ptr + 2);
					myReader.characterDataHandler(str);
				}
				if (skipNewLine) {
					ptr += 2;
				}
				start = ptr + 2;
				myReader.newLineHandler();
			} else if (chr != 0 && ((*ptr) & 0x80) == 0 && std::isspace(chr)) {
				if (chr != '\t') {
					setAscii(ptr, ' ');
				}
			}
		}
		if (start != end) {
			str.erase();
			myReader.myConverter->convert(str, start, end);
			myReader.characterDataHandler(str);
		}
	} while (length == BUFSIZE);
	delete[] buffer;
}

TxtReaderCoreUtf16LE::TxtReaderCoreUtf16LE(TxtReader &reader) : TxtReaderCoreUtf16(reader) {
}

char TxtReaderCoreUtf16LE::getAscii(const char *ptr) {
	return *(ptr + 1) == '\0' ? *ptr : '\0';
}

void TxtReaderCoreUtf16LE::setAscii(char *ptr, char ascii) {
	*ptr = ascii;
}

TxtReaderCoreUtf16BE::TxtReaderCoreUtf16BE(TxtReader &reader) : TxtReaderCoreUtf16(reader) {
}

char TxtReaderCoreUtf16BE::getAscii(const char *ptr) {
	return *ptr == '\0' ? *(ptr + 1) : '\0';
}

void TxtReaderCoreUtf16BE::setAscii(char *ptr, char ascii) {
	*(ptr + 1) = ascii;
}