summaryrefslogtreecommitdiffstats
path: root/reader/src/formats/rtf/RtfReaderStream.cpp
blob: f4537f746b6dc381411b6908c58c7add6d04ef3e (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
/*
 * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
 *
 * 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 <cstring>
#include <cstdlib>
#include <string>

#include "RtfReader.h"
#include "RtfReaderStream.h"

class RtfTextOnlyReader : public RtfReader {

public:
	RtfTextOnlyReader(char *buffer, std::size_t maxSize);
	~RtfTextOnlyReader();
	std::size_t readSize() const;

protected:
	void addCharData(const char *data, std::size_t len, bool convert);
	void insertImage(shared_ptr<ZLMimeType> mimeType, const std::string &fileName, std::size_t startOffset, std::size_t size);
	void setEncoding(int code);
	void switchDestination(DestinationType destination, bool on);
	void setAlignment();
	void setFontProperty(FontProperty property);
	void newParagraph();

	void interrupt();

private:
	struct RtfTextOnlyReaderState {
		bool ReadText;
	};

	RtfTextOnlyReaderState myCurrentState;

private:
	char* myBuffer;
	const std::size_t myMaxSize;
	std::size_t myFilledSize;
};

RtfTextOnlyReader::RtfTextOnlyReader(char *buffer, std::size_t maxSize) : RtfReader(std::string()), myBuffer(buffer), myMaxSize(maxSize), myFilledSize(0) {
	myCurrentState.ReadText = true;
}

RtfTextOnlyReader::~RtfTextOnlyReader() {
}

void RtfTextOnlyReader::addCharData(const char *data, std::size_t len, bool) {
	if (myBuffer == 0) {
		return;
	}
	if (myCurrentState.ReadText) {
		if (myFilledSize < myMaxSize) {
			len = std::min((std::size_t)len, myMaxSize - myFilledSize);
			std::memcpy(myBuffer + myFilledSize, data, len);
			myFilledSize += len;
		}
		if (myFilledSize < myMaxSize) {
			myBuffer[myFilledSize++]=' ';
		} else {
			interrupt();
		}
	}
}

std::size_t RtfTextOnlyReader::readSize() const {
	return myFilledSize;
}

void RtfTextOnlyReader::insertImage(shared_ptr<ZLMimeType>, const std::string&, std::size_t, std::size_t) {
}

void RtfTextOnlyReader::setEncoding(int) {
}

void RtfTextOnlyReader::switchDestination(DestinationType destination, bool on) {
	switch (destination) {
		case DESTINATION_NONE:
			break;
		case DESTINATION_SKIP:
		case DESTINATION_INFO:
		case DESTINATION_TITLE:
		case DESTINATION_AUTHOR:
		case DESTINATION_STYLESHEET:
			myCurrentState.ReadText = !on;
			break;
		case DESTINATION_PICTURE:
			myCurrentState.ReadText = !on;
			break;
		case DESTINATION_FOOTNOTE:
			if (on) {
				myCurrentState.ReadText = true;
			}
			break;
	}
}

void RtfTextOnlyReader::setAlignment() {
}

void RtfTextOnlyReader::setFontProperty(FontProperty) {
}

void RtfTextOnlyReader::newParagraph() {
}

void RtfTextOnlyReader::interrupt() {
}

RtfReaderStream::RtfReaderStream(const ZLFile& file, std::size_t maxSize) : myFile(file), myBuffer(0), mySize(maxSize) {
}

RtfReaderStream::~RtfReaderStream() {
	close();
}

bool RtfReaderStream::open() {
	if (mySize != 0) {
		myBuffer = new char[mySize];
	}
	RtfTextOnlyReader reader(myBuffer, mySize);
	reader.readDocument(myFile);
	mySize = reader.readSize();
	myOffset = 0;
	return true;
}

std::size_t RtfReaderStream::read(char *buffer, std::size_t maxSize) {
	maxSize = std::min(maxSize, mySize - myOffset);
	if ((buffer != 0) && (myBuffer !=0)) {
		std::memcpy(buffer, myBuffer + myOffset, maxSize);
	}
	myOffset += maxSize;
	return maxSize;
}

void RtfReaderStream::close() {
	if (myBuffer != 0) {
		delete[] myBuffer;
		myBuffer = 0;
	}
}

void RtfReaderStream::seek(int offset, bool absoluteOffset) {
	if (!absoluteOffset) {
		offset += myOffset;
	}
	myOffset = std::min(mySize, (std::size_t)std::max(0, offset));
}

std::size_t RtfReaderStream::offset() const {
	return myOffset;
}

std::size_t RtfReaderStream::sizeOfOpened() {
	return mySize;
}