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 const streamsize InputFile::BufferSize = 16384;
41 InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
42 const string &file_name,
43 shared_ptr<sigrok::InputFormat> format,
44 const map<string, Glib::VariantBase> &options) :
53 void InputFile::open()
58 session_ = context_->create_session();
60 input_ = format_->create_input(options_);
63 throw QString("Failed to create input");
65 // open() should add the input device to the session but
66 // we can't open the device without sending some data first
67 f = new ifstream(file_name_, ios::binary);
69 vector<char> buffer(BufferSize);
71 f->read(buffer.data(), BufferSize);
72 const streamsize size = f->gcount();
76 input_->send(buffer.data(), size);
79 device_ = input_->device();
80 } catch (sigrok::Error&) {
84 session_->add_device(device_);
87 void InputFile::close()
90 session_->remove_devices();
93 void InputFile::start()
100 // Previous call to run() processed the entire file already
101 f = new ifstream(file_name_, ios::binary);
105 vector<char> buffer(BufferSize);
108 while (!interrupt_ && !f->eof()) {
109 f->read(buffer.data(), BufferSize);
110 const streamsize size = f->gcount();
114 input_->send(buffer.data(), size);
116 if (size != BufferSize)
126 void InputFile::stop()
131 } // namespace devices