From 174ea4f4f952309b179d972582ffac17c80257ed Mon Sep 17 00:00:00 2001 From: emb <> Date: Thu, 1 Jan 2015 13:29:23 -0600 Subject: [PATCH] Cleaned up Main initialization handling. This will need to be refactored into a builder class. --- Source/Database/CollectionDatabase.cpp | 9 +-- Source/Database/CollectionDatabase.h | 2 +- Source/Database/DB.cpp | 5 +- Source/Database/DB.h | 2 +- Source/Main.cpp | 101 ++++++++++++++++--------- 5 files changed, 73 insertions(+), 46 deletions(-) diff --git a/Source/Database/CollectionDatabase.cpp b/Source/Database/CollectionDatabase.cpp index f8d1f5d..ad256c8 100644 --- a/Source/Database/CollectionDatabase.cpp +++ b/Source/Database/CollectionDatabase.cpp @@ -58,14 +58,11 @@ bool CollectionDatabase::ResetDatabase() retVal = false; } - //CheckDatabase(); - return retVal; } -bool CollectionDatabase::CheckDatabase() +bool CollectionDatabase::Initialize() { - bool retVal = true; int rc; char *error = NULL; sqlite3 *handle = DBInstance->GetHandle(); @@ -101,10 +98,10 @@ bool CollectionDatabase::CheckDatabase() ss << "Unable to create Configurations table. Error: " << error; Logger::Write(Logger::ZONE_ERROR, "Database", ss.str()); - retVal = false; + return false; } - return retVal; + return true; } diff --git a/Source/Database/CollectionDatabase.h b/Source/Database/CollectionDatabase.h index 5986123..a18f3e7 100644 --- a/Source/Database/CollectionDatabase.h +++ b/Source/Database/CollectionDatabase.h @@ -17,9 +17,9 @@ class CollectionDatabase public: CollectionDatabase(DB *db, Configuration *c); virtual ~CollectionDatabase(); + bool Initialize(); bool Import(); bool ResetDatabase(); - bool CheckDatabase(); bool GetCollection(std::string collectionName, std::vector &list); diff --git a/Source/Database/DB.cpp b/Source/Database/DB.cpp index b042f66..2c041fe 100644 --- a/Source/Database/DB.cpp +++ b/Source/Database/DB.cpp @@ -2,14 +2,13 @@ * file 'LICENSE.txt', which is part of this source code package. */ #include "DB.h" -#include "Configuration.h" #include "../Utility/Log.h" #include #include -DB::DB() -: Path(Configuration::GetAbsolutePath() + "/cache.db") +DB::DB(std::string dbFile) +: Path(dbFile) , Handle(NULL) { } diff --git a/Source/Database/DB.h b/Source/Database/DB.h index f214b85..c3f28a6 100644 --- a/Source/Database/DB.h +++ b/Source/Database/DB.h @@ -8,7 +8,7 @@ class DB { public: - DB(); + DB(std::string dbFile); bool Initialize(); void DeInitialize(); virtual ~DB(); diff --git a/Source/Main.cpp b/Source/Main.cpp index e8d846d..3e7ee11 100644 --- a/Source/Main.cpp +++ b/Source/Main.cpp @@ -17,6 +17,8 @@ #include static bool ImportConfiguration(Configuration *c); +static bool StartLogging(); +CollectionDatabase *InitializeCollectionDatabase(DB &db, Configuration &config); int main(int argc, char *argv[]) { @@ -24,55 +26,39 @@ int main(int argc, char *argv[]) Configuration config; - // set the log file to write to - std::string logFile = Configuration::GetAbsolutePath() + "/Log.txt"; - - if(!Logger::Initialize(logFile)) - { - Logger::Write(Logger::ZONE_ERROR, "RetroFE", "Could not open \"" + logFile + "\" for writing"); + if(!StartLogging()) { + return -1; } - Logger::Write(Logger::ZONE_INFO, "RetroFE", "Version " + Version::GetString() + " starting"); -#ifdef WIN32 - Logger::Write(Logger::ZONE_INFO, "RetroFE", "OS: Windows"); -#else - Logger::Write(Logger::ZONE_INFO, "RetroFE", "OS: Linux"); -#endif - - Logger::Write(Logger::ZONE_INFO, "RetroFE", "Absolute path: " + Configuration::GetAbsolutePath()); - if(!ImportConfiguration(&config)) { return -1; } - Logger::Write(Logger::ZONE_INFO, "RetroFE", "Imported configuration"); - std::string dbFile = (Configuration::GetAbsolutePath() + "/cache.db"); - std::ifstream infile(dbFile.c_str()); + DB db(Configuration::GetAbsolutePath() + "/cache.db"); - DB db; if(!db.Initialize()) { return -1; } + - CollectionDatabase cdb(&db, &config); - - cdb.CheckDatabase(); - - if(cdb.Import()) - { - RetroFE p(&cdb, &config); - - if(p.Initialize()) - { - p.Run(); - } - - p.DeInitialize(); + CollectionDatabase *cdb = InitializeCollectionDatabase(db, config); + if(!cdb) { + return -1; } + RetroFE p(cdb, &config); + + if(p.Initialize()) + { + p.Run(); + } + + p.DeInitialize(); + Logger::DeInitialize(); + return 0; } @@ -90,8 +76,6 @@ bool ImportConfiguration(Configuration *c) return false; } - - if(!c->Import("controls", configPath + "/Controls.conf")) { Logger::Write(Logger::ZONE_ERROR, "RetroFE", "Could not import \"" + configPath + "/Settings.conf\""); @@ -157,5 +141,52 @@ bool ImportConfiguration(Configuration *c) } } + Logger::Write(Logger::ZONE_INFO, "RetroFE", "Imported configuration"); + return true; } + +bool StartLogging() +{ + std::string logFile = Configuration::GetAbsolutePath() + "/Log.txt"; + + if(!Logger::Initialize(logFile)) + { + Logger::Write(Logger::ZONE_ERROR, "RetroFE", "Could not open \"" + logFile + "\" for writing"); + return false; + } + + Logger::Write(Logger::ZONE_INFO, "RetroFE", "Version " + Version::GetString() + " starting"); + +#ifdef WIN32 + Logger::Write(Logger::ZONE_INFO, "RetroFE", "OS: Windows"); +#else + Logger::Write(Logger::ZONE_INFO, "RetroFE", "OS: Linux"); +#endif + + Logger::Write(Logger::ZONE_INFO, "RetroFE", "Absolute path: " + Configuration::GetAbsolutePath()); + + return true; +} + +CollectionDatabase *InitializeCollectionDatabase(DB &db, Configuration &config) +{ + CollectionDatabase *cdb = NULL; + std::string dbFile = (Configuration::GetAbsolutePath() + "/cache.db"); + std::ifstream infile(dbFile.c_str()); + + cdb = new CollectionDatabase(&db, &config); + + if(!cdb->Initialize()) + { + delete cdb; + cdb = NULL; + } + else if(!cdb->Import()) + { + delete cdb; + cdb = NULL; + } + + return cdb; +} \ No newline at end of file