1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2020 Mike Tzou (Chocobo1)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
27 */
28
29 #include "io.h"
30
31 #include <QByteArray>
32 #include <QFileDevice>
33
FileDeviceOutputIterator(QFileDevice & device,const int bufferSize)34 Utils::IO::FileDeviceOutputIterator::FileDeviceOutputIterator(QFileDevice &device, const int bufferSize)
35 : m_device {&device}
36 , m_buffer {std::make_shared<QByteArray>()}
37 , m_bufferSize {bufferSize}
38 {
39 m_buffer->reserve(bufferSize);
40 }
41
~FileDeviceOutputIterator()42 Utils::IO::FileDeviceOutputIterator::~FileDeviceOutputIterator()
43 {
44 if (m_buffer.use_count() == 1)
45 {
46 if (m_device->error() == QFileDevice::NoError)
47 m_device->write(*m_buffer);
48 m_buffer->clear();
49 }
50 }
51
operator =(const char c)52 Utils::IO::FileDeviceOutputIterator &Utils::IO::FileDeviceOutputIterator::operator=(const char c)
53 {
54 m_buffer->append(c);
55 if (m_buffer->size() >= m_bufferSize)
56 {
57 if (m_device->error() == QFileDevice::NoError)
58 m_device->write(*m_buffer);
59 m_buffer->clear();
60 }
61 return *this;
62 }
63