X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=pv%2Fdevices%2Finputfile.cpp;h=4ffd2dfa22cbc702d6ad7dfeb476042ec2d73639;hb=1f419596224bd8f73fc8c59b52b8775685249a07;hp=22c9b1c8297e4e3d8d71eb73d925c122fc67c8f9;hpb=c8e50b6efe18ff5ede06f0321d27d572dc3a6bc5;p=pulseview.git diff --git a/pv/devices/inputfile.cpp b/pv/devices/inputfile.cpp index 22c9b1c..4ffd2df 100644 --- a/pv/devices/inputfile.cpp +++ b/pv/devices/inputfile.cpp @@ -14,41 +14,77 @@ * 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 #include +#include #include #include "inputfile.hpp" +using std::map; +using std::shared_ptr; +using std::streamsize; +using std::string; +using std::ifstream; +using std::ios; +using std::vector; + namespace pv { namespace devices { -const std::streamsize InputFile::BufferSize = 16384; +// Use a 4MB chunk size for reading a file into memory. Larger values don't +// seem to provide any substancial performance improvements, but can cause +// UI lag and a visually "stuttering" display of the data currently loading. +const streamsize InputFile::BufferSize = (4 * 1024 * 1024); -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), - 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 ifstream(file_name_, ios::binary); + + vector buffer(BufferSize); - session_ = context_->create_session(); + f->read(buffer.data(), BufferSize); + const streamsize size = f->gcount(); + if (size == 0) + return; + + input_->send(buffer.data(), size); + + try { + device_ = input_->device(); + } catch (sigrok::Error&) { + return; + } + + session_->add_device(device_); } void InputFile::close() @@ -63,38 +99,31 @@ void InputFile::start() void InputFile::run() { - char buffer[BufferSize]; - bool need_device = true; + if (!f) { + // Previous call to run() processed the entire file already + f = new ifstream(file_name_, ios::binary); + input_->reset(); + } - assert(session_); - assert(input_); + vector buffer(BufferSize); 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.data(), 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_->add_device(device_); - need_device = false; - } + input_->send(buffer.data(), size); if (size != BufferSize) break; } input_->end(); + + delete f; + f = nullptr; } void InputFile::stop()