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