]> sigrok.org Git - pulseview.git/blame - pv/devices/inputfile.cpp
inputfile: Increase chunk size from 16KB to 4MB.
[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>
3b0acbcb 22#include <vector>
5237f0c5
JH
23
24#include <QString>
25
26#include "inputfile.hpp"
27
6f925ba9
UH
28using std::map;
29using std::shared_ptr;
30using std::streamsize;
31using std::string;
32using std::ifstream;
33using std::ios;
3b0acbcb 34using std::vector;
6f925ba9 35
5237f0c5
JH
36namespace pv {
37namespace devices {
38
1f419596
UH
39// Use a 4MB chunk size for reading a file into memory. Larger values don't
40// seem to provide any substancial performance improvements, but can cause
41// UI lag and a visually "stuttering" display of the data currently loading.
42const streamsize InputFile::BufferSize = (4 * 1024 * 1024);
5237f0c5 43
6f925ba9
UH
44InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
45 const string &file_name,
46 shared_ptr<sigrok::InputFormat> format,
47 const map<string, Glib::VariantBase> &options) :
dd3fd4df 48 File(file_name),
5237f0c5 49 context_(context),
519d0ccb
SA
50 format_(format),
51 options_(options),
2ad82c2e
UH
52 interrupt_(false)
53{
5237f0c5
JH
54}
55
2ad82c2e
UH
56void InputFile::open()
57{
4d6c6ea3
SA
58 if (session_)
59 close();
519d0ccb
SA
60 else
61 session_ = context_->create_session();
cda51567
SA
62
63 input_ = format_->create_input(options_);
64
65 if (!input_)
66 throw QString("Failed to create input");
67
68 // open() should add the input device to the session but
69 // we can't open the device without sending some data first
6f925ba9 70 f = new ifstream(file_name_, ios::binary);
cda51567 71
3b0acbcb
UH
72 vector<char> buffer(BufferSize);
73
74 f->read(buffer.data(), BufferSize);
6f925ba9 75 const streamsize size = f->gcount();
cda51567
SA
76 if (size == 0)
77 return;
78
3b0acbcb 79 input_->send(buffer.data(), size);
cda51567
SA
80
81 try {
82 device_ = input_->device();
30677c13 83 } catch (sigrok::Error&) {
cda51567
SA
84 return;
85 }
86
87 session_->add_device(device_);
5237f0c5
JH
88}
89
2ad82c2e
UH
90void InputFile::close()
91{
4d6c6ea3
SA
92 if (session_)
93 session_->remove_devices();
94}
95
2ad82c2e
UH
96void InputFile::start()
97{
5237f0c5
JH
98}
99
2ad82c2e
UH
100void InputFile::run()
101{
cda51567
SA
102 if (!f) {
103 // Previous call to run() processed the entire file already
6f925ba9 104 f = new ifstream(file_name_, ios::binary);
cda51567
SA
105 input_->reset();
106 }
5237f0c5 107
3b0acbcb
UH
108 vector<char> buffer(BufferSize);
109
5237f0c5 110 interrupt_ = false;
cda51567 111 while (!interrupt_ && !f->eof()) {
3b0acbcb 112 f->read(buffer.data(), BufferSize);
6f925ba9 113 const streamsize size = f->gcount();
5237f0c5
JH
114 if (size == 0)
115 break;
116
3b0acbcb 117 input_->send(buffer.data(), size);
5237f0c5 118
5237f0c5
JH
119 if (size != BufferSize)
120 break;
121 }
122
123 input_->end();
cda51567
SA
124
125 delete f;
126 f = nullptr;
5237f0c5
JH
127}
128
2ad82c2e
UH
129void InputFile::stop()
130{
5237f0c5
JH
131 interrupt_ = true;
132}
133
134} // namespace devices
135} // namespace pv