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/>.
26 #include "inputfile.hpp"
29 using std::shared_ptr;
30 using std::streamsize;
39 // Use a 4MB chunk size for reading a file into memory. Larger values don't
40 // seem to provide any substancial performance improvements, but can cause
41 // UI lag and a visually "stuttering" display of the data currently loading.
42 const streamsize InputFile::BufferSize = (4 * 1024 * 1024);
44 InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
45 const string &file_name,
46 shared_ptr<sigrok::InputFormat> format,
47 const map<string, Glib::VariantBase> &options) :
56 void InputFile::open()
61 session_ = context_->create_session();
63 input_ = format_->create_input(options_);
66 throw QString("Failed to create input");
68 // open() should add the input device to the session but
69 // we can't open the device without sending some data first
70 f = new ifstream(file_name_, ios::binary);
72 vector<char> buffer(BufferSize);
74 f->read(buffer.data(), BufferSize);
75 const streamsize size = f->gcount();
79 input_->send(buffer.data(), size);
82 device_ = input_->device();
83 } catch (sigrok::Error&) {
87 session_->add_device(device_);
90 void InputFile::close()
93 session_->remove_devices();
96 void InputFile::start()
100 void InputFile::run()
103 // Previous call to run() processed the entire file already
104 f = new ifstream(file_name_, ios::binary);
108 vector<char> buffer(BufferSize);
111 while (!interrupt_ && !f->eof()) {
112 f->read(buffer.data(), BufferSize);
113 const streamsize size = f->gcount();
117 input_->send(buffer.data(), size);
119 if (size != BufferSize)
129 void InputFile::stop()
134 } // namespace devices