mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 03:02:29 +00:00
feat(datastore): add getter and setter for string
This commit is contained in:
parent
a4dfbeff7a
commit
d1e4812aa3
3 changed files with 52 additions and 0 deletions
|
|
@ -3,6 +3,7 @@
|
|||
#include <cstring>
|
||||
#include <storm/Error.hpp>
|
||||
#include <storm/Memory.hpp>
|
||||
#include <storm/String.hpp>
|
||||
|
||||
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<char*>(&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<const uint8_t*>(val), bytes);
|
||||
}
|
||||
|
||||
CDataStore& CDataStore::PutString(const char* val) {
|
||||
auto len = SStrLen(val);
|
||||
return this->PutArray(reinterpret_cast<const uint8_t*>(val), len);
|
||||
}
|
||||
|
||||
void CDataStore::Reset() {
|
||||
if (this->m_alloc == -1) {
|
||||
this->m_data = nullptr;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "common/DataStore.hpp"
|
||||
#include "test/Test.hpp"
|
||||
#include <storm/String.hpp>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue