Fix inconsistent declaration parameter names.
[pulseview.git] / android / assetreader.cpp
blobf14e7b65117933b5a153c0cf704e2af5dad191fb
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "assetreader.hpp"
21 #include <libsigrok/libsigrok.h>
22 #include <memory>
23 #include <QtCore/QDebug>
24 #include <QtCore/QFile>
25 #include <QtCore/QStandardPaths>
27 using namespace pv;
29 AndroidAssetReader::~AndroidAssetReader()
32 void AndroidAssetReader::open(struct sr_resource *res, std::string name)
34 if (res->type == SR_RESOURCE_FIRMWARE) {
35 auto path = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
36 QString::fromStdString("sigrok-firmware/" + name));
37 if (path.isEmpty())
38 path = QString::fromStdString("assets:/sigrok-firmware/" + name);
40 std::unique_ptr<QFile> file {new QFile{path}};
42 if (!file->open(QIODevice::ReadOnly))
43 throw sigrok::Error{SR_ERR};
45 const auto size = file->size();
46 if (size < 0)
47 throw sigrok::Error{SR_ERR};
49 res->size = size;
50 res->handle = file.release();
51 } else {
52 qWarning() << "AndroidAssetReader: Unknown resource type" << res->type;
53 throw sigrok::Error{SR_ERR};
57 void AndroidAssetReader::close(struct sr_resource *res)
59 if (!res->handle) {
60 qCritical("AndroidAssetReader: Invalid handle");
61 throw sigrok::Error{SR_ERR_ARG};
63 const std::unique_ptr<QFile> file {static_cast<QFile*>(res->handle)};
64 res->handle = nullptr;
66 file->close();
69 size_t AndroidAssetReader::read(const struct sr_resource *res, void *buf, size_t count)
71 if (!res->handle) {
72 qCritical("AndroidAssetReader: Invalid handle");
73 throw sigrok::Error{SR_ERR_ARG};
75 auto *const file = static_cast<QFile*>(res->handle);
77 const auto n_read = file->read(static_cast<char*>(buf), count);
78 if (n_read < 0)
79 throw sigrok::Error{SR_ERR};
81 return n_read;