2021-03-03 19:59:39 +01:00
|
|
|
#include "FFvaluestore.h"
|
|
|
|
|
|
2022-06-18 14:25:31 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
2022-08-20 18:17:00 +02:00
|
|
|
typedef char KeyType[33]; //32 byte key + \0
|
|
|
|
|
|
|
|
|
|
void ffValuestoreInit(FFvaluestore* vs, uint32_t valueSize)
|
2021-03-03 19:59:39 +01:00
|
|
|
{
|
2022-08-20 18:17:00 +02:00
|
|
|
ffListInit(vs, sizeof(KeyType) + valueSize);
|
2021-03-03 19:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-20 18:17:00 +02:00
|
|
|
void* ffValuestoreGet(FFvaluestore* vs, const char* key)
|
2021-03-03 19:59:39 +01:00
|
|
|
{
|
2022-08-20 18:17:00 +02:00
|
|
|
for(uint32_t i = 0; i < vs->length; i++)
|
2021-03-03 19:59:39 +01:00
|
|
|
{
|
2022-08-20 18:17:00 +02:00
|
|
|
char* element = ffListGet(vs, i);
|
|
|
|
|
if(strcmp(element, key) == 0)
|
|
|
|
|
return element + sizeof(KeyType);
|
2021-03-03 19:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-20 18:17:00 +02:00
|
|
|
return NULL;
|
2021-03-03 19:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-20 18:17:00 +02:00
|
|
|
void* ffValuestoreSet(FFvaluestore* vs, const char* key, bool* created)
|
2021-03-03 19:59:39 +01:00
|
|
|
{
|
2022-08-20 18:17:00 +02:00
|
|
|
char* element = ffValuestoreGet(vs, key);
|
|
|
|
|
if(element != NULL)
|
2021-03-03 19:59:39 +01:00
|
|
|
{
|
2022-08-20 18:17:00 +02:00
|
|
|
if(created != NULL)
|
|
|
|
|
*created = false;
|
|
|
|
|
return element;
|
2021-03-03 19:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-20 18:17:00 +02:00
|
|
|
element = ffListAdd(vs);
|
|
|
|
|
strncpy(element, key, sizeof(KeyType) - 1);
|
|
|
|
|
element[sizeof(KeyType) - 1] = '\0';
|
2021-03-03 19:59:39 +01:00
|
|
|
|
2022-08-20 18:17:00 +02:00
|
|
|
if(created != NULL)
|
|
|
|
|
*created = true;
|
|
|
|
|
return element + sizeof(KeyType);
|
2021-03-03 19:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-20 18:17:00 +02:00
|
|
|
void ffValuestoreDestroy(FFvaluestore* vs)
|
2021-03-03 19:59:39 +01:00
|
|
|
{
|
2022-08-20 18:17:00 +02:00
|
|
|
ffListDestroy(vs);
|
2021-03-27 18:14:19 +01:00
|
|
|
}
|