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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* This file is part of the PulseView project.
*
* Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <fstream>
#include <vector>
#include <QDebug>
#include <QString>
#include <pv/globalsettings.hpp>
#include "inputfile.hpp"
using sigrok::InputFormat;
using std::map;
using std::out_of_range;
using std::pair;
using std::shared_ptr;
using std::streamsize;
using std::string;
using std::ifstream;
using std::ios;
using std::vector;
namespace pv {
namespace devices {
// Use a 4MB chunk size for reading a file into memory. Larger values don't
// seem to provide any substancial performance improvements, but can cause
// UI lag and a visually "stuttering" display of the data currently loading.
const streamsize InputFile::BufferSize = (4 * 1024 * 1024);
InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
const string &file_name,
shared_ptr<sigrok::InputFormat> format,
const map<string, Glib::VariantBase> &options) :
File(file_name),
context_(context),
format_(format),
options_(options),
interrupt_(false)
{
}
InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
QSettings &settings):
File(""),
context_(context),
interrupt_(false)
{
file_name_ = settings.value("filename").toString().toStdString();
QString format_name = settings.value("format").toString();
// Find matching format
const map<string, shared_ptr<InputFormat> > formats = context->input_formats();
try {
format_ = formats.at(format_name.toStdString());
// Restore all saved options
int options = settings.value("options").toInt();
for (int i = 0; i < options; i++) {
settings.beginGroup("option" + QString::number(i));
QString name = settings.value("name").toString();
options_[name.toStdString()] = GlobalSettings::restore_variantbase(settings);
settings.endGroup();
}
} catch (out_of_range&) {
qWarning() << "Could not find input format" << format_name <<
"needed to restore session input file";
}
}
void InputFile::save_meta_to_settings(QSettings &settings)
{
settings.setValue("filename", QString::fromStdString(file_name_));
settings.setValue("format", QString::fromStdString(format_->name()));
settings.setValue("options", (int)options_.size());
int i = 0;
for (const pair<string, Glib::VariantBase>& option : options_) {
settings.beginGroup("option" + QString::number(i));
settings.setValue("name", QString::fromStdString(option.first));
GlobalSettings::store_variantbase(settings, option.second);
settings.endGroup();
i++;
}
}
void InputFile::open()
{
if (session_)
close();
else
session_ = context_->create_session();
if (!format_)
return;
input_ = format_->create_input(options_);
if (!input_)
throw QString("Failed to create input");
// open() should add the input device to the session but
// we can't open the device without sending some data first
f = new ifstream(file_name_, ios::binary);
vector<char> buffer(BufferSize);
f->read(buffer.data(), BufferSize);
const streamsize size = f->gcount();
if (size == 0)
throw QString("Failed to read file");
input_->send(buffer.data(), size);
try {
device_ = input_->device();
} catch (sigrok::Error& e) {
throw e;
}
session_->add_device(device_);
}
void InputFile::close()
{
if (session_)
session_->remove_devices();
}
void InputFile::start()
{
}
void InputFile::run()
{
if (!input_)
return;
if (!f) {
// Previous call to run() processed the entire file already
f = new ifstream(file_name_, ios::binary);
input_->reset();
}
vector<char> buffer(BufferSize);
interrupt_ = false;
while (!interrupt_ && !f->eof()) {
f->read(buffer.data(), BufferSize);
const streamsize size = f->gcount();
if (size == 0)
break;
input_->send(buffer.data(), size);
if (size != BufferSize)
break;
}
input_->end();
delete f;
f = nullptr;
}
void InputFile::stop()
{
interrupt_ = true;
}
} // namespace devices
} // namespace pv
|