gui
[lbook_fbreader.git] / fbreader / src / formats / rtf / RtfImage.cpp
blob13104339775baa867ad453b70e65bdb85e10f662
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <ZLStringUtil.h>
21 #include <ZLInputStream.h>
22 #include <ZLFile.h>
24 #include "RtfImage.h"
26 inline static char convertXDigit(char d) {
27 if (isdigit(d)) {
28 return d - '0';
29 } else if (islower(d)) {
30 return d - 'a' + 10;
31 } else {
32 return d - 'A' + 10;
36 void RtfImage::read() const {
37 shared_ptr<ZLInputStream> stream = ZLFile(myFileName).inputStream();
38 if (!stream.isNull() && stream->open()) {
39 myData = new std::string();
40 myData->reserve(myLength / 2);
41 stream->seek(myStartOffset, false);
42 const size_t bufferSize = 1024;
43 char *buffer = new char[bufferSize];
44 for (unsigned int i = 0; i < myLength; i += bufferSize) {
45 size_t toRead = std::min(bufferSize, myLength - i);
46 if (stream->read(buffer, toRead) != toRead) {
47 break;
49 for (size_t j = 0; j < toRead; j += 2) {
50 *myData += (convertXDigit(buffer[j]) << 4) + convertXDigit(buffer[j + 1]);
53 delete[] buffer;
54 stream->close();
58 const shared_ptr<std::string> RtfImage::stringData() const {
59 if (myData.isNull()) {
60 read();
62 return myData;