Skip to content

Commit

Permalink
Fix and improve BitNestedArchiveReader::items()
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Oct 13, 2024
1 parent 14165be commit ac0849e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
9 changes: 9 additions & 0 deletions include/bit7z/bitarchiveitemoffset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class BitArchiveItemOffset final : public BitArchiveItem {
*/
BIT7Z_NODISCARD auto itemProperty( BitProperty property ) const -> BitPropVariant override;

/**
* @brief Checks whether the item has the specified property or not.
*
* @param property the property to be checked.
*
* @return true if the item has the property, false otherwise.
*/
BIT7Z_NODISCARD auto hasProperty( BitProperty property ) const -> bool;

private:
/* Note: a pointer, instead of a reference, allows this class, and hence BitInputArchive::ConstIterator,
* to be CopyConstructible so that stl algorithms can be used with ConstIterator. */
Expand Down
4 changes: 4 additions & 0 deletions src/bitarchiveitemoffset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ auto BitArchiveItemOffset::itemProperty( BitProperty property ) const -> BitProp
return mArc != nullptr ? mArc->itemProperty( mItemIndex, property ) : BitPropVariant();
}

auto BitArchiveItemOffset::hasProperty( BitProperty property ) const -> bool {
return mArc != nullptr ? mArc->itemHasProperty( mItemIndex, property ) : false;
}

} // namespace bit7z
18 changes: 4 additions & 14 deletions src/bitnestedarchivereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,23 +178,13 @@ auto BitNestedArchiveReader::items() const -> std::vector< BitArchiveItemInfo >
result.reserve( static_cast< std::size_t >( itemsCount ) );
}

for ( std::uint32_t index = 0; index < itemsCount; ++index ) {
if ( !mNestedArchive.itemHasProperty( index, BitProperty::IsDir ) ) {
mLastReadItem = index;
for ( const auto& item : mNestedArchive ) {
if ( !item.hasProperty( BitProperty::IsDir ) ) {
mLastReadItem = item.index();
return result;
}

BitArchiveItemInfo item( index );
for ( std::uint32_t j = kpidNoProperty; j <= kpidCopyLink; ++j ) {
// We cast property twice (here and in itemProperty), to make the code is easier to read.
const auto property = static_cast< BitProperty >( j );
const auto propertyValue = mNestedArchive.itemProperty( index, property );

if ( !propertyValue.isEmpty() ) {
item.setProperty( property, propertyValue );
}
}
result.emplace_back( std::move( item ) );
result.emplace_back( item );
}
mLastReadItem = std::numeric_limits< decltype( mLastReadItem ) >::max();
return result;
Expand Down

0 comments on commit ac0849e

Please sign in to comment.