]> sigrok.org Git - pulseview.git/blobdiff - pv/storesession.cpp
Minor whitespace cosmetics.
[pulseview.git] / pv / storesession.cpp
index ad106bb9f5882f79f72f965b2d2822ca7e7d958b..5500292a8df777563b624a5365a241bd7e93978b 100644 (file)
 
 #include <cassert>
 
+#ifdef _WIN32
+// Windows: Avoid boost/thread namespace pollution (which includes windows.h).
+#define NOGDI
+#define NORESOURCE
+#endif
+#include <boost/thread/locks.hpp>
+#include <boost/thread/shared_mutex.hpp>
+
 #include "storesession.hpp"
 
+#include <pv/devicemanager.hpp>
 #include <pv/session.hpp>
 #include <pv/data/logic.hpp>
 #include <pv/data/logicsegment.hpp>
+#include <pv/devices/device.hpp>
 #include <pv/view/signal.hpp>
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
@@ -34,8 +44,10 @@ using boost::shared_mutex;
 
 using std::deque;
 using std::dynamic_pointer_cast;
+using std::ios_base;
 using std::lock_guard;
 using std::make_pair;
+using std::map;
 using std::min;
 using std::mutex;
 using std::pair;
@@ -43,20 +55,29 @@ using std::set;
 using std::shared_ptr;
 using std::string;
 using std::thread;
+using std::unordered_set;
 using std::vector;
 
+using Glib::VariantBase;
+
 using sigrok::ConfigKey;
 using sigrok::Error;
 using sigrok::OutputFormat;
+using sigrok::OutputFlag;
 
 namespace pv {
 
 const size_t StoreSession::BlockSize = 1024 * 1024;
 
 StoreSession::StoreSession(const std::string &file_name,
-       const shared_ptr<OutputFormat> &output_format, const Session &session) :
+       const shared_ptr<OutputFormat> &output_format,
+       const map<string, VariantBase> &options,
+       const std::pair<uint64_t, uint64_t> sample_range,
+       const Session &session) :
        file_name_(file_name),
        output_format_(output_format),
+       options_(options),
+       sample_range_(sample_range),
        session_(session),
        interrupt_(false),
        units_stored_(0),
@@ -82,11 +103,14 @@ const QString& StoreSession::error() const
 
 bool StoreSession::start()
 {
-       set< shared_ptr<data::SignalData> > data_set =
-               session_.get_data();
+       const unordered_set< shared_ptr<view::Signal> > sigs(session_.signals());
+
+       // Add enabled channels to the data set
+       set< shared_ptr<data::SignalData> > data_set;
 
-       shared_lock<shared_mutex> lock(session_.signals_mutex());
-       const vector< shared_ptr<view::Signal> > &sigs(session_.signals());
+       for (shared_ptr<view::Signal> signal : sigs)
+               if (signal->enabled())
+                       data_set.insert(signal->data());
 
        // Check we have logic data
        if (data_set.empty() || sigs.empty()) {
@@ -104,7 +128,7 @@ bool StoreSession::start()
        shared_ptr<data::Logic> data;
        if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
                error_ = tr("PulseView currently only has support for "
-                       "storing logic data.");
+                       "storing logic data.");
                return false;
        }
 
@@ -120,13 +144,27 @@ bool StoreSession::start()
        const shared_ptr<data::LogicSegment> segment(segments.front());
        assert(segment);
 
+       // Check whether the user wants to export a certain sample range
+       if (sample_range_.first == sample_range_.second) {
+               start_sample_ = 0;
+               sample_count_ = segment->get_sample_count();
+       } else {
+               start_sample_ = std::min(sample_range_.first, sample_range_.second);
+               sample_count_ = std::abs(sample_range_.second - sample_range_.first);
+       }
+
        // Begin storing
        try {
-               auto context = session_.session()->context();
-               auto device = session_.device();
-               output_ = output_format_->create_output(device,
-                       {{"filename",
-                               Glib::Variant<Glib::ustring>::create(file_name_)}});
+               const auto context = session_.device_manager().context();
+               auto device = session_.device()->device();
+
+               map<string, Glib::VariantBase> options = options_;
+
+               if (!output_format_->test_flag(OutputFlag::INTERNAL_IO_HANDLING))
+                       output_stream_.open(file_name_, ios_base::binary |
+                                       ios_base::trunc | ios_base::out);
+
+               output_ = output_format_->create_output(file_name_, device, options);
                auto meta = context->create_meta_packet(
                        {{ConfigKey::SAMPLERATE, Glib::Variant<guint64>::create(
                                segment->samplerate())}});
@@ -155,7 +193,6 @@ void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
 {
        assert(segment);
 
-       uint64_t start_sample = 0, sample_count;
        unsigned progress_scale = 0;
 
        /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
@@ -165,38 +202,40 @@ void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
        const int unit_size = segment->unit_size();
        assert(unit_size != 0);
 
-       sample_count = segment->get_sample_count();
-
-       // Qt needs the progress values to fit inside an int.  If they would
+       // Qt needs the progress values to fit inside an int. If they would
        // not, scale the current and max values down until they do.
-       while ((sample_count >> progress_scale) > INT_MAX)
+       while ((sample_count_ >> progress_scale) > INT_MAX)
                progress_scale ++;
 
-       unit_count_ = sample_count >> progress_scale;
+       unit_count_ = sample_count_ >> progress_scale;
 
        const unsigned int samples_per_block = BlockSize / unit_size;
 
-       while (!interrupt_ && start_sample < sample_count)
+       while (!interrupt_ && sample_count_)
        {
                progress_updated();
 
-               const uint64_t end_sample = min(
-                       start_sample + samples_per_block, sample_count);
-               segment->get_samples(data, start_sample, end_sample);
+               const uint64_t packet_len =
+                       std::min((uint64_t)samples_per_block, sample_count_);
+
+               segment->get_samples(data, start_sample_, start_sample_ + packet_len);
 
-               size_t length = end_sample - start_sample;
+               size_t length = packet_len * unit_size;
 
                try {
-                       auto context = session_.session()->context();
+                       const auto context = session_.device_manager().context();
                        auto logic = context->create_logic_packet(data, length, unit_size);
-                       output_->receive(logic);
+                       const string data = output_->receive(logic);
+                       if (output_stream_.is_open())
+                               output_stream_ << data;
                } catch (Error error) {
                        error_ = tr("Error while saving.");
                        break;
                }
 
-               start_sample = end_sample;
-               units_stored_ = start_sample >> progress_scale;
+               sample_count_ -= packet_len;
+               start_sample_ += packet_len;
+               units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
        }
 
        // Zeroing the progress variables indicates completion
@@ -205,6 +244,7 @@ void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
        progress_updated();
 
        output_.reset();
+       output_stream_.close();
 
        delete[] data;
 }