From f9edb050dee184a3b27b6adb8595527bbdc97308 Mon Sep 17 00:00:00 2001 From: emb <> Date: Fri, 30 Jan 2015 21:43:13 -0600 Subject: [PATCH] Prevent crashing when list was empty (or if the database files could not be found). --- RetroFE/Source/Database/MetadataDatabase.cpp | 101 +++++++++--------- .../Graphics/Component/ScrollingList.cpp | 47 ++++---- 2 files changed, 77 insertions(+), 71 deletions(-) diff --git a/RetroFE/Source/Database/MetadataDatabase.cpp b/RetroFE/Source/Database/MetadataDatabase.cpp index 1994472..38b5a46 100644 --- a/RetroFE/Source/Database/MetadataDatabase.cpp +++ b/RetroFE/Source/Database/MetadataDatabase.cpp @@ -109,8 +109,8 @@ bool MetadataDatabase::ImportDirectory() { DIR *dp; struct dirent *dirp; - std::string hyperListPath = "Meta/Hyperlist"; - std::string mameListPath = "Meta/Mamelist"; + std::string hyperListPath = Configuration::GetAbsolutePath() + "/Meta/Hyperlist"; + std::string mameListPath = Configuration::GetAbsolutePath() + "/Meta/Mamelist"; dp = opendir(hyperListPath.c_str()); @@ -134,7 +134,7 @@ bool MetadataDatabase::ImportDirectory() if(extension == ".xml") { - std::string importFile = Configuration::GetAbsolutePath() + "/" + hyperListPath + "/" + dirp->d_name; + std::string importFile = hyperListPath + "/" + dirp->d_name; Logger::Write(Logger::ZONE_INFO, "Metadata", "Importing hyperlist: " + importFile); ImportHyperList(importFile, collectionName); } @@ -166,7 +166,7 @@ bool MetadataDatabase::ImportDirectory() if(extension == ".xml") { - std::string importFile = Configuration::GetAbsolutePath() + "/" + mameListPath + "/" + dirp->d_name; + std::string importFile = mameListPath + "/" + dirp->d_name; Logger::Write(Logger::ZONE_INFO, "Metadata", "Importing mamelist: " + importFile); Config.SetStatus("Scraping data from " + importFile); ImportMameList(importFile, collectionName); @@ -300,7 +300,6 @@ bool MetadataDatabase::NeedsRefresh() bool MetadataDatabase::ImportHyperList(std::string hyperlistFile, std::string collectionName) { - bool retVal = false; char *error = NULL; Config.SetStatus("Scraping data from \"" + hyperlistFile + "\""); @@ -319,55 +318,54 @@ bool MetadataDatabase::ImportHyperList(std::string hyperlistFile, std::string co if(!root) { Logger::Write(Logger::ZONE_ERROR, "Metadata", "Does not appear to be a HyperList file (missing tag)"); - return NULL; + return false; } - else + sqlite3 *handle = DBInstance.GetHandle(); + sqlite3_exec(handle, "BEGIN IMMEDIATE TRANSACTION;", NULL, NULL, &error); + for(rapidxml::xml_node<> *game = root->first_node("game"); game; game = game->next_sibling("game")) { - sqlite3 *handle = DBInstance.GetHandle(); - sqlite3_exec(handle, "BEGIN IMMEDIATE TRANSACTION;", NULL, NULL, &error); - for(rapidxml::xml_node<> *game = root->first_node("game"); game; game = game->next_sibling("game")) + rapidxml::xml_attribute<> *nameXml = game->first_attribute("name"); + rapidxml::xml_node<> *descriptionXml = game->first_node("description"); + rapidxml::xml_node<> *cloneofXml = game->first_node("cloneof"); + rapidxml::xml_node<> *crcXml = game->first_node("crc"); + rapidxml::xml_node<> *manufacturerXml = game->first_node("manufacturer"); + rapidxml::xml_node<> *yearXml = game->first_node("year"); + rapidxml::xml_node<> *genreXml = game->first_node("genre"); + rapidxml::xml_node<> *ratingXml = game->first_node("rating"); + rapidxml::xml_node<> *enabledXml = game->first_node("enabled"); + std::string name = (nameXml) ? nameXml->value() : ""; + std::string description = (descriptionXml) ? descriptionXml->value() : ""; + std::string crc = (crcXml) ? crcXml->value() : ""; + std::string cloneOf = (cloneofXml) ? cloneofXml->value() : ""; + std::string manufacturer = (manufacturerXml) ? manufacturerXml->value() : ""; + std::string year = (yearXml) ? yearXml->value() : ""; + std::string genre = (genreXml) ? genreXml->value() : ""; + std::string rating = (ratingXml) ? ratingXml->value() : ""; + std::string enabled = (enabledXml) ? enabledXml->value() : ""; + + if(name.length() > 0) { - rapidxml::xml_attribute<> *nameXml = game->first_attribute("name"); - rapidxml::xml_node<> *descriptionXml = game->first_node("description"); - rapidxml::xml_node<> *cloneofXml = game->first_node("cloneof"); - rapidxml::xml_node<> *crcXml = game->first_node("crc"); - rapidxml::xml_node<> *manufacturerXml = game->first_node("manufacturer"); - rapidxml::xml_node<> *yearXml = game->first_node("year"); - rapidxml::xml_node<> *genreXml = game->first_node("genre"); - rapidxml::xml_node<> *ratingXml = game->first_node("rating"); - rapidxml::xml_node<> *enabledXml = game->first_node("enabled"); - std::string name = (nameXml) ? nameXml->value() : ""; - std::string description = (descriptionXml) ? descriptionXml->value() : ""; - std::string crc = (crcXml) ? crcXml->value() : ""; - std::string cloneOf = (cloneofXml) ? cloneofXml->value() : ""; - std::string manufacturer = (manufacturerXml) ? manufacturerXml->value() : ""; - std::string year = (yearXml) ? yearXml->value() : ""; - std::string genre = (genreXml) ? genreXml->value() : ""; - std::string rating = (ratingXml) ? ratingXml->value() : ""; - std::string enabled = (enabledXml) ? enabledXml->value() : ""; + sqlite3_stmt *stmt; - if(name.length() > 0) - { - sqlite3_stmt *stmt; + sqlite3_prepare_v2(handle, + "INSERT OR REPLACE INTO Meta (name, title, year, manufacturer, cloneOf, collectionName) VALUES (?,?,?,?,?,?)", + -1, &stmt, 0); - sqlite3_prepare_v2(handle, - "INSERT OR REPLACE INTO Meta (name, title, year, manufacturer, cloneOf, collectionName) VALUES (?,?,?,?,?,?)", - -1, &stmt, 0); + sqlite3_bind_text(stmt, 1, name.c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 2, description.c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 3, year.c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 4, manufacturer.c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 5, cloneOf.c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 6, collectionName.c_str(), -1, SQLITE_TRANSIENT); - sqlite3_bind_text(stmt, 1, name.c_str(), -1, SQLITE_TRANSIENT); - sqlite3_bind_text(stmt, 2, description.c_str(), -1, SQLITE_TRANSIENT); - sqlite3_bind_text(stmt, 3, year.c_str(), -1, SQLITE_TRANSIENT); - sqlite3_bind_text(stmt, 4, manufacturer.c_str(), -1, SQLITE_TRANSIENT); - sqlite3_bind_text(stmt, 5, cloneOf.c_str(), -1, SQLITE_TRANSIENT); - sqlite3_bind_text(stmt, 6, collectionName.c_str(), -1, SQLITE_TRANSIENT); - - sqlite3_step(stmt); - sqlite3_finalize(stmt); - } + sqlite3_step(stmt); + sqlite3_finalize(stmt); } - Config.SetStatus("Saving data from \"" + hyperlistFile + "\" to database"); - sqlite3_exec(handle, "COMMIT TRANSACTION;", NULL, NULL, &error); } + Config.SetStatus("Saving data from \"" + hyperlistFile + "\" to database"); + sqlite3_exec(handle, "COMMIT TRANSACTION;", NULL, NULL, &error); + + return true; } catch(rapidxml::parse_error &e) { @@ -385,12 +383,11 @@ bool MetadataDatabase::ImportHyperList(std::string hyperlistFile, std::string co } - return retVal; + return false; } bool MetadataDatabase::ImportMameList(std::string filename, std::string collectionName) { - bool retVal = true; rapidxml::xml_document<> doc; rapidxml::xml_node<> * rootNode; char *error = NULL; @@ -409,6 +406,12 @@ bool MetadataDatabase::ImportMameList(std::string filename, std::string collecti rootNode = doc.first_node("mame"); + if(!rootNode) + { + Logger::Write(Logger::ZONE_ERROR, "Metadata", "Does not appear to be a MameList file (missing tag)"); + return false; + } + sqlite3_exec(handle, "BEGIN IMMEDIATE TRANSACTION;", NULL, NULL, &error); for (rapidxml::xml_node<> * game = rootNode->first_node("game"); game; game = game->next_sibling()) { @@ -471,7 +474,7 @@ bool MetadataDatabase::ImportMameList(std::string filename, std::string collecti Config.SetStatus("Saving data from \"" + filename + "\" to database"); sqlite3_exec(handle, "COMMIT TRANSACTION;", NULL, NULL, &error); - return retVal; + return true; } diff --git a/RetroFE/Source/Graphics/Component/ScrollingList.cpp b/RetroFE/Source/Graphics/Component/ScrollingList.cpp index f38a3ef..988ac6e 100644 --- a/RetroFE/Source/Graphics/Component/ScrollingList.cpp +++ b/RetroFE/Source/Graphics/Component/ScrollingList.cpp @@ -75,8 +75,7 @@ void ScrollingList::SetItems(std::vector *spriteList) SpriteList = spriteList; FirstSpriteIndex = 0; - - if(!SpriteList) { return ; } + if(!SpriteList) { return; } unsigned int originalSize = SpriteList->size(); // loop the scroll points if there are not enough, the +2 represents the head and tail nodes (for when the item is allocated) @@ -103,6 +102,8 @@ void ScrollingList::SetItems(std::vector *spriteList) void ScrollingList::DeallocateSpritePoints() { + if(!SpriteList) { return; } + unsigned int spriteIndex = FirstSpriteIndex; for(unsigned int i = 0; i < ScrollPoints->size(); ++i) @@ -114,11 +115,15 @@ void ScrollingList::DeallocateSpritePoints() void ScrollingList::AllocateSpritePoints() { + if(!ScrollPoints) { return; } + if(!SpriteList) { return; } + if(SpriteList->size() == 0) { return; } + if(!TweenPoints) { return; } + unsigned int spriteIndex = FirstSpriteIndex; for(unsigned int i = 0; i < ScrollPoints->size(); ++i) { - AllocateTexture(SpriteList->at(spriteIndex)); Component *c = SpriteList->at(spriteIndex)->GetComponent(); ViewInfo *currentViewInfo = ScrollPoints->at(i); @@ -133,33 +138,31 @@ void ScrollingList::AllocateSpritePoints() void ScrollingList::DestroyItems() { - if(SpriteList) + if(!SpriteList) { return; } + std::vector::iterator it = SpriteList->begin(); + + while(it != SpriteList->end()) { - std::vector::iterator it = SpriteList->begin(); - - while(it != SpriteList->end()) + if(*it != NULL) { - if(*it != NULL) + DeallocateTexture(*it); + + if((*it)->GetCollectionItem()) { - DeallocateTexture(*it); - - if((*it)->GetCollectionItem()) - { - delete (*it)->GetCollectionItem(); - } - - delete *it; + delete (*it)->GetCollectionItem(); } - - SpriteList->erase(it); - - it = SpriteList->begin(); + delete *it; } - delete SpriteList; - SpriteList = NULL; + + SpriteList->erase(it); + + it = SpriteList->begin(); } + + delete SpriteList; + SpriteList = NULL; }