Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Entry::getRedirectEntryIndex() #716

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion include/zim/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ namespace zim
/** Get the Entry targeted by the entry.
*
* @return The entry directly targeted by this redirect entry.
* @exception InvalidEntry in the entry is not a redirection.
* @exception InvalidEntry if the entry is not a redirection.
*/
Entry getRedirectEntry() const;

/** Get the index of the Entry targeted by the entry.
*
* @return The index of the entry directly targeted by this redirect
* entry.
* @exception InvalidEntry if the entry is not a redirection.
*/
entry_index_type getRedirectEntryIndex() const;

entry_index_type getIndex() const { return m_idx; }

private:
Expand Down
8 changes: 6 additions & 2 deletions src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ Item Entry::getRedirect() const {
return nextEntry.getItem(false);
}

Entry Entry::getRedirectEntry() const {
entry_index_type Entry::getRedirectEntryIndex() const {
if (!isRedirect()) {
std::ostringstream sstream;
sstream << "Entry " << getPath() << " is not a redirect entry.";
throw InvalidType(sstream.str());
}
return Entry(m_file, static_cast<entry_index_type>(m_dirent->getRedirectIndex()));
return m_dirent->getRedirectIndex().v;
}

Entry Entry::getRedirectEntry() const {
return Entry(m_file, getRedirectEntryIndex());
}
4 changes: 4 additions & 0 deletions test/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ TEST(ZimArchive, openCreatedArchive)
ASSERT_EQ(foo.getPath(), "foo");
ASSERT_EQ(foo.getTitle(), "Foo");
ASSERT_EQ(std::string(foo.getItem().getData()), "FooContent");
ASSERT_THROW(foo.getRedirectEntry(), zim::InvalidType);
ASSERT_THROW(foo.getRedirectEntryIndex(), zim::InvalidType);

auto foo2 = archive.getEntryByPath("foo2");
ASSERT_EQ(foo2.getPath(), "foo2");
Expand All @@ -215,10 +217,12 @@ TEST(ZimArchive, openCreatedArchive)
ASSERT_EQ(foo3.getTitle(), "FooRedirection");
ASSERT_TRUE(foo3.isRedirect());
ASSERT_EQ(foo3.getRedirectEntry().getIndex(), foo.getIndex());
ASSERT_EQ(foo3.getRedirectEntryIndex(), foo.getIndex());

auto main = archive.getMainEntry();
ASSERT_TRUE(main.isRedirect());
ASSERT_EQ(main.getRedirectEntry().getIndex(), foo.getIndex());
ASSERT_EQ(main.getRedirectEntryIndex(), foo.getIndex());
ASSERT_EQ(archive.getMainEntryIndex(), main.getIndex());
}

Expand Down