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