From 56f9c3ac0c2486c00d031ac1664fec6322a2762e Mon Sep 17 00:00:00 2001 From: Wildex999 Date: Tue, 3 Nov 2020 01:35:31 +0100 Subject: ksys/utils/byaml: First iteration of implementation. 4 Functions are still not fully matching with their inlined functions, and at least one function is still missing. --- .../Utils/Byaml/ByamlStringTableIter.cpp | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/KingSystem/Utils/Byaml/ByamlStringTableIter.cpp (limited to 'src/KingSystem/Utils/Byaml/ByamlStringTableIter.cpp') diff --git a/src/KingSystem/Utils/Byaml/ByamlStringTableIter.cpp b/src/KingSystem/Utils/Byaml/ByamlStringTableIter.cpp new file mode 100644 index 00000000..f55e69c8 --- /dev/null +++ b/src/KingSystem/Utils/Byaml/ByamlStringTableIter.cpp @@ -0,0 +1,50 @@ +#include "KingSystem/Utils/Byaml/ByamlStringTableIter.h" +#include "KingSystem/Utils/Byaml/Byaml.h" +#include "KingSystem/Utils/Byaml/ByamlLocal.h" + +namespace al { + +ByamlStringTableIter::ByamlStringTableIter(const u8* data) { + mData = data; +} + +ByamlStringTableIter::ByamlStringTableIter(const ByamlStringTableIter& other) { + mData = other.mData; +} + +const char* ByamlStringTableIter::getString(s32 index) const { + const u32* string_offset_array = getStringOffsetArray(); + return reinterpret_cast(&mData[string_offset_array[index]]); +} + +s32 ByamlStringTableIter::findStringIndex(const char* string) const { + const u32* string_offset_array = getStringOffsetArray(); + u32 table_size = ByamlLocalUtil::getContainerSize(mData); + if (table_size == 0) { + return -1; + } + + // Binary Search + s32 start = 0; + s32 end = table_size; + s32 index; + while (true) { + if (start >= end) { + return -1; + } + + index = (start + end) / 2; + const char* str = reinterpret_cast(&mData[string_offset_array[index]]); + s32 result = std::strcmp(string, str); + if (result == 0) + break; + if (result > 0) + start = index + 1; + else if (result < 0) + end = index; + } + + return index; +} + +} // namespace al -- cgit v1.2.3