1 /* libwpg
2 * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02111-1301 USA
18 *
19 * For further information visit http://libwpg.sourceforge.net
20 */
22 /* "This product is not manufactured, approved, or supported by
23 * Corel Corporation or Corel Corporation Limited."
24 */
26 #include "WPGStreamImplementation.h"
27 #include "WPGOLEStream.h"
28 #include "libwpg_utils.h"
30 #include <fstream>
31 #include <sstream>
32 #include <string>
34 // MSVC++ 6.0 defines getc as macro, so undefine it
35 #undef getc
37 namespace libwpg
38 {
40 class WPGFileStreamPrivate
41 {
42 public:
43 WPGFileStreamPrivate();
44 std::fstream file;
45 std::stringstream buffer;
46 };
48 class WPGMemoryStreamPrivate
49 {
50 public:
51 WPGMemoryStreamPrivate(const std::string str);
52 std::stringstream buffer;
53 };
55 } // namespace libwpg
57 using namespace libwpg;
59 WPGFileStreamPrivate::WPGFileStreamPrivate() :
60 buffer(std::ios::binary | std::ios::in | std::ios::out)
61 {
62 }
64 WPGMemoryStreamPrivate::WPGMemoryStreamPrivate(const std::string str) :
65 buffer(str, std::ios::binary | std::ios::in)
66 {
67 }
70 WPGFileStream::WPGFileStream(const char* filename)
71 {
72 d = new WPGFileStreamPrivate;
74 d->file.open( filename, std::ios::binary | std::ios::in );
75 }
77 WPGFileStream::~WPGFileStream()
78 {
79 delete d;
80 }
82 unsigned char WPGFileStream::getc()
83 {
84 return d->file.get();
85 }
87 long WPGFileStream::read(long nbytes, char* buffer)
88 {
89 long nread = 0;
91 if(d->file.good())
92 {
93 long curpos = d->file.tellg();
94 d->file.read(buffer, nbytes);
95 nread = (long)d->file.tellg() - curpos;
96 }
98 return nread;
99 }
101 long WPGFileStream::tell()
102 {
103 return d->file.good() ? (long)d->file.tellg() : -1L;
104 }
106 void WPGFileStream::seek(long offset)
107 {
108 if(d->file.good())
109 d->file.seekg(offset);
110 }
112 bool WPGFileStream::atEnd()
113 {
114 return d->file.eof();
115 }
117 bool WPGFileStream::isOle()
118 {
119 if (d->buffer.str().empty())
120 d->buffer << d->file.rdbuf();
121 Storage tmpStorage( d->buffer );
122 if (tmpStorage.isOle())
123 return true;
124 return false;
125 }
127 WPGInputStream* WPGFileStream::getWPGOleStream()
128 {
129 if (d->buffer.str().empty())
130 d->buffer << d->file.rdbuf();
131 Storage *tmpStorage = new Storage( d->buffer );
132 Stream tmpStream( tmpStorage, "PerfectOffice_MAIN" );
133 if (!tmpStorage || (tmpStorage->result() != Storage::Ok) || !tmpStream.size())
134 {
135 if (tmpStorage)
136 delete tmpStorage;
137 return (WPGInputStream*)0;
138 }
140 unsigned char *tmpBuffer = new unsigned char[tmpStream.size()];
141 unsigned long tmpLength;
142 tmpLength = tmpStream.read(tmpBuffer, tmpStream.size());
144 // sanity check
145 if (tmpLength > tmpStream.size() || tmpLength < tmpStream.size())
146 /* something went wrong here and we do not trust the
147 resulting buffer */
148 {
149 if (tmpStorage)
150 delete tmpStorage;
151 return (WPGInputStream*)0;
152 }
154 delete tmpStorage;
155 return new WPGMemoryStream((const char *)tmpBuffer, tmpLength);
156 }
159 WPGMemoryStream::WPGMemoryStream(const char *data, const unsigned int dataSize)
160 {
161 d = new WPGMemoryStreamPrivate(std::string(data, dataSize));
162 }
164 WPGMemoryStream::~WPGMemoryStream()
165 {
166 delete d;
167 }
169 unsigned char WPGMemoryStream::getc()
170 {
171 return d->buffer.get();
172 }
174 long WPGMemoryStream::read(long nbytes, char* buffer)
175 {
176 long nread = 0;
178 if(d->buffer.good())
179 {
180 long curpos = d->buffer.tellg();
181 d->buffer.read(buffer, nbytes);
182 nread = (long)d->buffer.tellg() - curpos;
183 }
185 return nread;
186 }
188 long WPGMemoryStream::tell()
189 {
190 return d->buffer.good() ? (long)d->buffer.tellg() : -1L;
191 }
193 void WPGMemoryStream::seek(long offset)
194 {
195 if(d->buffer.good())
196 d->buffer.seekg(offset);
197 }
199 bool WPGMemoryStream::atEnd()
200 {
201 return d->buffer.eof();
202 }
204 bool WPGMemoryStream::isOle()
205 {
206 Storage tmpStorage( d->buffer );
207 if (tmpStorage.isOle())
208 return true;
209 return false;
210 }
212 WPGInputStream* WPGMemoryStream::getWPGOleStream()
213 {
214 Storage *tmpStorage = new Storage( d->buffer );
215 Stream tmpStream( tmpStorage, "PerfectOffice_MAIN" );
216 if (!tmpStorage || (tmpStorage->result() != Storage::Ok) || !tmpStream.size())
217 {
218 if (tmpStorage)
219 delete tmpStorage;
220 return (WPGInputStream*)0;
221 }
223 unsigned char *tmpBuffer = new unsigned char[tmpStream.size()];
224 unsigned long tmpLength;
225 tmpLength = tmpStream.read(tmpBuffer, tmpStream.size());
227 // sanity check
228 if (tmpLength > tmpStream.size() || tmpLength < tmpStream.size())
229 /* something went wrong here and we do not trust the
230 resulting buffer */
231 {
232 if (tmpStorage)
233 delete tmpStorage;
234 return (WPGInputStream*)0;
235 }
237 delete tmpStorage;
238 return new WPGMemoryStream((const char *)tmpBuffer, tmpLength);
239 }