Skip to content

Commit 8e3a6cd

Browse files
committed
sarc: Add operator== (and !=) for oead::Sarc
And add AreFilesEqual too.
1 parent 95e7201 commit 8e3a6cd

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

py/py_sarc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ void BindSarc(py::module& m) {
2929
py::class_<Sarc::File> file_cl(m, "File");
3030

3131
cl.def(py::init<tcb::span<const u8>>(), "data"_a, py::keep_alive<1, 2>())
32+
.def(py::self == py::self)
33+
.def("are_files_equal", &Sarc::AreFilesEqual)
3234
.def("get_num_files", &Sarc::GetNumFiles)
3335
.def("get_data_offset", &Sarc::GetDataOffset)
3436
.def("get_endianness", &Sarc::GetEndianness)

src/include/oead/sarc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#pragma once
2121

22+
#include <absl/algorithm/container.h>
2223
#include <absl/container/btree_map.h>
2324
#include <absl/container/flat_hash_map.h>
2425
#include <easy_iterator.h>
@@ -41,6 +42,12 @@ class Sarc {
4142
std::string_view name;
4243
/// File data (as a view).
4344
tcb::span<const u8> data;
45+
46+
bool operator==(const File& other) const {
47+
return name == other.name && absl::c_equal(data, other.data);
48+
}
49+
50+
bool operator!=(const File& other) const { return !(*this == other); }
4451
};
4552

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

85+
/// Returns true if and only if the raw archive data is identical.
86+
bool operator==(const Sarc& other) const;
87+
bool operator!=(const Sarc& other) const { return !(*this == other); }
88+
89+
bool AreFilesEqual(const Sarc& other) const;
90+
7891
private:
7992
u16 m_num_files;
8093
u16 m_entries_offset;

src/sarc.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,21 @@ std::optional<Sarc::File> Sarc::GetFile(std::string_view name) const {
168168
return std::nullopt;
169169
}
170170

171+
bool Sarc::operator==(const Sarc& other) const {
172+
return absl::c_equal(m_reader.span(), other.m_reader.span());
173+
}
174+
175+
bool Sarc::AreFilesEqual(const Sarc& other) const {
176+
if (GetNumFiles() != other.GetNumFiles())
177+
return false;
178+
179+
for (const auto& [file1, file2] : easy_iterator::zip(GetFiles(), other.GetFiles())) {
180+
if (file1 != file2)
181+
return false;
182+
}
183+
return true;
184+
}
185+
171186
static constexpr bool IsValidAlignment(size_t alignment) {
172187
return alignment != 0 && (alignment & (alignment - 1)) == 0;
173188
}

0 commit comments

Comments
 (0)