File: afcfilereader.cpp

package info (click to toggle)
kio-extras 4%3A25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 31,928 kB
  • sloc: cpp: 28,852; ansic: 3,084; perl: 1,048; xml: 116; sh: 92; python: 28; makefile: 9
file content (72 lines) | stat: -rw-r--r-- 1,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "afcfile.h"

#include "afc_debug.h"

#include "afcfilereader.h"
#include "afcutils.h"

#include <limits>

using namespace KIO;

AfcFileReader::AfcFileReader(const AfcClient::Ptr &client, uint64_t handle)
    : m_client(client)
    , m_handle(handle)
{
}

filesize_t AfcFileReader::size() const
{
    return m_size;
}

void AfcFileReader::setSize(filesize_t size)
{
    m_size = size;
    m_remainingSize = size;
    m_data.clear();
}

WorkerResult AfcFileReader::read()
{
    m_data.clear();

    if (m_remainingSize == 0) {
        return WorkerResult::pass();
    }

    int bytesToRead = std::numeric_limits<int>::max();
    if (m_remainingSize < static_cast<filesize_t>(bytesToRead)) {
        bytesToRead = m_remainingSize;
    }

    if (m_data.length() < bytesToRead) {
        m_data.resize(bytesToRead);
    }

    uint32_t bytesRead = 0;
    afc_error_t ret = afc_file_read(m_client->internalClient(), m_handle, m_data.data(), bytesToRead, &bytesRead);
    m_data.resize(bytesRead);

    if (ret != AFC_E_SUCCESS && ret != AFC_E_END_OF_DATA) {
        return AfcUtils::Result::from(ret);
    }

    m_remainingSize -= bytesRead;
    return WorkerResult::pass();
}

QByteArray AfcFileReader::data() const
{
    return m_data;
}

bool AfcFileReader::hasMore() const
{
    return m_remainingSize > 0;
}