StorageAPI: code cleanup

This commit is contained in:
Maschell 2025-01-25 22:20:26 +01:00
parent 1cef78d8bf
commit 9d4052f9be
2 changed files with 15 additions and 26 deletions

View File

@ -34,7 +34,7 @@ namespace StorageUtils {
for (auto it = json.begin(); it != json.end(); ++it) { for (auto it = json.begin(); it != json.end(); ++it) {
StorageSubItem::StorageSubItemError subItemError = StorageSubItem::STORAGE_SUB_ITEM_ERROR_NONE; StorageSubItem::StorageSubItemError subItemError = StorageSubItem::STORAGE_SUB_ITEM_ERROR_NONE;
if (it.value().is_object()) { if (it.value().is_object()) {
auto res = item.createSubItem(it.key().c_str(), subItemError); const auto res = item.createSubItem(it.key().c_str(), subItemError);
if (!res) { if (!res) {
DEBUG_FUNCTION_LINE_WARN("Failed to create sub item: Error %d", subItemError); DEBUG_FUNCTION_LINE_WARN("Failed to create sub item: Error %d", subItemError);
return false; return false;
@ -46,26 +46,26 @@ namespace StorageUtils {
} }
} }
} else { } else {
auto res = item.createItem(it.key().c_str(), subItemError); const auto res = item.createItem(it.key().c_str(), subItemError);
if (!res) { if (!res) {
DEBUG_FUNCTION_LINE_WARN("Failed to create Item for key %s. Error %d", it.key().c_str(), subItemError); DEBUG_FUNCTION_LINE_WARN("Failed to create Item for key %s. Error %d", it.key().c_str(), subItemError);
return false; return false;
} }
if (it.value().is_string()) { if (it.value().is_string()) {
auto val = it.value().get<std::string>(); const auto val = it.value().get<std::string>();
res->setValue(val); res->setValue(val);
} else if (it.value().is_boolean()) { } else if (it.value().is_boolean()) {
auto val = it.value().get<bool>(); const auto val = it.value().get<bool>();
res->setValue(val); res->setValue(val);
} else if (it.value().is_number_unsigned()) { } else if (it.value().is_number_unsigned()) {
auto val = it.value().get<std::uint64_t>(); const auto val = it.value().get<std::uint64_t>();
res->setValue(val); res->setValue(val);
} else if (it.value().is_number_integer()) { } else if (it.value().is_number_integer()) {
auto val = it.value().get<std::int64_t>(); const auto val = it.value().get<std::int64_t>();
res->setValue(val); res->setValue(val);
} else if (it.value().is_number_float()) { } else if (it.value().is_number_float()) {
auto val = it.value().get<std::double_t>(); const auto val = it.value().get<std::double_t>();
res->setValue(val); res->setValue(val);
} else { } else {
DEBUG_FUNCTION_LINE_ERR("Unknown type %s for value %s", it.value().type_name(), it.key().c_str()); DEBUG_FUNCTION_LINE_ERR("Unknown type %s for value %s", it.value().type_name(), it.key().c_str());
@ -97,36 +97,31 @@ namespace StorageUtils {
for (const auto &[key, value] : baseItem.getItems()) { for (const auto &[key, value] : baseItem.getItems()) {
switch ((StorageItemType) value.getType()) { switch ((StorageItemType) value.getType()) {
case StorageItemType::String: { case StorageItemType::String: {
std::string res; if (std::string res; value.getValue(res)) {
if (value.getValue(res)) {
json[key] = res; json[key] = res;
} }
break; break;
} }
case StorageItemType::Boolean: { case StorageItemType::Boolean: {
bool res; if (bool res; value.getValue(res)) {
if (value.getValue(res)) {
json[key] = res; json[key] = res;
} }
break; break;
} }
case StorageItemType::S64: { case StorageItemType::S64: {
int64_t res; if (int64_t res; value.getValue(res)) {
if (value.getValue(res)) {
json[key] = res; json[key] = res;
} }
break; break;
} }
case StorageItemType::U64: { case StorageItemType::U64: {
uint64_t res; if (uint64_t res; value.getValue(res)) {
if (value.getValue(res)) {
json[key] = res; json[key] = res;
} }
break; break;
} }
case StorageItemType::Double: { case StorageItemType::Double: {
double res; if (double res; value.getValue(res)) {
if (value.getValue(res)) {
json[key] = res; json[key] = res;
} }
break; break;
@ -209,14 +204,8 @@ namespace StorageUtils {
return WUPS_STORAGE_ERROR_SUCCESS; return WUPS_STORAGE_ERROR_SUCCESS;
} }
static WUPSStorageError WriteStorageToSD(wups_storage_root_item root, bool forceSave) { static WUPSStorageError WriteStorageToSD(wups_storage_root_item root, const bool forceSave) {
const StorageItemRoot *rootItem = nullptr; const auto rootItem = Helper::getRootItem(root);
for (const auto &cur : gStorage) {
if (cur.getHandle() == reinterpret_cast<uint32_t>(root)) {
rootItem = &cur;
break;
}
}
if (!rootItem) { if (!rootItem) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED; return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
} }

View File

@ -9,7 +9,7 @@
namespace StorageUtils::API { namespace StorageUtils::API {
namespace Internal { namespace Internal {
WUPSStorageError OpenStorage(std::string_view plugin_id, wups_storage_root_item &outItem); WUPSStorageError OpenStorage(std::string_view plugin_id, wups_storage_root_item &outItem);
WUPSStorageError CloseStorage(wups_storage_root_item item); WUPSStorageError CloseStorage(wups_storage_root_item root);
} // namespace Internal } // namespace Internal
WUPSStorageError SaveStorage(wups_storage_root_item root, bool force); WUPSStorageError SaveStorage(wups_storage_root_item root, bool force);