From d1e4812aa311780d06c1e5460c23009507c7f92e Mon Sep 17 00:00:00 2001 From: fallenoak Date: Fri, 10 Feb 2023 00:14:40 -0600 Subject: [PATCH] feat(datastore): add getter and setter for string --- common/datastore/CDataStore.cpp | 25 +++++++++++++++++++++++++ common/datastore/CDataStore.hpp | 2 ++ test/DataStore.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/common/datastore/CDataStore.cpp b/common/datastore/CDataStore.cpp index 8544ebe..6f944d4 100644 --- a/common/datastore/CDataStore.cpp +++ b/common/datastore/CDataStore.cpp @@ -3,6 +3,7 @@ #include #include #include +#include void CDataStore::DetachBuffer(void** data, uint32_t* size, uint32_t* alloc) { // TODO @@ -154,6 +155,25 @@ uint32_t CDataStore::GetHeaderSpace() { return 0; } +CDataStore& CDataStore::GetString(char* val, uint32_t maxChars) { + STORM_ASSERT(this->IsFinal()); + STORM_ASSERT(val || maxChars == 0); + + if (this->FetchRead(this->m_read, 1)) { + auto ofs = this->m_read - this->m_base; + auto ptr = &this->m_data[ofs]; + + uint32_t i; + for (i = 0; ptr[i] != '\0' && i < maxChars; i++) { + val[i] = *reinterpret_cast(&ptr[i]); + } + + this->m_read += i; + } + + return *this; +} + void CDataStore::InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc) { if (alloc && data) { SMemFree(data, __FILE__, __LINE__, 0); @@ -294,6 +314,11 @@ CDataStore& CDataStore::PutData(const void* val, uint32_t bytes) { return this->PutArray(static_cast(val), bytes); } +CDataStore& CDataStore::PutString(const char* val) { + auto len = SStrLen(val); + return this->PutArray(reinterpret_cast(val), len); +} + void CDataStore::Reset() { if (this->m_alloc == -1) { this->m_data = nullptr; diff --git a/common/datastore/CDataStore.hpp b/common/datastore/CDataStore.hpp index f8db388..3f616b8 100644 --- a/common/datastore/CDataStore.hpp +++ b/common/datastore/CDataStore.hpp @@ -35,6 +35,7 @@ class CDataStore { CDataStore& Get(uint64_t& val); CDataStore& Get(float& val); CDataStore& GetDataInSitu(void*& val, uint32_t bytes); + CDataStore& GetString(char* val, uint32_t maxChars); int32_t IsFinal(); CDataStore& Put(uint8_t val); CDataStore& Put(uint16_t val); @@ -43,6 +44,7 @@ class CDataStore { CDataStore& Put(float val); CDataStore& PutArray(const uint8_t* val, uint32_t count); CDataStore& PutData(const void* val, uint32_t bytes); + CDataStore& PutString(const char* val); CDataStore& Set(uint32_t pos, uint16_t val); bool Sub8CBBF0(uint32_t a2); }; diff --git a/test/DataStore.cpp b/test/DataStore.cpp index 8435c38..ecfa3cc 100644 --- a/test/DataStore.cpp +++ b/test/DataStore.cpp @@ -1,5 +1,6 @@ #include "common/DataStore.hpp" #include "test/Test.hpp" +#include TEST_CASE("CDataStore::CDataStore", "[datastore]") { SECTION("constructs new data store") { @@ -68,4 +69,28 @@ TEST_CASE("CDataStore::Get", "[datastore]") { REQUIRE(readVal == writeVal); } + + SECTION("gets string") { + const char* writeVal = "foobar"; + char readVal[7] = "barfoo"; + + CDataStore msg; + msg.PutString(writeVal); + msg.Finalize(); + msg.GetString(readVal, sizeof(readVal)); + + REQUIRE(SStrCmp(readVal, writeVal, STORM_MAX_STR) == 0); + } + + SECTION("gets string honoring maxchars") { + const char* writeVal = "foobar"; + char readVal[7] = "bar"; + + CDataStore msg; + msg.PutString(writeVal); + msg.Finalize(); + msg.GetString(readVal, 3); + + REQUIRE(SStrCmp(readVal, "foo", STORM_MAX_STR) == 0); + } }