StorageAPI: Only proccess items for the last returned sub category or root item.

This commit is contained in:
Maschell 2023-02-15 19:42:47 +01:00
parent 7f76e31e70
commit e40d91fcec
2 changed files with 94 additions and 19 deletions

View File

@ -27,6 +27,7 @@ typedef enum {
WUPS_STORAGE_ERROR_B64_DECODE_FAILED = -9,
WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL = -10,
WUPS_STORAGE_ERROR_MALLOC_FAILED = -11,
WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY = -13,
} WUPSStorageError;
typedef struct wups_storage_item_t_ {

View File

@ -13,6 +13,8 @@ static uint32_t isOpened __attribute__((section(".data")));
static uint32_t isDirty __attribute__((section(".data")));
static wups_storage_item_t rootItem __attribute__((section(".data")));
static wups_storage_item_t *sActiveSubItem __attribute__((section(".data"))) = nullptr;
void WUPS_InitStorage(wups_loader_init_storage_args_t args) {
openfunction_ptr = args.open_storage_ptr;
closefunction_ptr = args.close_storage_ptr;
@ -21,6 +23,7 @@ void WUPS_InitStorage(wups_loader_init_storage_args_t args) {
storage_initialized = true;
isOpened = false;
isDirty = false;
sActiveSubItem = nullptr;
rootItem.key = nullptr;
rootItem.data = nullptr;
@ -55,6 +58,8 @@ const char *WUPS_GetStorageStatusStr(WUPSStorageError status) {
return "WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL";
case WUPS_STORAGE_ERROR_MALLOC_FAILED:
return "WUPS_STORAGE_ERROR_MALLOC_FAILED";
case WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY:
return "WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY";
}
return "WUPS_STORAGE_ERROR_UNKNOWN";
}
@ -116,6 +121,7 @@ WUPSStorageError WUPS_CloseStorage(void) {
rootItem.data = nullptr;
rootItem.key = nullptr;
}
sActiveSubItem = nullptr;
return result;
}
@ -135,9 +141,16 @@ WUPSStorageError WUPS_DeleteItem(wups_storage_item_t *parent, const char *key) {
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
isDirty = true;
@ -154,6 +167,9 @@ WUPSStorageError WUPS_DeleteItem(wups_storage_item_t *parent, const char *key) {
item->key = nullptr;
item->data = nullptr;
item->deleted = true;
if (sActiveSubItem == item) {
sActiveSubItem = nullptr;
}
return WUPS_STORAGE_ERROR_SUCCESS;
}
}
@ -263,9 +279,16 @@ WUPSStorageError WUPS_CreateSubItem(wups_storage_item_t *parent, const char *key
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
isDirty = true;
@ -274,6 +297,7 @@ WUPSStorageError WUPS_CreateSubItem(wups_storage_item_t *parent, const char *key
if (item == nullptr) {
return error;
}
sActiveSubItem = item;
*outItem = item;
return WUPS_STORAGE_ERROR_SUCCESS;
@ -294,9 +318,16 @@ WUPSStorageError WUPS_GetSubItem(wups_storage_item_t *parent, const char *key, w
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
for (uint32_t i = 0; i < parent->data_size; i++) {
wups_storage_item_t *item = &((wups_storage_item_t *) parent->data)[i];
@ -306,6 +337,7 @@ WUPSStorageError WUPS_GetSubItem(wups_storage_item_t *parent, const char *key, w
}
if (strcmp(item->key, key) == 0) {
sActiveSubItem = item;
*outItem = item;
return WUPS_STORAGE_ERROR_SUCCESS;
}
@ -329,9 +361,16 @@ WUPSStorageError WUPS_StoreString(wups_storage_item_t *parent, const char *key,
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
isDirty = true;
@ -373,9 +412,16 @@ WUPSStorageError WUPS_StoreInt(wups_storage_item_t *parent, const char *key, int
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
isDirty = true;
@ -412,9 +458,16 @@ WUPSStorageError WUPS_StoreBinary(wups_storage_item_t *parent, const char *key,
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
isDirty = true;
@ -450,9 +503,16 @@ WUPSStorageError WUPS_GetString(wups_storage_item_t *parent, const char *key, ch
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
for (uint32_t i = 0; i < parent->data_size; i++) {
wups_storage_item_t *item = &((wups_storage_item_t *) parent->data)[i];
@ -496,9 +556,16 @@ WUPSStorageError WUPS_GetInt(wups_storage_item_t *parent, const char *key, int32
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
for (uint32_t i = 0; i < parent->data_size; i++) {
wups_storage_item_t *item = &((wups_storage_item_t *) parent->data)[i];
@ -531,9 +598,16 @@ WUPSStorageError WUPS_GetBinary(wups_storage_item_t *parent, const char *key, vo
if (!parent) {
parent = &rootItem;
} else if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
} else {
// We can only safely process items of a parent if the parent was the last
// item returned by WUPS_GetSubItem or WUPS_CreateSubItem
if (parent != sActiveSubItem) {
return WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY;
}
if (parent->type != WUPS_STORAGE_TYPE_ITEM) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
}
for (uint32_t i = 0; i < parent->data_size; i++) {
wups_storage_item_t *item = &((wups_storage_item_t *) parent->data)[i];