Debian Codegraph @

qt5/src/poppler-qiodeviceoutstream.cc

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