init: Implemented lightweightVersionCheck

This commit is contained in:
Paul Beckingham 2016-03-02 08:50:12 -05:00
parent 792c0a7b5c
commit 0a29c9d321
3 changed files with 61 additions and 56 deletions

View file

@ -34,7 +34,20 @@
#include <commands.h> #include <commands.h>
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream> // TODO Remove #include <cstring>
#include <iostream>
////////////////////////////////////////////////////////////////////////////////
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) void initializeData (Configuration& configuration, Database& database)

View file

@ -35,74 +35,65 @@
//#include <LR0.h> //#include <LR0.h>
#include <iostream> #include <iostream>
#include <new> #include <new>
#include <cstring>
// No global data. // No global data.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, const char** argv) int main (int argc, const char** argv)
{ {
// Lightweight version checking that doesn't require initialization or I/O.
int status = 0; int status = 0;
bool debug = true; if (lightweightVersionCheck (argc, argv))
return status;
// Lightweight version checking that doesn't require initialization or any I/O. try
if (argc == 2 && ! strcmp (argv[1], "--version"))
{ {
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 std::cerr << error << "\n";
{ status = -1;
// 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. catch (std::bad_alloc& error)
// TODO Load CLI grammar. {
// TODO Load from string, else file on config override. std::cerr << "Error: Memory allocation failed: " << error.what () << "\n";
// TODO Migrate from loading a grammar from file, to a default string. status = -3;
/* }
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. catch (...)
{
// Load the rules. std::cerr << "Error: Unknown problem, please report.\n";
Rules rules; status = -2;
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;
}
} }
return status; return status;

View file

@ -32,6 +32,7 @@
#include <Rules.h> #include <Rules.h>
// init.cpp // init.cpp
bool lightweightVersionCheck (int, const char**);
void initializeData (Configuration&, Database&); void initializeData (Configuration&, Database&);
void initializeRules (Configuration&, Rules&); void initializeRules (Configuration&, Rules&);
int dispatchCommand (int, const char**, Configuration&, Rules&); int dispatchCommand (int, const char**, Configuration&, Rules&);