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/>.
25 #include "inputfile.hpp"
28 using std::shared_ptr;
29 using std::streamsize;
37 const streamsize InputFile::BufferSize = 16384;
39 InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
40 const string &file_name,
41 shared_ptr<sigrok::InputFormat> format,
42 const map<string, Glib::VariantBase> &options) :
51 void InputFile::open()
56 session_ = context_->create_session();
58 input_ = format_->create_input(options_);
61 throw QString("Failed to create input");
63 // open() should add the input device to the session but
64 // we can't open the device without sending some data first
65 f = new ifstream(file_name_, ios::binary);
67 char buffer[BufferSize];
68 f->read(buffer, BufferSize);
69 const streamsize size = f->gcount();
73 input_->send(buffer, size);
76 device_ = input_->device();
77 } catch (sigrok::Error&) {
81 session_->add_device(device_);
84 void InputFile::close()
87 session_->remove_devices();
90 void InputFile::start()
96 char buffer[BufferSize];
99 // Previous call to run() processed the entire file already
100 f = new ifstream(file_name_, ios::binary);
105 while (!interrupt_ && !f->eof()) {
106 f->read(buffer, BufferSize);
107 const streamsize size = f->gcount();
111 input_->send(buffer, size);
113 if (size != BufferSize)
123 void InputFile::stop()
128 } // namespace devices