scsl/Dictionary.cc

152 lines
2.7 KiB
C++
Raw Normal View History

2023-10-06 03:13:46 +00:00
#include <cstring>
#include <cstdlib>
2023-10-06 06:17:09 +00:00
#include "Dictionary.h"
2023-10-06 03:13:46 +00:00
2023-10-06 06:08:35 +00:00
#if defined(DESKTOP_BUILD)
#include <iostream>
#endif
2023-10-06 03:13:46 +00:00
bool
2023-10-06 06:17:09 +00:00
Dictionary::Lookup(const char *key, uint8_t klen, TLV::Record &res)
2023-10-06 03:13:46 +00:00
{
2023-10-06 06:08:35 +00:00
res.Tag = this->kTag;
uint8_t *cursor = TLV::FindTag(this->arena, NULL, res);
2023-10-06 03:13:46 +00:00
while (cursor != NULL) {
if ((klen == res.Len) &&
(memcmp(res.Val, key, klen) == 0)) {
2023-10-06 06:08:35 +00:00
TLV::ReadFromMemory(res, cursor);
if (res.Tag != this->vTag) {
2023-10-06 03:13:46 +00:00
abort();
}
return true;
}
2023-10-06 06:08:35 +00:00
cursor = TLV::FindTag(this->arena, cursor, res);
2023-10-06 03:13:46 +00:00
}
return false;
}
int
2023-10-06 06:17:09 +00:00
Dictionary::Set(const char *key, uint8_t klen, const char *val, uint8_t vlen)
2023-10-06 03:13:46 +00:00
{
TLV::Record rec;
uint8_t *cursor = NULL;
2023-10-06 06:08:35 +00:00
SetRecord(rec, this->kTag, klen, key);
2023-10-06 03:13:46 +00:00
cursor = this->seek(key, klen);
if (cursor != NULL) {
2023-10-06 06:08:35 +00:00
TLV::DeleteRecord(this->arena, cursor);
TLV::DeleteRecord(this->arena, cursor);
2023-10-06 03:13:46 +00:00
}
2023-10-06 06:08:35 +00:00
if (!spaceAvailable(klen, vlen)) {
2023-10-06 04:18:21 +00:00
return -1;
}
2023-10-06 06:08:35 +00:00
cursor = TLV::WriteToMemory(this->arena, NULL, rec);
2023-10-06 03:13:46 +00:00
if (cursor == NULL) {
return -1;
}
2023-10-06 06:08:35 +00:00
SetRecord(rec, this->vTag, vlen, val);
if (TLV::WriteToMemory(this->arena, NULL, rec) == NULL) {
2023-10-06 03:13:46 +00:00
return -1;
}
return 0;
}
uint8_t *
2023-10-06 06:17:09 +00:00
Dictionary::seek(const char *key, uint8_t klen)
2023-10-06 03:13:46 +00:00
{
TLV::Record rec;
2023-10-06 06:08:35 +00:00
rec.Tag = this->kTag;
uint8_t *cursor = TLV::LocateTag(this->arena, NULL, rec);
2023-10-06 03:13:46 +00:00
while (cursor != NULL) {
2023-10-06 06:08:35 +00:00
if ((klen == rec.Len) && (this->kTag == rec.Tag)) {
if (memcmp(rec.Val, key, klen) == 0) {
return cursor;
}
2023-10-06 03:13:46 +00:00
}
2023-10-06 06:08:35 +00:00
cursor = TLV::SkipRecord(rec, cursor);
cursor = TLV::LocateTag(this->arena, cursor, rec);
2023-10-06 03:13:46 +00:00
}
return NULL;
}
bool
2023-10-06 06:17:09 +00:00
Dictionary::Has(const char *key, uint8_t klen)
2023-10-06 03:13:46 +00:00
{
return this->seek(key, klen) != NULL;
}
2023-10-06 04:18:21 +00:00
bool
2023-10-06 06:17:09 +00:00
Dictionary::spaceAvailable(uint8_t klen, uint8_t vlen)
2023-10-06 04:18:21 +00:00
{
size_t required = 0;
uintptr_t remaining = 0;
uint8_t *cursor = NULL;
2023-10-06 06:08:35 +00:00
cursor = TLV::FindEmpty(this->arena, NULL);
2023-10-06 04:18:21 +00:00
if (cursor == NULL) {
return false;
}
2023-10-06 06:08:35 +00:00
required += klen + 2;
required += vlen + 2;
2023-10-06 04:18:21 +00:00
remaining = (uintptr_t)cursor - (uintptr_t)arena.NewCursor();
remaining = arena.Size() - remaining;
2023-10-06 04:18:21 +00:00
return ((size_t)remaining >= required);
}
2023-10-06 06:08:35 +00:00
#if defined(DESKTOP_BUILD)
void
2023-10-06 06:17:09 +00:00
Dictionary::DumpKVPairs()
2023-10-06 06:08:35 +00:00
{
uint8_t *cursor = (this->arena).NewCursor();
2023-10-06 06:08:35 +00:00
TLV::Record rec;
TLV::ReadFromMemory(rec, cursor);
2023-10-06 06:17:09 +00:00
std::cout << "Dictionary KV pairs" << std::endl;
2023-10-06 06:08:35 +00:00
if (rec.Tag == TAG_EMPTY) {
std::cout << "\t(NONE)" << std::endl;
return;
}
while ((cursor != NULL) && (rec.Tag != TAG_EMPTY)) {
std::cout << "\t" << rec.Val << "->";
cursor = TLV::SkipRecord(rec, cursor);
TLV::ReadFromMemory(rec, cursor);
std::cout << rec.Val << std::endl;
cursor = TLV::SkipRecord(rec, cursor);
TLV::ReadFromMemory(rec, cursor);
}
}
#else
2023-10-06 06:08:35 +00:00
void
Dictionary::DumpKVPairs()
2023-10-06 06:08:35 +00:00
{
2023-10-06 06:08:35 +00:00
}
#endif
2023-10-06 06:08:35 +00:00
void
Dictionary::DumpToFile(const char *path)
2023-10-06 06:08:35 +00:00
{
this->arena.Write(path);
2023-10-06 06:08:35 +00:00
}