3
Copyright (c) 2006-2007, BBR Inc. All rights reserved.
5
Permission is hereby granted, free of charge, to any person obtaining
6
a copy of this software and associated documentation files (the
7
"Software"), to deal in the Software without restriction, including
8
without limitation the rights to use, copy, modify, merge, publish,
9
distribute, sublicense, and/or sell copies of the Software, and to
10
permit persons to whom the Software is furnished to do so, subject to
11
the following conditions:
13
The above copyright notice and this permission notice shall be included
14
in all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
pdftopdf stream for output
32
#include "P2POutputStream.h"
37
#define LINEBUFSIZE 1024
41
P2POutputStream::P2POutputStream(FILE *fpA)
48
P2POutputStream::~P2POutputStream()
52
int P2POutputStream::puts(const char *str)
54
return write(str,strlen(str));
57
int P2POutputStream::putchar(char c)
61
if ((r = write(&c,1)) < 0) return r;
65
int P2POutputStream::printf(const char *format,...)
67
char linebuf[LINEBUFSIZE];
72
r = vsnprintf(linebuf,LINEBUFSIZE,format,ap);
75
if (puts(linebuf) < 0) {
82
int P2POutputStream::write(const void *buf, int n)
87
char outputBuf[LINEBUFSIZE*2];
90
zstream.next_in = const_cast<Bytef *>(static_cast<const Bytef *>(buf));
95
zstream.next_out = reinterpret_cast<Bytef *>(outputBuf);
96
zstream.avail_out = LINEBUFSIZE*2;
97
if ((r = deflate(&zstream,0)) != Z_OK) {
98
p2pError(-1,const_cast<char *>("ZLIB:deflate error"));
103
outputLen = LINEBUFSIZE*2-zstream.avail_out;
104
if ((r = fwrite(outputBuf,1,outputLen,fp)) != outputLen) {
105
p2pError(-1,const_cast<char *>("P2POutputStream: write error"));
109
} while (zstream.avail_in > 0);
113
if ((r = fwrite(buf,1,n,fp)) != n) {
114
p2pError(-1,const_cast<char *>("P2POutputStream: write error"));
124
/* define alloc functions */
126
voidpf gmalloc_zalloc(voidpf opaque, uInt items, uInt size)
128
return gmalloc(items*size);
130
void gmalloc_zfree(voidpf opqaue, voidpf address)
136
void P2POutputStream::startDeflate()
139
zstream.zalloc = gmalloc_zalloc;
140
zstream.zfree = gmalloc_zfree;
143
if (deflateInit(&zstream,Z_DEFAULT_COMPRESSION) != Z_OK) {
144
p2pError(-1,const_cast<char *>("ZLIB:deflateInit error"));
151
void P2POutputStream::endDeflate()
154
char outputBuf[LINEBUFSIZE*2];
157
if (!deflating) return;
162
zstream.next_out = reinterpret_cast<Bytef *>(outputBuf);
163
zstream.avail_out = LINEBUFSIZE*2;
165
zstream.avail_in = 0;
166
if ((r = deflate(&zstream,Z_FINISH)) != Z_STREAM_END && r != Z_OK) {
167
p2pError(-1,const_cast<char *>("ZLIB:deflate error"));
170
/* Z_OK or Z_STREAM_END */
172
outputLen = LINEBUFSIZE*2-zstream.avail_out;
173
if ((n = fwrite(outputBuf,1,outputLen,fp)) != outputLen) {
174
p2pError(-1,const_cast<char *>("P2POutputStream: write error"));
178
} while (r != Z_STREAM_END);
179
if (deflateEnd(&zstream) != Z_OK) {
180
p2pError(-1,const_cast<char *>("ZLIB:deflateEnd error"));