]> sigrok.org Git - pulseview.git/blobdiff - pv/devices/inputfile.cpp
inputfile: Use a vector instead of an array on the stack.
[pulseview.git] / pv / devices / inputfile.cpp
index e92bf208bf17989039bed7b4dbbbbe277f49093f..1bf34bad22a33bc0307199657bfe5ecf8dc9ef3d 100644 (file)
  * 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 <http://www.gnu.org/licenses/>.
  */
 
 #include <cassert>
 #include <fstream>
+#include <vector>
 
 #include <QString>
 
 #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;
+const streamsize InputFile::BufferSize = 16384;
 
-InputFile::InputFile(const std::shared_ptr<sigrok::Context> &context,
-       const std::string &file_name,
-       std::shared_ptr<sigrok::InputFormat> format,
-       const std::map<std::string, Glib::VariantBase> &options) :
+InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
+       const string &file_name,
+       shared_ptr<sigrok::InputFormat> format,
+       const map<string, Glib::VariantBase> &options) :
        File(file_name),
        context_(context),
-       input_(format->create_input(options)),
-       interrupt_(false) {
+       format_(format),
+       options_(options),
+       interrupt_(false)
+{
+}
+
+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<char> buffer(BufferSize);
+
+       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::create() {
-       session_ = context_->create_session();
+void InputFile::close()
+{
+       if (session_)
+               session_->remove_devices();
 }
 
-void InputFile::start() {
+void InputFile::start()
+{
 }
 
-void InputFile::run() {
-       char buffer[BufferSize];
-       bool need_device = true;
+void InputFile::run()
+{
+       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<char> buffer(BufferSize);
 
        interrupt_ = false;
-       std::ifstream f(file_name_);
-       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() {
+void InputFile::stop()
+{
        interrupt_ = true;
 }