Fix some random clang-tidy warnings.
[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 using std::map;
28 using std::shared_ptr;
29 using std::streamsize;
30 using std::string;
31 using std::ifstream;
32 using std::ios;
33
34 namespace pv {
35 namespace devices {
36
37 const streamsize InputFile::BufferSize = 16384;
38
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) :
43         File(file_name),
44         context_(context),
45         format_(format),
46         options_(options),
47         interrupt_(false)
48 {
49 }
50
51 void InputFile::open()
52 {
53         if (session_)
54                 close();
55         else
56                 session_ = context_->create_session();
57
58         input_ = format_->create_input(options_);
59
60         if (!input_)
61                 throw QString("Failed to create input");
62
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);
66
67         char buffer[BufferSize];
68         f->read(buffer, BufferSize);
69         const streamsize size = f->gcount();
70         if (size == 0)
71                 return;
72
73         input_->send(buffer, size);
74
75         try {
76                 device_ = input_->device();
77         } catch (sigrok::Error) {
78                 return;
79         }
80
81         session_->add_device(device_);
82 }
83
84 void InputFile::close()
85 {
86         if (session_)
87                 session_->remove_devices();
88 }
89
90 void InputFile::start()
91 {
92 }
93
94 void InputFile::run()
95 {
96         char buffer[BufferSize];
97
98         if (!f) {
99                 // Previous call to run() processed the entire file already
100                 f = new ifstream(file_name_, ios::binary);
101                 input_->reset();
102         }
103
104         interrupt_ = false;
105         while (!interrupt_ && !f->eof()) {
106                 f->read(buffer, BufferSize);
107                 const streamsize size = f->gcount();
108                 if (size == 0)
109                         break;
110
111                 input_->send(buffer, size);
112
113                 if (size != BufferSize)
114                         break;
115         }
116
117         input_->end();
118
119         delete f;
120         f = nullptr;
121 }
122
123 void InputFile::stop()
124 {
125         interrupt_ = true;
126 }
127
128 } // namespace devices
129 } // namespace pv