X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fdevices%2Finputfile.cpp;h=58c60d700bbd25375222b20f09c7f35a41097e3a;hp=935b0c47bdf6e41714bf889a530af61513ccb420;hb=2601ce965fc455d23f5a964b84ddd74eb93705c4;hpb=519d0ccbe67d005a9c442795ce3b8255e78ca46d diff --git a/pv/devices/inputfile.cpp b/pv/devices/inputfile.cpp index 935b0c47..58c60d70 100644 --- a/pv/devices/inputfile.cpp +++ b/pv/devices/inputfile.cpp @@ -14,8 +14,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * along with this program; if not, see . */ #include @@ -25,15 +24,22 @@ #include "inputfile.hpp" +using std::map; +using std::shared_ptr; +using std::streamsize; +using std::string; +using std::ifstream; +using std::ios; + namespace pv { namespace devices { -const std::streamsize InputFile::BufferSize = 16384; +const streamsize InputFile::BufferSize = 16384; -InputFile::InputFile(const std::shared_ptr &context, - const std::string &file_name, - std::shared_ptr format, - const std::map &options) : +InputFile::InputFile(const shared_ptr &context, + const string &file_name, + shared_ptr format, + const map &options) : File(file_name), context_(context), format_(format), @@ -48,6 +54,31 @@ void InputFile::open() close(); else session_ = context_->create_session(); + + input_ = format_->create_input(options_); + + if (!input_) + throw QString("Failed to create input"); + + // open() should add the input device to the session but + // we can't open the device without sending some data first + f = new ifstream(file_name_, ios::binary); + + char buffer[BufferSize]; + f->read(buffer, BufferSize); + const streamsize size = f->gcount(); + if (size == 0) + return; + + input_->send(buffer, size); + + try { + device_ = input_->device(); + } catch (sigrok::Error) { + return; + } + + session_->add_device(device_); } void InputFile::close() @@ -63,42 +94,30 @@ void InputFile::start() void InputFile::run() { char buffer[BufferSize]; - bool need_device = true; - - assert(session_); - input_ = format_->create_input(options_); - - if (!input_) - throw QString("Failed to create input"); + if (!f) { + // Previous call to run() processed the entire file already + f = new ifstream(file_name_, ios::binary); + input_->reset(); + } interrupt_ = false; - std::ifstream f(file_name_, std::ios::binary); - while (!interrupt_ && f) { - f.read(buffer, BufferSize); - const std::streamsize size = f.gcount(); + while (!interrupt_ && !f->eof()) { + f->read(buffer, BufferSize); + const streamsize size = f->gcount(); if (size == 0) break; input_->send(buffer, size); - if (need_device) { - try { - device_ = input_->device(); - } catch (sigrok::Error) { - break; - } - - session_->remove_devices(); // Remove instance from previous run - session_->add_device(device_); - need_device = false; - } - if (size != BufferSize) break; } input_->end(); + + delete f; + f = nullptr; } void InputFile::stop()