WiiUPluginLoaderBackend/source/utils/storage/StorageSubItem.cpp

85 lines
2.3 KiB
C++
Raw Normal View History

2023-10-07 22:38:27 +02:00
#include "StorageSubItem.h"
2024-11-27 20:44:36 +01:00
#include <utils/utils.h>
StorageSubItem::StorageSubItem(const std::string_view key) : StorageItem(key) {
}
2023-12-16 12:44:20 +01:00
StorageSubItem *StorageSubItem::getSubItem(wups_storage_item item) {
2023-10-07 22:38:27 +02:00
// Try to find the sub-item based on item handle.
2023-12-16 12:44:20 +01:00
for (auto &cur : mSubCategories) {
2024-11-27 20:44:36 +01:00
if (cur.getHandle() == reinterpret_cast<uint32_t>(item)) {
2023-12-16 12:44:20 +01:00
return &cur;
2023-10-07 22:38:27 +02:00
}
}
// If not found in current category, recursively search in sub-categories.
2023-12-16 12:44:20 +01:00
for (auto &cur : mSubCategories) {
2024-11-27 20:44:36 +01:00
if (const auto res = cur.getSubItem(item)) {
2023-10-07 22:38:27 +02:00
return res;
}
}
return nullptr;
}
const StorageSubItem *StorageSubItem::getSubItem(const char *key) const {
// Try to find the sub-item based on key.
for (const auto &cur : mSubCategories) {
2023-12-16 12:44:20 +01:00
if (cur.getKey() == key) {
return &cur;
2023-10-07 22:38:27 +02:00
}
}
return nullptr;
}
bool StorageSubItem::deleteItem(const char *key) {
2023-12-16 12:44:20 +01:00
if (remove_first_if(mSubCategories, [key](auto &cur) { return cur.getKey() == key; })) {
2023-10-07 22:38:27 +02:00
return true;
}
2024-11-27 20:44:36 +01:00
if (const auto itemItr = mItems.find(key); itemItr != mItems.end()) {
2023-10-07 22:38:27 +02:00
mItems.erase(itemItr);
return true; // Item found and deleted.
}
return false;
}
2024-11-27 20:44:36 +01:00
StorageItem *StorageSubItem::createItem(const char *key, StorageSubItemError &error) {
if (getSubItem(key) != nullptr) {
error = STORAGE_SUB_ITEM_KEY_ALREADY_IN_USE;
return nullptr;
2023-10-07 22:38:27 +02:00
}
2024-11-27 20:44:36 +01:00
if (const auto [addedItem, itemAdded] = mItems.insert({key, StorageItem(key)}); itemAdded) {
return &addedItem->second;
2023-10-07 22:38:27 +02:00
}
2024-11-27 20:44:36 +01:00
error = STORAGE_SUB_ITEM_KEY_ALREADY_IN_USE;
2023-12-16 12:44:20 +01:00
return nullptr;
2023-10-07 22:38:27 +02:00
}
2024-11-27 20:44:36 +01:00
StorageSubItem *StorageSubItem::createSubItem(const char *key, StorageSubItemError &error) {
if (getItem(key) != nullptr || getSubItem(key) != nullptr) {
2023-10-07 22:38:27 +02:00
error = STORAGE_SUB_ITEM_KEY_ALREADY_IN_USE;
return nullptr;
}
2023-12-16 12:44:20 +01:00
mSubCategories.emplace_front(key);
return &mSubCategories.front();
2023-10-07 22:38:27 +02:00
}
StorageItem *StorageSubItem::getItem(const char *name) {
2024-11-27 20:44:36 +01:00
if (const auto resItr = mItems.find(name); resItr != mItems.end()) {
2023-12-16 12:44:20 +01:00
return &resItr->second;
2023-10-07 22:38:27 +02:00
}
return nullptr;
}
2024-11-27 20:44:36 +01:00
const std::forward_list<StorageSubItem> &StorageSubItem::getSubItems() const {
return mSubCategories;
}
const std::map<std::string, StorageItem> &StorageSubItem::getItems() const {
return mItems;
}