]> sigrok.org Git - pulseview.git/blob - pv/device/inputfile.cpp
Don't use Qt-defined keywords, they can cause problems with other headers.
[pulseview.git] / pv / device / inputfile.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <cassert>
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "inputfile.h"
28
29 #include <libsigrok/libsigrok.h>
30
31 using std::string;
32
33 namespace pv {
34 namespace device {
35
36 InputFile::InputFile(const std::string &path) :
37         File(path),
38         _input(NULL)
39 {
40 }
41
42 sr_dev_inst* InputFile::dev_inst() const
43 {
44         assert(_input);
45         return _input->sdi;
46 }
47
48 void InputFile::use(SigSession *owner) throw(QString)
49 {
50         assert(!_input);
51
52         _input = load_input_file_format(_path, NULL);
53         File::use(owner);
54
55         sr_session_new(&SigSession::_sr_session);
56
57         if (sr_session_dev_add(SigSession::_sr_session, _input->sdi) != SR_OK)
58                 throw tr("Failed to add session device.");
59 }
60
61 void InputFile::release()
62 {
63         if (!_owner)
64                 return;
65
66         assert(_input);
67         File::release();
68         sr_dev_close(_input->sdi);
69         sr_session_destroy(SigSession::_sr_session);
70         _input = NULL;
71 }
72
73 sr_input_format* InputFile::determine_input_file_format(
74         const string &filename)
75 {
76         int i;
77
78         /* If there are no input formats, return NULL right away. */
79         sr_input_format *const *const inputs = sr_input_list();
80         if (!inputs) {
81                 g_critical("No supported input formats available.");
82                 return NULL;
83         }
84
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()))
88                         break;
89         }
90
91         /* Return NULL if no input module wanted to touch this. */
92         if (!inputs[i]) {
93                 g_critical("Error: no matching input module found.");
94                 return NULL;
95         }
96
97         return inputs[i];
98 }
99
100 sr_input* InputFile::load_input_file_format(const string &filename,
101         sr_input_format *format)
102 {
103         struct stat st;
104         sr_input *in;
105
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");
110         }
111
112         if (stat(filename.c_str(), &st) == -1)
113                 throw tr("Failed to load file");
114
115         /* Initialize the input module. */
116         if (!(in = new sr_input)) {
117                 throw tr("Failed to allocate input module.");
118         }
119
120         in->format = format;
121         in->param = NULL;
122         if (in->format->init &&
123                 in->format->init(in, filename.c_str()) != SR_OK) {
124                 throw tr("Failed to load file");
125         }
126
127         return in;
128 }
129
130 void InputFile::start()
131 {
132 }
133
134 void InputFile::run()
135 {
136         assert(_input);
137         assert(_input->format);
138         assert(_input->format->loadfile);
139         _input->format->loadfile(_input, _path.c_str());
140 }
141
142 } // device
143 } // pv