]> sigrok.org Git - pulseview.git/blame - pv/devices/inputfile.cpp
Free unused segment memory after acquisition
[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
27namespace pv {
28namespace devices {
29
30const std::streamsize InputFile::BufferSize = 16384;
31
32InputFile::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) :
dd3fd4df 36 File(file_name),
5237f0c5 37 context_(context),
519d0ccb
SA
38 format_(format),
39 options_(options),
2ad82c2e
UH
40 interrupt_(false)
41{
5237f0c5
JH
42}
43
2ad82c2e
UH
44void InputFile::open()
45{
4d6c6ea3
SA
46 if (session_)
47 close();
519d0ccb
SA
48 else
49 session_ = context_->create_session();
cda51567
SA
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_);
5237f0c5
JH
75}
76
2ad82c2e
UH
77void InputFile::close()
78{
4d6c6ea3
SA
79 if (session_)
80 session_->remove_devices();
81}
82
2ad82c2e
UH
83void InputFile::start()
84{
5237f0c5
JH
85}
86
2ad82c2e
UH
87void InputFile::run()
88{
5237f0c5 89 char buffer[BufferSize];
519d0ccb 90
cda51567
SA
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 }
5237f0c5
JH
96
97 interrupt_ = false;
cda51567
SA
98 while (!interrupt_ && !f->eof()) {
99 f->read(buffer, BufferSize);
100 const std::streamsize size = f->gcount();
5237f0c5
JH
101 if (size == 0)
102 break;
103
104 input_->send(buffer, size);
105
5237f0c5
JH
106 if (size != BufferSize)
107 break;
108 }
109
110 input_->end();
cda51567
SA
111
112 delete f;
113 f = nullptr;
5237f0c5
JH
114}
115
2ad82c2e
UH
116void InputFile::stop()
117{
5237f0c5
JH
118 interrupt_ = true;
119}
120
121} // namespace devices
122} // namespace pv