]> sigrok.org Git - pulseview.git/blame - android/assetreader.cpp
Fix #1089 by updating the signal labels and group labels
[pulseview.git] / android / assetreader.cpp
CommitLineData
dddff2e7
DE
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "assetreader.hpp"
aca9aa83 21
dddff2e7 22#include <memory>
aca9aa83 23
dddff2e7
DE
24#include <QtCore/QDebug>
25#include <QtCore/QFile>
059599e8 26#include <QtCore/QStandardPaths>
dddff2e7 27
aca9aa83
UH
28#include <libsigrok/libsigrok.h>
29
dddff2e7
DE
30using namespace pv;
31
6f925ba9
UH
32using std::string;
33using std::unique_ptr;
34
6f925ba9 35void AndroidAssetReader::open(struct sr_resource *res, string name)
dddff2e7
DE
36{
37 if (res->type == SR_RESOURCE_FIRMWARE) {
059599e8
DE
38 auto path = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
39 QString::fromStdString("sigrok-firmware/" + name));
40 if (path.isEmpty())
41 path = QString::fromStdString("assets:/sigrok-firmware/" + name);
42
6f925ba9 43 unique_ptr<QFile> file {new QFile{path}};
dddff2e7
DE
44
45 if (!file->open(QIODevice::ReadOnly))
46 throw sigrok::Error{SR_ERR};
47
48 const auto size = file->size();
49 if (size < 0)
50 throw sigrok::Error{SR_ERR};
51
52 res->size = size;
53 res->handle = file.release();
54 } else {
55 qWarning() << "AndroidAssetReader: Unknown resource type" << res->type;
56 throw sigrok::Error{SR_ERR};
57 }
58}
59
60void AndroidAssetReader::close(struct sr_resource *res)
61{
62 if (!res->handle) {
63 qCritical("AndroidAssetReader: Invalid handle");
64 throw sigrok::Error{SR_ERR_ARG};
65 }
6f925ba9 66 const unique_ptr<QFile> file {static_cast<QFile*>(res->handle)};
dddff2e7
DE
67 res->handle = nullptr;
68
69 file->close();
70}
71
72size_t AndroidAssetReader::read(const struct sr_resource *res, void *buf, size_t count)
73{
74 if (!res->handle) {
75 qCritical("AndroidAssetReader: Invalid handle");
76 throw sigrok::Error{SR_ERR_ARG};
77 }
78 auto *const file = static_cast<QFile*>(res->handle);
79
80 const auto n_read = file->read(static_cast<char*>(buf), count);
81 if (n_read < 0)
82 throw sigrok::Error{SR_ERR};
83
84 return n_read;
85}