X-Git-Url: http://git.code-monkey.de/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fdevices%2Finputfile.cpp;fp=pv%2Fdevices%2Finputfile.cpp;h=411877473b9bf30e235b990243c574f7418c1a61;hp=0000000000000000000000000000000000000000;hb=5237f0c50352b523c6a0c3d7f931081ecdbdecaa;hpb=e6c1382ebf0994e55e9470724df985a1b7750709 diff --git a/pv/devices/inputfile.cpp b/pv/devices/inputfile.cpp new file mode 100644 index 0000000..4118774 --- /dev/null +++ b/pv/devices/inputfile.cpp @@ -0,0 +1,92 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2015 Joel Holdsworth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 + */ + +#include +#include + +#include + +#include "inputfile.hpp" + +namespace pv { +namespace devices { + +const std::streamsize InputFile::BufferSize = 16384; + +InputFile::InputFile(const std::shared_ptr &context, + const std::string &file_name, + std::shared_ptr format, + const std::map &options) : + context_(context), + input_(format->create_input(options)), + file_name_(file_name), + interrupt_(false) { + if (!input_) + throw QString("Failed to create input"); +} + +void InputFile::create() { + session_ = context_->create_session(); +} + +void InputFile::start() { +} + +void InputFile::run() { + char buffer[BufferSize]; + bool need_device = true; + + assert(session_); + assert(input_); + + interrupt_ = false; + std::ifstream f(file_name_); + while (!interrupt_ && f) { + 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(); +} + +void InputFile::stop() { + interrupt_ = true; +} + +} // namespace devices +} // namespace pv