1 /* poppler-qiodevicestream.cc: Qt6 interface to poppler
2  * Copyright (C) 2008, Pino Toscano <pino@kde.org>
3  * Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
4  * Copyright (C) 2020, 2021 Albert Astals Cid <aacid@kde.org>
5  * Copyright (C) 2021, Even Rouault <even.rouault@spatialys.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include "poppler-qiodeviceoutstream-private.h"
23 
24 #include <QtCore/QIODevice>
25 
26 #include <cstdio>
27 
28 namespace Poppler {
29 
QIODeviceOutStream(QIODevice * device)30 QIODeviceOutStream::QIODeviceOutStream(QIODevice *device) : m_device(device) { }
31 
~QIODeviceOutStream()32 QIODeviceOutStream::~QIODeviceOutStream() { }
33 
close()34 void QIODeviceOutStream::close() { }
35 
getPos()36 Goffset QIODeviceOutStream::getPos()
37 {
38     return m_device->pos();
39 }
40 
put(char c)41 void QIODeviceOutStream::put(char c)
42 {
43     m_device->putChar(c);
44 }
45 
46 static int poppler_vasprintf(char **buf_ptr, const char *format, va_list ap) GCC_PRINTF_FORMAT(2, 0);
47 
poppler_vasprintf(char ** buf_ptr,const char * format,va_list ap)48 static int poppler_vasprintf(char **buf_ptr, const char *format, va_list ap)
49 {
50     va_list ap_copy;
51     va_copy(ap_copy, ap);
52     const size_t size = vsnprintf(nullptr, 0, format, ap_copy) + 1;
53     va_end(ap_copy);
54     *buf_ptr = new char[size];
55 
56     return qvsnprintf(*buf_ptr, size, format, ap);
57 }
58 
printf(const char * format,...)59 void QIODeviceOutStream::printf(const char *format, ...)
60 {
61     va_list ap;
62     va_start(ap, format);
63     char *buf;
64     const size_t bufsize = poppler_vasprintf(&buf, format, ap);
65     va_end(ap);
66     m_device->write(buf, bufsize);
67     delete[] buf;
68 }
69 
70 }
71