From 0a29c9d32142dbc4e465377a3dc1acef4d2ee7d4 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 2 Mar 2016 08:50:12 -0500 Subject: [PATCH] init: Implemented lightweightVersionCheck --- src/init.cpp | 15 +++++++- src/timew.cpp | 101 +++++++++++++++++++++++--------------------------- src/timew.h | 1 + 3 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 325c8424..2525c8db 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -34,7 +34,20 @@ #include #include #include -#include // TODO Remove +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +bool lightweightVersionCheck (int argc, const char** argv) +{ + if (argc == 2 && ! std::strcmp (argv[1], "--version")) + { + std::cout << VERSION << "\n"; + return true; + } + + return false; +} //////////////////////////////////////////////////////////////////////////////// void initializeData (Configuration& configuration, Database& database) diff --git a/src/timew.cpp b/src/timew.cpp index a8fc2f51..c46260f4 100644 --- a/src/timew.cpp +++ b/src/timew.cpp @@ -35,74 +35,65 @@ //#include #include #include -#include // No global data. //////////////////////////////////////////////////////////////////////////////// int main (int argc, const char** argv) { + // Lightweight version checking that doesn't require initialization or I/O. int status = 0; - bool debug = true; + if (lightweightVersionCheck (argc, argv)) + return status; - // Lightweight version checking that doesn't require initialization or any I/O. - if (argc == 2 && ! strcmp (argv[1], "--version")) + try { - std::cout << VERSION << "\n"; + // Load the configuration, prepare the database, but do not read data. + Configuration configuration; + Database database; + initializeData (configuration, database); + + // TODO Arrange the following to minimize memory use. + // TODO Load CLI grammar. + // TODO Load from string, else file on config override. + // TODO Migrate from loading a grammar from file, to a default string. +/* + File cliFile ("./cli.grammar"); + Grammar cliGrammar; + cliGrammar.debug (debug); + cliGrammar.loadFromFile (cliFile); + + // Instantiate the parser. + LR0 cliParser; + cliParser.debug (debug); + cliParser.initialize (cliGrammar); + // TODO Parse CLI. +*/ + + // Load the rules. + Rules rules; + initializeRules (configuration, rules); + + // Dispatch to commands. + status = dispatchCommand (argc, argv, configuration, rules); } - else + + catch (const std::string& error) { - try - { - // Load the configuration, prepare the database, but do not read data. - Configuration configuration; - Database database; - initializeData (configuration, database); + std::cerr << error << "\n"; + status = -1; + } - // TODO Arrange the following to minimize memory use. - // TODO Load CLI grammar. - // TODO Load from string, else file on config override. - // TODO Migrate from loading a grammar from file, to a default string. -/* - File cliFile ("./cli.grammar"); - Grammar cliGrammar; - cliGrammar.debug (debug); - cliGrammar.loadFromFile (cliFile); -*/ - // Instantiate the parser. -/* - LR0 cliParser; - cliParser.debug (debug); - cliParser.initialize (cliGrammar); -*/ + catch (std::bad_alloc& error) + { + std::cerr << "Error: Memory allocation failed: " << error.what () << "\n"; + status = -3; + } - // TODO Parse CLI. - - // Load the rules. - Rules rules; - initializeRules (configuration, rules); - - // Dispatch to commands. - status = dispatchCommand (argc, argv, configuration, rules); - } - - catch (const std::string& error) - { - std::cerr << error << "\n"; - status = -1; - } - - catch (std::bad_alloc& error) - { - std::cerr << "Error: Memory allocation failed: " << error.what () << "\n"; - status = -3; - } - - catch (...) - { - std::cerr << "Error: Unknown problem, please report.\n"; - status = -2; - } + catch (...) + { + std::cerr << "Error: Unknown problem, please report.\n"; + status = -2; } return status; diff --git a/src/timew.h b/src/timew.h index c20079b0..c0efb747 100644 --- a/src/timew.h +++ b/src/timew.h @@ -32,6 +32,7 @@ #include // init.cpp +bool lightweightVersionCheck (int, const char**); void initializeData (Configuration&, Database&); void initializeRules (Configuration&, Rules&); int dispatchCommand (int, const char**, Configuration&, Rules&);