]> sigrok.org Git - pulseview.git/blob - pv/devices/inputfile.cpp
license: remove FSF postal address from boiler plate license text
[pulseview.git] / pv / devices / inputfile.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <cassert>
21 #include <fstream>
22
23 #include <QString>
24
25 #include "inputfile.hpp"
26
27 namespace pv {
28 namespace devices {
29
30 const std::streamsize InputFile::BufferSize = 16384;
31
32 InputFile::InputFile(const std::shared_ptr<sigrok::Context> &context,
33         const std::string &file_name,
34         std::shared_ptr<sigrok::InputFormat> format,
35         const std::map<std::string, Glib::VariantBase> &options) :
36         File(file_name),
37         context_(context),
38         format_(format),
39         options_(options),
40         interrupt_(false)
41 {
42 }
43
44 void InputFile::open()
45 {
46         if (session_)
47                 close();
48         else
49                 session_ = context_->create_session();
50
51         input_ = format_->create_input(options_);
52
53         if (!input_)
54                 throw QString("Failed to create input");
55
56         // open() should add the input device to the session but
57         // we can't open the device without sending some data first
58         f = new std::ifstream(file_name_, std::ios::binary);
59
60         char buffer[BufferSize];
61         f->read(buffer, BufferSize);
62         const std::streamsize size = f->gcount();
63         if (size == 0)
64                 return;
65
66         input_->send(buffer, size);
67
68         try {
69                 device_ = input_->device();
70         } catch (sigrok::Error) {
71                 return;
72         }
73
74         session_->add_device(device_);
75 }
76
77 void InputFile::close()
78 {
79         if (session_)
80                 session_->remove_devices();
81 }
82
83 void InputFile::start()
84 {
85 }
86
87 void InputFile::run()
88 {
89         char buffer[BufferSize];
90
91         if (!f) {
92                 // Previous call to run() processed the entire file already
93                 f = new std::ifstream(file_name_, std::ios::binary);
94                 input_->reset();
95         }
96
97         interrupt_ = false;
98         while (!interrupt_ && !f->eof()) {
99                 f->read(buffer, BufferSize);
100                 const std::streamsize size = f->gcount();
101                 if (size == 0)
102                         break;
103
104                 input_->send(buffer, size);
105
106                 if (size != BufferSize)
107                         break;
108         }
109
110         input_->end();
111
112         delete f;
113         f = nullptr;
114 }
115
116 void InputFile::stop()
117 {
118         interrupt_ = true;
119 }
120
121 } // namespace devices
122 } // namespace pv