2 * This file is part of the PulseView project.
4 * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
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 2 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/>.
27 #include <pv/globalsettings.hpp>
29 #include "inputfile.hpp"
31 using sigrok::InputFormat;
34 using std::out_of_range;
36 using std::shared_ptr;
37 using std::streamsize;
46 // Use a 4MB chunk size for reading a file into memory. Larger values don't
47 // seem to provide any substancial performance improvements, but can cause
48 // UI lag and a visually "stuttering" display of the data currently loading.
49 const streamsize InputFile::BufferSize = (4 * 1024 * 1024);
51 InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
52 const string &file_name,
53 shared_ptr<sigrok::InputFormat> format,
54 const map<string, Glib::VariantBase> &options) :
63 InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
69 file_name_ = settings.value("filename").toString().toStdString();
71 QString format_name = settings.value("format").toString();
73 // Find matching format
74 const map<string, shared_ptr<InputFormat> > formats = context->input_formats();
77 format_ = formats.at(format_name.toStdString());
79 // Restore all saved options
80 int options = settings.value("options").toInt();
82 for (int i = 0; i < options; i++) {
83 settings.beginGroup("option" + QString::number(i));
84 QString name = settings.value("name").toString();
85 options_[name.toStdString()] = GlobalSettings::restore_variantbase(settings);
89 } catch (out_of_range&) {
90 qWarning() << "Could not find input format" << format_name <<
91 "needed to restore session input file";
95 void InputFile::save_meta_to_settings(QSettings &settings)
97 settings.setValue("filename", QString::fromStdString(file_name_));
99 settings.setValue("format", QString::fromStdString(format_->name()));
101 settings.setValue("options", (int)options_.size());
104 for (const pair<string, Glib::VariantBase>& option : options_) {
105 settings.beginGroup("option" + QString::number(i));
106 settings.setValue("name", QString::fromStdString(option.first));
107 GlobalSettings::store_variantbase(settings, option.second);
113 void InputFile::open()
118 session_ = context_->create_session();
123 input_ = format_->create_input(options_);
126 throw QString("Failed to create input");
128 // open() should add the input device to the session but
129 // we can't open the device without sending some data first
130 f = new ifstream(file_name_, ios::binary);
132 vector<char> buffer(BufferSize);
134 f->read(buffer.data(), BufferSize);
135 const streamsize size = f->gcount();
138 throw QString("Failed to read file");
140 input_->send(buffer.data(), size);
143 device_ = input_->device();
144 } catch (sigrok::Error& e) {
148 session_->add_device(device_);
151 void InputFile::close()
154 session_->remove_devices();
157 void InputFile::start()
161 void InputFile::run()
167 // Previous call to run() processed the entire file already
168 f = new ifstream(file_name_, ios::binary);
172 vector<char> buffer(BufferSize);
175 while (!interrupt_ && !f->eof()) {
176 f->read(buffer.data(), BufferSize);
177 const streamsize size = f->gcount();
181 input_->send(buffer.data(), size);
183 if (size != BufferSize)
193 void InputFile::stop()
198 } // namespace devices