]>
Commit | Line | Data |
---|---|---|
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 |
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; | |
3b0acbcb | 34 | using std::vector; |
6f925ba9 | 35 | |
5237f0c5 JH |
36 | namespace pv { |
37 | namespace devices { | |
38 | ||
6f925ba9 | 39 | const streamsize InputFile::BufferSize = 16384; |
5237f0c5 | 40 | |
6f925ba9 UH |
41 | InputFile::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) : | |
dd3fd4df | 45 | File(file_name), |
5237f0c5 | 46 | context_(context), |
519d0ccb SA |
47 | format_(format), |
48 | options_(options), | |
2ad82c2e UH |
49 | interrupt_(false) |
50 | { | |
5237f0c5 JH |
51 | } |
52 | ||
2ad82c2e UH |
53 | void InputFile::open() |
54 | { | |
4d6c6ea3 SA |
55 | if (session_) |
56 | close(); | |
519d0ccb SA |
57 | else |
58 | session_ = context_->create_session(); | |
cda51567 SA |
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 | |
6f925ba9 | 67 | f = new ifstream(file_name_, ios::binary); |
cda51567 | 68 | |
3b0acbcb UH |
69 | vector<char> buffer(BufferSize); |
70 | ||
71 | f->read(buffer.data(), BufferSize); | |
6f925ba9 | 72 | const streamsize size = f->gcount(); |
cda51567 SA |
73 | if (size == 0) |
74 | return; | |
75 | ||
3b0acbcb | 76 | input_->send(buffer.data(), size); |
cda51567 SA |
77 | |
78 | try { | |
79 | device_ = input_->device(); | |
30677c13 | 80 | } catch (sigrok::Error&) { |
cda51567 SA |
81 | return; |
82 | } | |
83 | ||
84 | session_->add_device(device_); | |
5237f0c5 JH |
85 | } |
86 | ||
2ad82c2e UH |
87 | void InputFile::close() |
88 | { | |
4d6c6ea3 SA |
89 | if (session_) |
90 | session_->remove_devices(); | |
91 | } | |
92 | ||
2ad82c2e UH |
93 | void InputFile::start() |
94 | { | |
5237f0c5 JH |
95 | } |
96 | ||
2ad82c2e UH |
97 | void InputFile::run() |
98 | { | |
cda51567 SA |
99 | if (!f) { |
100 | // Previous call to run() processed the entire file already | |
6f925ba9 | 101 | f = new ifstream(file_name_, ios::binary); |
cda51567 SA |
102 | input_->reset(); |
103 | } | |
5237f0c5 | 104 | |
3b0acbcb UH |
105 | vector<char> buffer(BufferSize); |
106 | ||
5237f0c5 | 107 | interrupt_ = false; |
cda51567 | 108 | while (!interrupt_ && !f->eof()) { |
3b0acbcb | 109 | f->read(buffer.data(), BufferSize); |
6f925ba9 | 110 | const streamsize size = f->gcount(); |
5237f0c5 JH |
111 | if (size == 0) |
112 | break; | |
113 | ||
3b0acbcb | 114 | input_->send(buffer.data(), size); |
5237f0c5 | 115 | |
5237f0c5 JH |
116 | if (size != BufferSize) |
117 | break; | |
118 | } | |
119 | ||
120 | input_->end(); | |
cda51567 SA |
121 | |
122 | delete f; | |
123 | f = nullptr; | |
5237f0c5 JH |
124 | } |
125 | ||
2ad82c2e UH |
126 | void InputFile::stop() |
127 | { | |
5237f0c5 JH |
128 | interrupt_ = true; |
129 | } | |
130 | ||
131 | } // namespace devices | |
132 | } // namespace pv |