| 98 | } |
| 99 | |
| 100 | void SampleQueue::CleanQueue(std::unique_lock<std::mutex>& lock) { |
| 101 | const auto* dg3 = dynamic_cast<detail::Dg3Block*>(&data_group_); |
| 102 | if (dg3 == nullptr ) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | lock.unlock(); |
| 107 | |
| 108 | writer_.Open(std::ios_base::in | std::ios_base::out | std::ios_base::binary); |
| 109 | if (!writer_.IsOpen()) { |
| 110 | lock.lock(); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | SetLastPosition(*writer_.file_); |
| 115 | |
| 116 | lock.lock(); |
| 117 | |
| 118 | // Trim the queue so the start time is included in the first sample |
| 119 | TrimQueue(); |
| 120 | const auto stop_time = writer_.StopTime(); |
| 121 | // Save the queue onto the file |
| 122 | while (!IsEmpty()) { |
| 123 | // Write a sample last to file |
| 124 | auto sample = GetSample(); |
| 125 | if (stop_time > 0 && sample.timestamp > stop_time) { |
| 126 | break; // Skip this sample |
| 127 | } |
| 128 | lock.unlock(); |
| 129 | |
| 130 | if (dg3->NofRecordId() > 0) { |
| 131 | const auto id = static_cast<uint8_t>(sample.record_id); |
| 132 | writer_.file_->sputc(static_cast<char>(id)); |
| 133 | } |
| 134 | writer_.file_->sputn(reinterpret_cast<const char*>(sample.record_buffer.data()), |
| 135 | static_cast<std::streamsize>(sample.record_buffer.size()) ); |
| 136 | if (dg3->NofRecordId() > 1) { |
| 137 | const auto id = static_cast<uint8_t>(sample.record_id); |
| 138 | writer_.file_->sputc(static_cast<char>(id)); |
| 139 | } |
| 140 | IncrementNofSamples(sample.record_id); |
| 141 | lock.lock(); |
| 142 | } |
| 143 | |
| 144 | // Update channel group headers to reflect the new number of samples |
| 145 | lock.unlock(); |
| 146 | for (const auto& cg3 : dg3->Cg3()) { |
| 147 | if (cg3 != nullptr) { |
| 148 | cg3->Write(*writer_.file_); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | writer_.Close(); |
| 153 | lock.lock(); |
| 154 | |
| 155 | } |
| 156 | |
| 157 | void SampleQueue::IncrementNofSamples(uint64_t record_id) const { |
no test coverage detected