X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fdevices%2Finputfile.cpp;h=c30bcd33db570d1b56ff219b94928b322dc57924;hp=22c9b1c8297e4e3d8d71eb73d925c122fc67c8f9;hb=cda515676ce6c2fa81e1cecacba3ea26ec2ee50e;hpb=c8e50b6efe18ff5ede06f0321d27d572dc3a6bc5 diff --git a/pv/devices/inputfile.cpp b/pv/devices/inputfile.cpp index 22c9b1c8..c30bcd33 100644 --- a/pv/devices/inputfile.cpp +++ b/pv/devices/inputfile.cpp @@ -36,19 +36,43 @@ InputFile::InputFile(const std::shared_ptr &context, const std::map &options) : File(file_name), context_(context), - input_(format->create_input(options)), + format_(format), + options_(options), interrupt_(false) { - if (!input_) - throw QString("Failed to create input"); } void InputFile::open() { if (session_) 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 std::ifstream(file_name_, std::ios::binary); + + char buffer[BufferSize]; + f->read(buffer, BufferSize); + const std::streamsize size = f->gcount(); + if (size == 0) + return; + + input_->send(buffer, size); + + try { + device_ = input_->device(); + } catch (sigrok::Error) { + return; + } - session_ = context_->create_session(); + session_->add_device(device_); } void InputFile::close() @@ -64,37 +88,30 @@ void InputFile::start() void InputFile::run() { char buffer[BufferSize]; - bool need_device = true; - assert(session_); - assert(input_); + if (!f) { + // Previous call to run() processed the entire file already + f = new std::ifstream(file_name_, std::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 std::streamsize size = f->gcount(); if (size == 0) break; input_->send(buffer, size); - if (need_device) { - try { - device_ = input_->device(); - } catch (sigrok::Error) { - break; - } - - session_->add_device(device_); - need_device = false; - } - if (size != BufferSize) break; } input_->end(); + + delete f; + f = nullptr; } void InputFile::stop()