2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sys/types.h>
27 #include "inputfile.h"
29 #include <libsigrok/libsigrok.h>
36 InputFile::InputFile(const std::string &path) :
42 sr_dev_inst* InputFile::dev_inst() const
48 void InputFile::use(SigSession *owner) throw(QString)
52 _input = load_input_file_format(_path, NULL);
55 sr_session_new(&SigSession::_sr_session);
57 if (sr_session_dev_add(SigSession::_sr_session, _input->sdi) != SR_OK)
58 throw tr("Failed to add session device.");
61 void InputFile::release()
68 sr_dev_close(_input->sdi);
69 sr_session_destroy(SigSession::_sr_session);
73 sr_input_format* InputFile::determine_input_file_format(
74 const string &filename)
78 /* If there are no input formats, return NULL right away. */
79 sr_input_format *const *const inputs = sr_input_list();
81 g_critical("No supported input formats available.");
85 /* Otherwise, try to find an input module that can handle this file. */
86 for (i = 0; inputs[i]; i++) {
87 if (inputs[i]->format_match(filename.c_str()))
91 /* Return NULL if no input module wanted to touch this. */
93 g_critical("Error: no matching input module found.");
100 sr_input* InputFile::load_input_file_format(const string &filename,
101 sr_input_format *format)
106 if (!format && !(format =
107 determine_input_file_format(filename.c_str()))) {
108 /* The exact cause was already logged. */
109 throw tr("Failed to load file");
112 if (stat(filename.c_str(), &st) == -1)
113 throw tr("Failed to load file");
115 /* Initialize the input module. */
116 if (!(in = new sr_input)) {
117 throw tr("Failed to allocate input module.");
122 if (in->format->init &&
123 in->format->init(in, filename.c_str()) != SR_OK) {
124 throw tr("Failed to load file");
130 void InputFile::start()
134 void InputFile::run()
137 assert(_input->format);
138 assert(_input->format->loadfile);
139 _input->format->loadfile(_input, _path.c_str());