Simlify code, avoid try-catch, remove disabled code

This commit is contained in:
Martchus 2021-01-27 20:23:59 +01:00
parent 1499b71b37
commit b39e9dc475
1 changed files with 27 additions and 32 deletions

View File

@ -171,11 +171,11 @@ void MatroskaCuePositionUpdater::parse(EbmlElement *cuesElement, Diagnostics &di
*/ */
bool MatroskaCuePositionUpdater::updateOffsets(std::uint64_t originalOffset, std::uint64_t newOffset) bool MatroskaCuePositionUpdater::updateOffsets(std::uint64_t originalOffset, std::uint64_t newOffset)
{ {
bool updated = false; auto updated = false;
const auto newOffsetLength = static_cast<int>(EbmlElement::calculateUIntegerLength(newOffset));
for (auto &offset : m_offsets) { for (auto &offset : m_offsets) {
if (offset.second.initialValue() == originalOffset && offset.second.currentValue() != newOffset) { if (offset.second.initialValue() == originalOffset && offset.second.currentValue() != newOffset) {
updated = updateSize(offset.first->parent(), updated = updateSize(offset.first->parent(), newOffsetLength
static_cast<int>(EbmlElement::calculateUIntegerLength(newOffset))
- static_cast<int>(EbmlElement::calculateUIntegerLength(offset.second.currentValue()))) - static_cast<int>(EbmlElement::calculateUIntegerLength(offset.second.currentValue())))
|| updated; || updated;
offset.second.update(newOffset); offset.second.update(newOffset);
@ -191,12 +191,12 @@ bool MatroskaCuePositionUpdater::updateOffsets(std::uint64_t originalOffset, std
bool MatroskaCuePositionUpdater::updateRelativeOffsets( bool MatroskaCuePositionUpdater::updateRelativeOffsets(
std::uint64_t referenceOffset, std::uint64_t originalRelativeOffset, std::uint64_t newRelativeOffset) std::uint64_t referenceOffset, std::uint64_t originalRelativeOffset, std::uint64_t newRelativeOffset)
{ {
bool updated = false; auto updated = false;
const auto newRelativeOffsetLength = static_cast<int>(EbmlElement::calculateUIntegerLength(newRelativeOffset));
for (auto &offset : m_relativeOffsets) { for (auto &offset : m_relativeOffsets) {
if (offset.second.referenceOffset() == referenceOffset && offset.second.initialValue() == originalRelativeOffset if (offset.second.referenceOffset() == referenceOffset && offset.second.initialValue() == originalRelativeOffset
&& offset.second.currentValue() != newRelativeOffset) { && offset.second.currentValue() != newRelativeOffset) {
updated = updateSize(offset.first->parent(), updated = updateSize(offset.first->parent(), newRelativeOffsetLength
static_cast<int>(EbmlElement::calculateUIntegerLength(newRelativeOffset))
- static_cast<int>(EbmlElement::calculateUIntegerLength(offset.second.currentValue()))) - static_cast<int>(EbmlElement::calculateUIntegerLength(offset.second.currentValue())))
|| updated; || updated;
offset.second.update(newRelativeOffset); offset.second.update(newRelativeOffset);
@ -212,34 +212,32 @@ bool MatroskaCuePositionUpdater::updateRelativeOffsets(
bool MatroskaCuePositionUpdater::updateSize(EbmlElement *element, int shift) bool MatroskaCuePositionUpdater::updateSize(EbmlElement *element, int shift)
{ {
if (!shift) { if (!shift) {
// shift is gone return false; // shift is gone
return false;
} }
if (!element) { if (!element) {
// there was no parent (shouldn't happen in a normal file structure since the Segment element should // there was no parent (shouldn't happen in a normal file structure since the Segment element should
// be parent of the Cues element) // be parent of the Cues element)
return shift; return shift;
} }
try { // get size info
// get size info const auto sizeIterator = m_sizes.find(element);
std::uint64_t &size = m_sizes.at(element); if (sizeIterator == m_sizes.end()) {
// calculate new size return shift; // the element is out of the scope of the cue position updater (likely the Segment element)
const std::uint64_t newSize = shift > 0 ? size + static_cast<std::uint64_t>(shift) : size - static_cast<std::uint64_t>(-shift);
// shift parent
const bool updated = updateSize(element->parent(),
shift + static_cast<int>(EbmlElement::calculateSizeDenotationLength(newSize))
- static_cast<int>(EbmlElement::calculateSizeDenotationLength(size)));
// apply new size
size = newSize;
return updated;
} catch (const out_of_range &) {
// the element is out of the scope of the cue position updater (likely the Segment element)
return shift;
} }
std::uint64_t &size = sizeIterator->second;
// calculate new size
const std::uint64_t newSize = shift > 0 ? size + static_cast<std::uint64_t>(shift) : size - static_cast<std::uint64_t>(-shift);
// shift parent
const bool updated = updateSize(element->parent(),
shift + static_cast<int>(EbmlElement::calculateSizeDenotationLength(newSize))
- static_cast<int>(EbmlElement::calculateSizeDenotationLength(size)));
// apply new size
size = newSize;
return updated;
} }
/*! /*!
* \brief Writes the previously parsed "Cues"-element with updates positions to the specified \a stream. * \brief Writes the previously parsed "Cues"-element with updated positions to the specified \a stream.
*/ */
void MatroskaCuePositionUpdater::make(ostream &stream, Diagnostics &diag) void MatroskaCuePositionUpdater::make(ostream &stream, Diagnostics &diag)
{ {
@ -279,7 +277,6 @@ void MatroskaCuePositionUpdater::make(ostream &stream, Diagnostics &diag)
// write "CueTime"-element // write "CueTime"-element
cuePointChild->copyBuffer(stream); cuePointChild->copyBuffer(stream);
cuePointChild->discardBuffer(); cuePointChild->discardBuffer();
//cuePointChild->copyEntirely(stream);
break; break;
case MatroskaIds::CueTrackPositions: case MatroskaIds::CueTrackPositions:
// write "CueTrackPositions"-element // write "CueTrackPositions"-element
@ -296,16 +293,14 @@ void MatroskaCuePositionUpdater::make(ostream &stream, Diagnostics &diag)
// write unchanged children of "CueTrackPositions"-element // write unchanged children of "CueTrackPositions"-element
cueTrackPositionsChild->copyBuffer(stream); cueTrackPositionsChild->copyBuffer(stream);
cueTrackPositionsChild->discardBuffer(); cueTrackPositionsChild->discardBuffer();
//cueTrackPositionsChild->copyEntirely(stream);
break; break;
case MatroskaIds::CueRelativePosition: case MatroskaIds::CueRelativePosition:
try { if (const auto relativeOffset = m_relativeOffsets.find(cueTrackPositionsChild);
EbmlElement::makeSimpleElement( relativeOffset != m_relativeOffsets.end()) {
stream, cueTrackPositionsChild->id(), m_relativeOffsets.at(cueTrackPositionsChild).currentValue()); EbmlElement::makeSimpleElement(stream, cueTrackPositionsChild->id(), relativeOffset->second.currentValue());
} catch (const out_of_range &) {
// we were not able parse the relative offset because the absolute offset is missing
// continue anyways
} }
// we were not able parse the relative offset because the absolute offset is missing
// continue anyways
break; break;
case MatroskaIds::CueClusterPosition: case MatroskaIds::CueClusterPosition:
case MatroskaIds::CueCodecState: case MatroskaIds::CueCodecState: