Skip to content

Commit

Permalink
sarc: Add operator== (and !=) for oead::Sarc
Browse files Browse the repository at this point in the history
And add AreFilesEqual too.
  • Loading branch information
leoetlino committed Jul 13, 2020
1 parent 95e7201 commit 8e3a6cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions py/py_sarc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void BindSarc(py::module& m) {
py::class_<Sarc::File> file_cl(m, "File");

cl.def(py::init<tcb::span<const u8>>(), "data"_a, py::keep_alive<1, 2>())
.def(py::self == py::self)
.def("are_files_equal", &Sarc::AreFilesEqual)
.def("get_num_files", &Sarc::GetNumFiles)
.def("get_data_offset", &Sarc::GetDataOffset)
.def("get_endianness", &Sarc::GetEndianness)
Expand Down
13 changes: 13 additions & 0 deletions src/include/oead/sarc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#pragma once

#include <absl/algorithm/container.h>
#include <absl/container/btree_map.h>
#include <absl/container/flat_hash_map.h>
#include <easy_iterator.h>
Expand All @@ -41,6 +42,12 @@ class Sarc {
std::string_view name;
/// File data (as a view).
tcb::span<const u8> data;

bool operator==(const File& other) const {
return name == other.name && absl::c_equal(data, other.data);
}

bool operator!=(const File& other) const { return !(*this == other); }
};

/// File iterator.
Expand Down Expand Up @@ -75,6 +82,12 @@ class Sarc {
/// Guess the minimum data alignment for files that are stored in the archive.
size_t GuessMinAlignment() const;

/// Returns true if and only if the raw archive data is identical.
bool operator==(const Sarc& other) const;
bool operator!=(const Sarc& other) const { return !(*this == other); }

bool AreFilesEqual(const Sarc& other) const;

private:
u16 m_num_files;
u16 m_entries_offset;
Expand Down
15 changes: 15 additions & 0 deletions src/sarc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ std::optional<Sarc::File> Sarc::GetFile(std::string_view name) const {
return std::nullopt;
}

bool Sarc::operator==(const Sarc& other) const {
return absl::c_equal(m_reader.span(), other.m_reader.span());
}

bool Sarc::AreFilesEqual(const Sarc& other) const {
if (GetNumFiles() != other.GetNumFiles())
return false;

for (const auto& [file1, file2] : easy_iterator::zip(GetFiles(), other.GetFiles())) {
if (file1 != file2)
return false;
}
return true;
}

static constexpr bool IsValidAlignment(size_t alignment) {
return alignment != 0 && (alignment & (alignment - 1)) == 0;
}
Expand Down

0 comments on commit 8e3a6cd

Please sign in to comment.