diff options
| author | Wildex999 <wildex999@gmail.com> | 2020-11-03 01:35:31 +0100 |
|---|---|---|
| committer | Wildex999 <wildex999@gmail.com> | 2020-11-14 13:07:07 +0100 |
| commit | 56f9c3ac0c2486c00d031ac1664fec6322a2762e (patch) | |
| tree | 0e2b3b2605105c8dfcc52c48ca62fe96a057daa1 /src/KingSystem/Utils/Byaml/ByamlStringTableIter.cpp | |
| parent | c639c70c5b0340136b7625a08d08ed9b7a47f64f (diff) | |
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.
Diffstat (limited to 'src/KingSystem/Utils/Byaml/ByamlStringTableIter.cpp')
| -rw-r--r-- | src/KingSystem/Utils/Byaml/ByamlStringTableIter.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
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<const char*>(&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<const char*>(&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 |
