]> sigrok.org Git - pulseview.git/blame - pv/devices/inputfile.cpp
Rework decode sample count getters
[pulseview.git] / pv / devices / inputfile.cpp
CommitLineData
5237f0c5
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
5237f0c5
JH
18 */
19
20#include <cassert>
21#include <fstream>
22
23#include <QString>
24
25#include "inputfile.hpp"
26
6f925ba9
UH
27using std::map;
28using std::shared_ptr;
29using std::streamsize;
30using std::string;
31using std::ifstream;
32using std::ios;
33
5237f0c5
JH
34namespace pv {
35namespace devices {
36
6f925ba9 37const streamsize InputFile::BufferSize = 16384;
5237f0c5 38
6f925ba9
UH
39InputFile::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) :
dd3fd4df 43 File(file_name),
5237f0c5 44 context_(context),
519d0ccb
SA
45 format_(format),
46 options_(options),
2ad82c2e
UH
47 interrupt_(false)
48{
5237f0c5
JH
49}
50
2ad82c2e
UH
51void InputFile::open()
52{
4d6c6ea3
SA
53 if (session_)
54 close();
519d0ccb
SA
55 else
56 session_ = context_->create_session();
cda51567
SA
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
6f925ba9 65 f = new ifstream(file_name_, ios::binary);
cda51567
SA
66
67 char buffer[BufferSize];
68 f->read(buffer, BufferSize);
6f925ba9 69 const streamsize size = f->gcount();
cda51567
SA
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_);
5237f0c5
JH
82}
83
2ad82c2e
UH
84void InputFile::close()
85{
4d6c6ea3
SA
86 if (session_)
87 session_->remove_devices();
88}
89
2ad82c2e
UH
90void InputFile::start()
91{
5237f0c5
JH
92}
93
2ad82c2e
UH
94void InputFile::run()
95{
5237f0c5 96 char buffer[BufferSize];
519d0ccb 97
cda51567
SA
98 if (!f) {
99 // Previous call to run() processed the entire file already
6f925ba9 100 f = new ifstream(file_name_, ios::binary);
cda51567
SA
101 input_->reset();
102 }
5237f0c5
JH
103
104 interrupt_ = false;
cda51567
SA
105 while (!interrupt_ && !f->eof()) {
106 f->read(buffer, BufferSize);
6f925ba9 107 const streamsize size = f->gcount();
5237f0c5
JH
108 if (size == 0)
109 break;
110
111 input_->send(buffer, size);
112
5237f0c5
JH
113 if (size != BufferSize)
114 break;
115 }
116
117 input_->end();
cda51567
SA
118
119 delete f;
120 f = nullptr;
5237f0c5
JH
121}
122
2ad82c2e
UH
123void InputFile::stop()
124{
5237f0c5
JH
125 interrupt_ = true;
126}
127
128} // namespace devices
129} // namespace pv